AWS IoT Core is a managed service that lets you securely join billions of Web of Issues (IoT) gadgets to the AWS cloud. The AWS IoT guidelines engine is a element of AWS IoT Core and gives SQL-like capabilities to filter, remodel, and decode your IoT machine information. You need to use AWS IoT guidelines to route information to greater than 20 AWS companies and HTTP endpoints utilizing AWS IoT rule actions. Substitution templates are a functionality in IoT guidelines that augments the JSON information returned when a rule is triggered and AWS IoT performs an motion. This weblog publish explores how AWS IoT rule actions with substitution templates unlock easier, extra highly effective IoT architectures. You’ll be taught confirmed methods to chop prices and improve scalability. By means of sensible examples of message routing and cargo balancing, smarter, extra environment friendly IoT options.
Understanding the basic elements
Every AWS IoT rule is constructed upon three elementary elements: a SQL-like assertion that handles message filtering and transformation, a number of IoT rule actions that run and route information to completely different AWS and third occasion companies, and non-obligatory capabilities that may be utilized in each the SQL assertion and rule actions.
The next is an instance of an AWS IoT rule and its elements.
{
"sql": "SELECT *, get_mqtt_property(title) FROM 'gadgets/+/telemetry'",
"actions":[
{
"s3":{
"roleArn": "arn:aws:iam::123456789012:role/aws_iot_s3",
"bucketname": "MyBucket",
"key" : "MyS3Key"
}
}
]
}
The SQL assertion serves because the gateway for rule processing and determines which MQTT messages ought to be dealt with primarily based on particular matter patterns and circumstances. The rule employs a SQL-like and helps SELECT, FROM, and WHERE clauses (for extra info, see AWS IoT SQL reference). Inside this construction, the FROM clause defines the MQTT matter filter, and the SELECT and WHERE clauses specify which information parts ought to be extracted or reworked from the incoming message.
Features are important to the SQL assertion and IoT rule actions. AWS IoT guidelines present an in depth assortment of inner capabilities designed to transform information varieties, manipulate strings, carry out mathematical calculations, deal with timestamps, and way more. Moreover, AWS IoT guidelines present a set of exterior capabilities that assist you to to retrieve information from AWS companies (similar to, Amazon DynamoDB, AWS Lambda, Amazon Secrets and techniques Supervisor, and AWS IoT Gadget Shadow) and embed that information in your message payload. These capabilities help subtle information transformations instantly throughout the rule processing pipeline and eliminates the necessity for exterior processing.
Rule actions decide the vacation spot and dealing with of processed information. AWS IoT guidelines help a library of built-in rule actions that may transmit information to AWS companies, like AWS Lambda, Amazon Easy Storage Service (Amazon S3), Amazon DynamoDB, and Amazon Easy Queue Service (Amazon SQS). These rule actions also can transmit information to third-party companies like Apache Kafka. Every rule motion could be configured with particular parameters that govern how the info ought to be delivered or processed by the goal service.
Substitution templates: The hidden gem
You’ll be able to implement capabilities throughout the AWS IoT rule SELECT and WHERE statements to remodel and put together message payloads. When you apply this method too steadily, nonetheless, you would possibly overlook the highly effective possibility to make use of substitution templates and carry out transformations instantly throughout the IoT rule motion.
Substitution templates help dynamically inserted values and rule capabilities into the rule motion’s JSON utilizing the ${expression} syntax. These templates help many SQL assertion capabilities, similar to timestamp manipulation, encoding/decoding operations, string processing, and matter extraction. While you make the most of substitution templates inside AWS IoT rule actions, you’ll be able to implement subtle routing that considerably reduces the complexity in different architectural layers, leading to extra environment friendly and maintainable AWS IoT options.
Actual-world implementation patterns
Let’s dive into some sensible examples that present the flexibility and energy of utilizing substitution templates in AWS IoT guidelines actions. These examples will display how this characteristic can simplify your IoT information processing pipelines and unlock new capabilities in your IoT purposes.
Instance 1: Conditional message distribution utilizing AWS IoT registry attributes
Take into account a typical IoT situation the place a platform distributes machine messages to completely different enterprise companions, and every companion has their very own message processing SQS queue. Totally different companions personal every machine within the fleet and their relationship is maintained within the registry as a factor attribute known as partnerId.
The standard method consists of the next:
- Choice 1 – Keep companion routing logic on the machine. A number of AWS IoT guidelines depend on WHERE circumstances to enter payload:
- Requires gadgets to know their companion’s ID.
- Will increase machine complexity and upkeep.
- Creates safety issues with exposing companion identifiers.
- Makes companion modifications tough to handle.
- Choice 2 – Make use of an middleman Lambda perform to retrieve the companion ID values related to gadgets from the AWS IoT registry and subsequently propagate the message to the companion particular SQS queue:
- Provides pointless compute and registry question prices.
- Probably will increase message latency.
- Creates extra factors of failure.
- Requires upkeep of routing logic.
- Could face Lambda concurrency limits.
Right here’s a extra elegant answer and course of that makes use of substitution templates and the brand new AWS IoT propagating attributes characteristic:
- Insert the Accomplice IDs as attributes within the AWS IoT registry
- Use the propagating attributes characteristic to counterpoint your MQTTv5 person property and dynamically assemble the Amazon SQS queue URL utilizing the machine’s
partnerId. See the next instance:
{
"ruleArn": "arn:aws:iot:us-east-1:123456789012:rule/partnerMessageRouting",
"rule": {
"ruleName": "partnerMessageRouting",
"sql": "SELECT * FROM 'gadgets/+/telemetry'",
"actions": [{
"sqs": {
"queueUrl": "https://sqs.us-east-1.amazonaws.com/123456789012/partner-queue-${get(get_user_properties('partnerId'),0}}",
"roleArn": "arn:aws:iam::123456789012:role/service-role/iotRuleSQSRole",
"useBase64": false
}
}],
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23"
}
}
Utilizing this answer, a tool with partnerId=”partner123″ publishes a message. The message is routinely routed to the “partner-queue-partner123” SQS queue.
Advantages of this answer:
Utilizing the substitution template considerably simplifies the structure and gives a scalable and maintainable answer for partner-specific message distribution. The answer,
- Eliminates the necessity for extra compute assets.
- Gives rapid routing with out added latency.
- Simplifies companion relationship administration by updates within the AWS IoT factor registry. For instance, introducing new companions, could be up to date by modifying the registry attributes. This replace wouldn’t require any updates or modifications to the gadgets or the routing logic.
- Maintains safety by not exposing queue info to gadgets.
Instance 2: Clever load balancing with Amazon Kinesis Knowledge Firehose
Take into account a situation the place tens of millions of gadgets publish telemetry information to the identical matter. There’s additionally a have to distribute this high-volume information throughout a number of Amazon Knowledge Firehose streams to keep away from throttling points when buffering the info to Amazon S3.
The standard method consists of the next:
- Gadget-side load balancing:
- Implement configuration administration to offer completely different stream IDs throughout the gadgets.
- Require the gadgets to incorporate stream focusing on of their messages.
- Create a number of AWS IoT guidelines to match the particular stream IDs.
- AWS Lambda-based routing:
- Deploy a Lambda perform to distribute messages throughout streams.
- Implement customized load balancing logic.
Conventional approaches exhibit comparable destructive impacts as outlined within the previous instance (upkeep overhead, safety vulnerabilities, machine complexity, extra prices, elevated latency, and failure factors). Moreover, they current particular challenges in high-volume situations, similar to heightened threat of throttling and complicated streams administration.
By leveraging AWS IoT rule substitution templates, you’ll be able to implement a streamlined, serverless load balancing answer that dynamically assigns messages to completely different Firehose supply streams by:
- Generate a random quantity between 0-100000 utilizing rand()*100000.
- Convert (casting) this random quantity to an integer.
- Use modulo operation (mod) to get the rest when divided by 8.
- Append this the rest (0-7) to the bottom title “firehose_stream_”.
The result’s that messages are randomly distributed throughout eight completely different Amazon Knowledge Firehose streams (firehose_stream_0 by firehose_stream_7). See the next instance:
{
"ruleArn":
"arn:aws:iot:us-east-1:123456789012:rule/testFirehoseBalancing",
"rule": {
"ruleName": "testFirehoseBalancing",
"sql": "SELECT * FROM 'gadgets/+/telemetry'",
"description": "",
"createdAt": "2025-04-11T11:09:02+00:00",
"actions": [
{ "firehose": {
"roleArn": "arn:aws:iam::123456789012:role/service-role/firebaseDistributionRoleDemo",
"deliveryStreamName": "firehose_stream_${mod(cast((rand()*100000) as Int),8)}",
"separator": ",",
"batchMode": false
}
}
],
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23"
}
}
Advantages of this answer:
This versatile load balancing sample helps to deal with excessive message volumes by spreading the load throughout a number of streams. The first benefit of this method lies in its scalability. By modifying the modulo perform (which determines the rest of a division, as an illustration, 5 mod 3 = 2), the dividend (at the moment set to eight) could be adjusted to correspond with the specified variety of streams. For instance:
- Change to mod(…, 4) for distribution throughout 4 streams.
- Change to mod(…, 16) for distribution throughout 16 streams.
Utilizing this template makes it straightforward to scale your structure up or down with out altering the core logic of the rule.
Instance 3: Use CASE statements in substitution templates to construct a conditional routing logic
Take into account a situation the place you must route your IoT machine information, relying on the particular machine, both to a production-based or to a Improvement/Testing (Dev/Check) Lambda perform.
The standard method consists of the next:
- Gadget-side load balancing:
-
- Implement configuration administration to offer completely different setting IDs throughout the gadgets.
- Require the gadgets to incorporate an setting IDs of their messages.
- Create a number of AWS IoT guidelines to match the particular setting IDs.
- AWS Lambda-based routing:
- Deploy a Lambda perform to distribute messages throughout the completely different setting AWS Lambda capabilities after a examine towards the AWS IoT registry (or another database).
Conventional approaches exhibit the identical destructive impacts as outlined within the previous examples.
Right here’s a extra elegant answer and course of that makes use of substitution templates and the brand new AWS IoT propagating attributes characteristic:
- Affiliate the setting IDs as attributes for all gadgets within the AWS IoT Registry
- Use the propagating attributes characteristic to counterpoint your MQTTv5 person property
- Make the most of the propagated property to dynamically assemble the AWS Lambda perform ARN inside a CASE assertion embedded throughout the AWS IoT Rule motion definition.
See the next instance:
{
"ruleArn":
"arn:aws:iot:us-east-1:123456789012:rule/ConditionalActions",
"rule": {
"ruleName": "testLambdaConditions",
"sql": "SELECT * FROM 'gadgets/+/telemetry'",
"description": "",
"createdAt": "2025-04-11T11:09:02+00:00",
"actions": [
{ "lambda": {
"functionArn":
"arn:aws:lambda:us-east-1:123456789012:function:${CASE get(get_user_properties('environment'),0)
WHEN "PROD" THEN "message_handler_PROD"
WHEN "DEV" THEN "message_handler_DEV"
WHEN NULL THEN "message_handler_PROD"
ELSE "message_handler_PROD" END }",
}
}
],
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23"
}
}
Advantages of this answer:
Utilizing the substitution template considerably simplifies the structure and gives a scalable and maintainable answer for partner-specific message distribution. The answer,
- Removes the requirement to outline separate IoT rule and IoT rule actions for every situation.
- Helps you scale back the price of utilizing IoT guidelines and IoT rule actions.
Conclusion
This weblog publish explored how substitution templates for AWS IoT guidelines can remodel complicated IoT architectures into elegant and environment friendly options. The examples demonstrated that substitution templates are greater than only a characteristic – they’re a robust architectural instrument that leverages AWS IoT capabilities to effectively remedy complicated challenges with out introducing extra complexity or value. Substitution templates present a serverless, scalable method that eliminates the necessity for extra compute assets or complicated client-side logic. This method not solely reduces operational overhead but additionally gives rapid value advantages by eradicating pointless compute assets and simplifying the general structure.
The subsequent time you end up designing AWS IoT message routing patterns or dealing with scaling challenges, think about how a substitution template would possibly supply an easier and extra environment friendly answer. By leveraging these highly effective AWS IoT options, you’ll be able to create extra maintainable, cost-effective, and scalable IoT options that really serve your online business wants.
Keep in mind: The only answer is usually essentially the most elegant one. With AWS IoT rule substitution templates, that simplicity comes in-built.
In regards to the Authors
Andrea Sichel is a Principal Specialist IoT Options Architect at Amazon Net Companies, the place he helps clients navigate their cloud adoption journey within the IoT area. Pushed by curiosity and a customer-first mindset, he works on creating progressive options whereas staying on the forefront of cloud know-how. Andrea enjoys tackling complicated challenges and serving to organizations suppose large about their IoT transformations. Exterior of labor, Andrea coaches his son’s soccer group and pursues his ardour for pictures. When not behind the digicam or on the soccer subject, you will discover him swimming laps to remain energetic and keep a wholesome work-life stability.
Avinash Upadhyaya is Senior Product Supervisor for AWS IoT Core the place he’s accountable to outline product technique, roadmap prioritization, pricing, and a go-to-market technique for options throughout the AWS IoT service.


