Developer Guide

Creating an Amazon SNS Rule

You can define a rule that sends message data to an Amazon SNS topic.

In this tutorial, you create a rule that sends the name of the AWS IoT thing that triggered the rule to all subscribers of an Amazon SNS topic.

To create a rule with an SNS action

  1. In the AWS IoT console, in the left navigation pane, choose Act.

  2. On the Rules page, choose Create.

  3. Enter a name for your rule.

  4. In the Rule query statement editor, enter

    SELECT *, topic(3) as thing FROM '$aws/things/+/shadow/update/accepted'

    (The topic filter following the "FROM" specifies the topics that, when a message is published to them, trigger the rule's action. The plus sign (+) used in the topic filter is a wildcard character that matches any thing name. The "topic(3)" attribute following "SELECT" appends the thing name, which is the third topic field, onto the message contents.)

  5. In the Set one or more actions section, choose Add action.

  6. Under Select an action, choose Send a message as an SNS push notification, and then choose Configure action.

  7. On the Configure action page, for SNS target, choose Create.

  8. A new tab opens in your browser. Enter a topic name, and then choose Create.

  9. On the Configure action page, for SNS target, choose the SNS topic you just created. For Message format, choose JSON. For IAM role name, choose Create a new role.

  10. Enter a name for the role, and then choose Create a new role.

  11. Select the role you just created, and then choose Add action.

  12. Choose Create rule.

To test the rule you just created, add a subscription to the SNS topic you created, and update the shadow of any AWS IoT thing.

You can use the AWS IoT console to find a thing, open its detail page, and change the device's shadow. When the Device Shadow service is notified of the change, it publishes a message on $aws/things/MySNSThing/shadow/update/accepted. Your rule is triggered and all subscribers to your SNS topic receive a message that contains your thing's name.