Use CreateTopicRule with an Amazon SDK or CLI
The following code examples show how to use CreateTopicRule.
- .NET
-
- Amazon SDK for .NET (v4)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. /// <summary> /// Creates an IoT topic rule. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="snsTopicArn">The ARN of the SNS topic for the action.</param> /// <param name="roleArn">The ARN of the IAM role.</param> /// <returns>True if successful, false otherwise.</returns> public async Task<bool> CreateTopicRuleAsync(string ruleName, string snsTopicArn, string roleArn) { try { var request = new CreateTopicRuleRequest { RuleName = ruleName, TopicRulePayload = new TopicRulePayload { Sql = "SELECT * FROM 'topic/subtopic'", Description = $"Rule created by .NET example: {ruleName}", Actions = new List<Amazon.IoT.Model.Action> { new Amazon.IoT.Model.Action { Sns = new SnsAction { TargetArn = snsTopicArn, RoleArn = roleArn } } }, RuleDisabled = false } }; await _amazonIoT.CreateTopicRuleAsync(request); _logger.LogInformation($"Created IoT rule {ruleName}"); return true; } catch (Amazon.IoT.Model.ResourceAlreadyExistsException ex) { _logger.LogWarning($"Rule {ruleName} already exists: {ex.Message}"); return false; } catch (Exception ex) { _logger.LogError($"Couldn't create topic rule. Here's why: {ex.Message}"); return false; } }-
For API details, see CreateTopicRule in Amazon SDK for .NET API Reference.
-
- C++
-
- SDK for C++
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. //! Create an AWS IoT rule with an SNS topic as the target. /*! \param ruleName: The name for the rule. \param snsTopic: The SNS topic ARN for the action. \param sql: The SQL statement used to query the topic. \param roleARN: The IAM role ARN for the action. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::IoT::createTopicRule(const Aws::String &ruleName, const Aws::String &snsTopicARN, const Aws::String &sql, const Aws::String &roleARN, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::IoT::IoTClient iotClient(clientConfiguration); Aws::IoT::Model::CreateTopicRuleRequest request; request.SetRuleName(ruleName); Aws::IoT::Model::SnsAction snsAction; snsAction.SetTargetArn(snsTopicARN); snsAction.SetRoleArn(roleARN); Aws::IoT::Model::Action action; action.SetSns(snsAction); Aws::IoT::Model::TopicRulePayload topicRulePayload; topicRulePayload.SetSql(sql); topicRulePayload.SetActions({action}); request.SetTopicRulePayload(topicRulePayload); auto outcome = iotClient.CreateTopicRule(request); if (outcome.IsSuccess()) { std::cout << "Successfully created topic rule " << ruleName << "." << std::endl; } else { std::cerr << "Error creating topic rule " << ruleName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }-
For API details, see CreateTopicRule in Amazon SDK for C++ API Reference.
-
- CLI
-
- Amazon CLI
-
To create a rule that sends an Amazon SNS alert
The following
create-topic-ruleexample creates a rule that sends an Amazon SNS message when soil moisture level readings, as found in a device shadow, are low.aws iot create-topic-rule \ --rule-name"LowMoistureRule"\ --topic-rule-payloadfile://plant-rule.jsonThe example requires the following JSON code to be saved to a file named
plant-rule.json:{ "sql": "SELECT * FROM '$aws/things/MyRPi/shadow/update/accepted' WHERE state.reported.moisture = 'low'\n", "description": "Sends an alert whenever soil moisture level readings are too low.", "ruleDisabled": false, "awsIotSqlVersion": "2016-03-23", "actions": [{ "sns": { "targetArn": "arn:aws:sns:us-west-2:123456789012:MyRPiLowMoistureTopic", "roleArn": "arn:aws:iam::123456789012:role/service-role/MyRPiLowMoistureTopicRole", "messageFormat": "RAW" } }] }This command produces no output.
For more information, see Creating an Amazon IoT Rule
in the Amazon IoT Developers Guide. -
For API details, see CreateTopicRule
in Amazon CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. /** * Creates an IoT rule asynchronously. * * @param roleARN The ARN of the IAM role that grants access to the rule's actions. * @param ruleName The name of the IoT rule. * @param action The ARN of the action to perform when the rule is triggered. * * This method initiates an asynchronous request to create an IoT rule. * If the request is successful, it prints a confirmation message. * If an exception occurs, it prints the error message. */ public void createIoTRule(String roleARN, String ruleName, String action) { String sql = "SELECT * FROM '" + TOPIC + "'"; SnsAction action1 = SnsAction.builder() .targetArn(action) .roleArn(roleARN) .build(); // Create the action. Action myAction = Action.builder() .sns(action1) .build(); // Create the topic rule payload. TopicRulePayload topicRulePayload = TopicRulePayload.builder() .sql(sql) .actions(myAction) .build(); // Create the topic rule request. CreateTopicRuleRequest topicRuleRequest = CreateTopicRuleRequest.builder() .ruleName(ruleName) .topicRulePayload(topicRulePayload) .build(); CompletableFuture<CreateTopicRuleResponse> future = getAsyncClient().createTopicRule(topicRuleRequest); future.whenComplete((response, ex) -> { if (response != null) { System.out.println("IoT Rule created successfully."); } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to create IoT Rule."); } } }); future.join(); }-
For API details, see CreateTopicRule in Amazon SDK for Java 2.x API Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. suspend fun createIoTRule( roleARNVal: String?, ruleNameVal: String?, action: String?, ) { val sqlVal = "SELECT * FROM '$TOPIC '" val action1 = SnsAction { targetArn = action roleArn = roleARNVal } val myAction = Action { sns = action1 } val topicRulePayloadVal = TopicRulePayload { sql = sqlVal actions = listOf(myAction) } val topicRuleRequest = CreateTopicRuleRequest { ruleName = ruleNameVal topicRulePayload = topicRulePayloadVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> iotClient.createTopicRule(topicRuleRequest) println("IoT rule created successfully.") } }-
For API details, see CreateTopicRule
in Amazon SDK for Kotlin API reference.
-
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. class IoTWrapper: """Encapsulates AWS IoT actions.""" def __init__(self, iot_client, iot_data_client=None): """ :param iot_client: A Boto3 AWS IoT client. :param iot_data_client: A Boto3 AWS IoT Data Plane client. """ self.iot_client = iot_client self.iot_data_client = iot_data_client @classmethod def from_client(cls): iot_client = boto3.client("iot") iot_data_client = boto3.client("iot-data") return cls(iot_client, iot_data_client) def create_topic_rule(self, rule_name, topic, sns_action_arn, role_arn): """ Creates an AWS IoT topic rule. :param rule_name: The name of the rule. :param topic: The MQTT topic to subscribe to. :param sns_action_arn: The ARN of the SNS topic to publish to. :param role_arn: The ARN of the IAM role. """ try: self.iot_client.create_topic_rule( ruleName=rule_name, topicRulePayload={ "sql": f"SELECT * FROM '{topic}'", "actions": [ {"sns": {"targetArn": sns_action_arn, "roleArn": role_arn}} ], }, ) logger.info("Created topic rule %s.", rule_name) except ClientError as err: if err.response["Error"]["Code"] == "ResourceAlreadyExistsException": logger.info("Topic rule %s already exists. Skipping creation.", rule_name) return logger.error( "Couldn't create topic rule. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise-
For API details, see CreateTopicRule in Amazon SDK for Python (Boto3) API Reference.
-
For a complete list of Amazon SDK developer guides and code examples, see Using Amazon IoT with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.