AWS IoT
Developer Guide

Creating an AWS Lambda Rule

You can define a rule that calls a Lambda function, passing in data from the MQTT message that triggered the rule. This allows you to extract data from the incoming message and then call another AWS or third-party service. In this tutorial, we assume you have completed the AWS IoT Getting Started Tutorial in which you create and subscribe to an Amazon SNS topic. Now you create a Lambda function that publishes a message to the Amazon SNS topic you created in the AWS IoT Getting Started Tutorial. You also create a Lambda rule that calls the Lambda function, passing in some data from the MQTT message that triggered the rule.

In this tutorial, you use the AWS IoT MQTT client to send a message that triggers the rule.

Create a Lambda Function

  1. In the AWS Lambda console, choose Create function.

  2. On the Create function page, choose Use a blueprint. In the Blueprints filter field, enter hello-world, and then press Enter. Choose the hello-world-python blueprint, and then choose Configure.

  3. In Basic information, enter a name for your function.

    Note

    We do not recommend the use of personally identifiable information in your rule names or descriptions.

  4. From Execution Role, choose Create a new role from AWS policy templates. Under Role name, enter a name for the role. From Policy templates, choose Amazon SNS publish policy. Click outside of the drop-down menu to dismiss it.

  5. Choose Create function.

  6. In the Lambda console, choose the name of your Lambda function. Information about your Lambda function is displayed. Scroll down to the Function code section and replace the existing code with the following:

    from __future__ import print_function import json import boto3 print('Loading function') def lambda_handler(event, context): # Parse the JSON message eventText = json.dumps(event) # Print the parsed JSON message to the console; you can view this text in the Monitoring tab in the Lambda console or in the CloudWatch Logs console print('Received event: ', eventText) # Create an SNS client sns = boto3.client('sns') # Publish a message to the specified topic response = sns.publish ( TopicArn = 'arn:aws:iam::123456789012:role/service-role/myLambdaFunctionRole', Message = eventText ) print(response)

    Note

    Replace the value of TopicArn with the ARN of the Amazon SNS topic you created earlier.

    Choose Save.

Test Your Lambda Function

  1. In the upper right of the Lambda function detail page, from Select a test event, choose Configure test events.

  2. On Configure test event, enter a name for your test event and replace the message JSON with the following:

    { "message" : "Hello, world" }

    Choose Create.

  3. In the upper right of the Lambda function detail page, choose Test to test your Lambda function with the message you specified in the test event.

  4. Under your Lambda function code, on the Execution result tab, you see the output from the Lambda function.

Create a Lambda Rule

This section provides steps for creating a rule with a Lambda action and an error action. The Lambda action calls your Lambda function. If an error occurs when calling the Lambda function, the error action publishes a message to the lambda/error MQTT topic. This is useful when you are testing the rule.

  1. Browse to the AWS IoT console, and from the navigation pane, choose Act.

  2. Choose Create to create an AWS IoT rule.

  3. On the Create a rule page, enter a name for your rule.

  4. In Rule query statement, enter the following query:

    SELECT * FROM "my/lambda/topic"
  5. In Set one or more actions, choose Add action.

  6. Under Select an action, choose Send a message to a Lambda function, and then choose Configure action.

  7. On Configure action, choose Select.

  8. Choose your Lambda function.

  9. Choose Add action.

  10. On the Create a rule page, in Error action, choose Add action.

  11. On the Set an action as error action page, choose Republish a message to an AWS IoT topic, and then choose Configure action.

  12. On the Configure action page, under Topic, enter lambda/error.

  13. Under Choose or create a role to grant AWS IoT access to perform this action, choose Create Role.

  14. In Create a new role, enter a name for the role, and then choose Create role.

  15. In Configure action, choose Add action.

  16. In Create a rule, choose Create rule.

Test Your Lambda Rule

  1. To test your Lambda rule, open the AWS IoT console, and from the navigation pane, choose Test.

  2. In the MQTT client, under Subscription topic, enter lambda/error, and then choose Subscribe to topic.

  3. Under Publish, enter my/lambda/topic, and then choose Publish to topic to publish the default JSON message.

Publishing this message should trigger the rule and call your Lambda function. Your Lambda function pushes an Amazon SNS message to a phone number subscribed to your Amazon SNS topic. If you do not get a text message, in the MQTT client, check to see if any messages were published to lambda/error.

Troubleshooting Lambda Rules

If your Lambda function is called, but you do not receive a text message, make sure your phone number is subscribed to your Amazon SNS topic. If your phone number is subscribed, check the CloudWatch logs for your Lambda function. AWS Lambda writes logs to CloudWatch, which makes it possible for you to see output from your Lambda function.

To view CloudWatch Logs

  1. In the Lambda console, from the navigation pane, choose Functions.

  2. Choose your Lambda function.

  3. On the Lambda function detail page, choose the Monitoring tab.

  4. Choose View logs in CloudWatch.

  5. Choose the latest log stream.

  6. The log stream displays the logs written by your Lambda function.