AWS IoT Core is a managed service that lets you securely join billions of Web of Issues (IoT) units to the AWS cloud. The AWS IoT guidelines engine is a element of AWS IoT Core and offers SQL-like capabilities to filter, remodel, and decode your IoT machine information. You should utilize AWS IoT guidelines to route information to greater than 20 AWS providers 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 put up explores how AWS IoT rule actions with substitution templates unlock easier, extra highly effective IoT architectures. You’ll study 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 elemental 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 providers, and optionally available features 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(identify) FROM 'units/+/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 must be dealt with primarily based on particular matter patterns and situations. 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 must be extracted or reworked from the incoming message.
Capabilities are important to the SQL assertion and IoT rule actions. AWS IoT guidelines present an intensive assortment of inside features designed to transform information varieties, manipulate strings, carry out mathematical calculations, deal with timestamps, and far more. Moreover, AWS IoT guidelines present a set of exterior features that assist you to retrieve information from AWS providers (comparable to, Amazon DynamoDB, AWS Lambda, Amazon Secrets and techniques Supervisor, and AWS IoT System Shadow) and embed that information in your message payload. These features assist subtle information transformations immediately 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 assist a library of built-in rule actions that may transmit information to AWS providers, like AWS Lambda, Amazon Easy Storage Service (Amazon S3), Amazon DynamoDB, and Amazon Easy Queue Service (Amazon SQS). These rule actions may also transmit information to third-party providers like Apache Kafka. Every rule motion will be configured with particular parameters that govern how the information must be delivered or processed by the goal service.
Substitution templates: The hidden gem
You possibly can implement features throughout the AWS IoT rule SELECT and WHERE statements to rework and put together message payloads. In case you apply this method too ceaselessly, nonetheless, you may overlook the highly effective possibility to make use of substitution templates and carry out transformations immediately throughout the IoT rule motion.
Substitution templates assist dynamically inserted values and rule features into the rule motion’s JSON utilizing the ${expression}
syntax. These templates assist many SQL assertion features, comparable to timestamp manipulation, encoding/decoding operations, string processing, and matter extraction. If you make the most of substitution templates inside AWS IoT rule actions, you may 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 reveal how this function can simplify your IoT information processing pipelines and unlock new capabilities in your IoT functions.
Instance 1: Conditional message distribution utilizing AWS IoT registry attributes
Think about a typical IoT state of affairs the place a platform distributes machine messages to completely different enterprise companions, and every associate 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 referred to as partnerId.
The normal method consists of the next:
- Choice 1 – Keep associate routing logic on the machine. A number of AWS IoT guidelines depend on WHERE situations to enter payload:
- Requires units to know their associate’s ID.
- Will increase machine complexity and upkeep.
- Creates safety issues with exposing associate identifiers.
- Makes associate adjustments tough to handle.
- Choice 2 – Make use of an middleman Lambda perform to retrieve the associate ID values related to units from the AWS IoT registry and subsequently propagate the message to the associate particular SQS queue:
- Provides pointless compute and registry question prices.
- Doubtlessly will increase message latency.
- Creates further factors of failure.
- Requires upkeep of routing logic.
- Could face Lambda concurrency limits.
Right here’s a extra elegant resolution and course of that makes use of substitution templates and the brand new AWS IoT propagating attributes function:
- Insert the Associate IDs as attributes within the AWS IoT registry
- Use the propagating attributes function to complement your MQTTv5 consumer 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 'units/+/telemetry'",
"actions": [{
"sqs": {
"queueUrl": "
"roleArn": "arn:aws:iam::123456789012:role/service-role/iotRuleSQSRole",
"useBase64": false
}
}],
"ruleDisabled": false,
"awsIotSqlVersion": "2016-03-23"
}
}
Utilizing this resolution, a tool with partnerId
=”partner123″ publishes a message. The message is robotically routed to the “partner-queue-partner123” SQS queue.
Advantages of this resolution:
Utilizing the substitution template considerably simplifies the structure and offers a scalable and maintainable resolution for partner-specific message distribution. The answer,
- Eliminates the necessity for extra compute sources.
- Offers rapid routing with out added latency.
- Simplifies associate relationship administration by means of updates within the AWS IoT factor registry. For instance, introducing new companions, will be up to date by modifying the registry attributes. This replace wouldn’t require any updates or adjustments to the units or the routing logic.
- Maintains safety by not exposing queue info to units.
Instance 2: Clever load balancing with Amazon Kinesis Information Firehose
Think about a state of affairs the place tens of millions of units publish telemetry information to the identical matter. There’s additionally a must distribute this high-volume information throughout a number of Amazon Information Firehose streams to keep away from throttling points when buffering the information to Amazon S3.
The normal method consists of the next:
- System-side load balancing:
- Implement configuration administration to supply completely different stream IDs throughout the units.
- Require the units to incorporate stream concentrating 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 related adverse impacts as outlined within the previous instance (upkeep overhead, safety vulnerabilities, machine complexity, further prices, elevated latency, and failure factors). Moreover, they current particular challenges in high-volume situations, comparable to heightened danger of throttling and sophisticated streams administration.
By leveraging AWS IoT rule substitution templates, you may implement a streamlined, serverless load balancing resolution 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 identify “firehose_stream_”.
The result’s that messages are randomly distributed throughout eight completely different Amazon Information Firehose streams (firehose_stream_0 by means of firehose_stream_7). See the next instance:
{
"ruleArn":
"arn:aws:iot:us-east-1:123456789012:rule/testFirehoseBalancing",
"rule": {
"ruleName": "testFirehoseBalancing",
"sql": "SELECT * FROM 'units/+/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 resolution:
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 present set to eight) will 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 simple 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
Think about a state of affairs the place you’ll want to route your IoT machine information, relying on the particular machine, both to a production-based or to a Growth/Testing (Dev/Take a look at) Lambda perform.
The normal method consists of the next:
- System-side load balancing:
- Implement configuration administration to supply completely different atmosphere IDs throughout the units.
- Require the units to incorporate an atmosphere IDs of their messages.
- Create a number of AWS IoT guidelines to match the particular atmosphere IDs.
- AWS Lambda-based routing:
- Deploy a Lambda perform to distribute messages throughout the completely different atmosphere AWS Lambda features after a verify in opposition to the AWS IoT registry (or another database).
Conventional approaches exhibit the identical adverse impacts as outlined within the previous examples.
Right here’s a extra elegant resolution and course of that makes use of substitution templates and the brand new AWS IoT propagating attributes function:
- Affiliate the atmosphere IDs as attributes for all units within the AWS IoT Registry
- Use the propagating attributes function to complement your MQTTv5 consumer 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 'units/+/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 resolution:
Utilizing the substitution template considerably simplifies the structure and offers a scalable and maintainable resolution for partner-specific message distribution. The answer,
- Removes the requirement to outline separate IoT rule and IoT rule actions for every situation.
- Helps you cut back the price of utilizing IoT guidelines and IoT rule actions.
Conclusion
This weblog put up explored how substitution templates for AWS IoT guidelines can remodel advanced IoT architectures into elegant and environment friendly options. The examples demonstrated that substitution templates are greater than only a function – they’re a robust architectural software that leverages AWS IoT capabilities to effectively remedy advanced challenges with out introducing further complexity or value. Substitution templates present a serverless, scalable method that eliminates the necessity for extra compute sources or advanced client-side logic. This method not solely reduces operational overhead but in addition offers rapid value advantages by eradicating pointless compute sources and simplifying the general structure.
The subsequent time you end up designing AWS IoT message routing patterns or going through scaling challenges, contemplate how a substitution template may provide a less complicated and extra environment friendly resolution. By leveraging these highly effective AWS IoT options, you may create extra maintainable, cost-effective, and scalable IoT options that really serve your corporation wants.
Keep in mind: The only resolution is commonly probably the most elegant one. With AWS IoT rule substitution templates, that simplicity comes in-built.
Concerning the Authors
Andrea Sichel is a Principal Specialist IoT Options Architect at Amazon Internet Providers, 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 modern options whereas staying on the forefront of cloud expertise. Andrea enjoys tackling advanced challenges and serving to organizations assume large about their IoT transformations. Exterior of labor, Andrea coaches his son’s soccer staff and pursues his ardour for images. When not behind the digital camera or on the soccer subject, you will discover him swimming laps to remain lively 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.