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 process 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 using your cell phone number. 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 the Lambda Function

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

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

  3. On the Basic information page, enter a name for your function.

    Note

    We do not recommend using personally identifiable information in your rule names or descriptions.

  4. In the Role drop-down menu, choose Create a custom role. This opens the IAM console in a new tab. In the IAM console, enter a name for the role, and then choose View Policy Document.

  5. Choose Edit to edit the role's policy and replace the policy document with the following.

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "sns:Publish" ], "Resource": "arn:aws:sns:us-west-2:123456789012:MyIotSNSTopic" } ] }

    This policy document adds permission to publish to your Amazon SNS topic.

    Note

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

    Choose Allow.

  6. In the Lambda console, choose Create function.

  7. On the Configuration page for the function, under Add Triggers, choose AWS IoT.

  8. In Configure triggers, under IoT type, choose Custom IoT rule. Under Rule, choose Create a new rule.

    1. In Rule name, enter a name for the rule.

    2. In Rule description, enter a short rule description.

    3. In Rule query statement, enter SELECT * FROM "my/lambda/topic".

    4. Choose Enable trigger.

    5. Choose Add.

    6. In the upper-right corner of the page, choose Save.

  9. On the Configuration page for the function, choose the name of your Lambda function. In the Function code section of the page, replace the existing code with the following.

    console.log('Loading function'); // Load the AWS SDK var AWS = require("aws-sdk"); // Set up the code to call when the Lambda function is invoked exports.handler = (event, context, callback) => { // Load the message passed into the Lambda function into a JSON object var eventText = JSON.stringify(event, null, 2); // Log a message to the console; you can view this text in the Monitoring tab in the Lambda console or in the CloudWatch Logs console console.log("Received event:", eventText); // Create a string, extracting the click type and serial number from the message sent by the AWS IoT button var messageText = "Received " + event.message + " from AWS IoT rule"; // Write the string to the console console.log("Message to send: " + messageText); // Create an SNS object var sns = new AWS.SNS(); // Populate the parameters for the publish operation // - Message : the text of the message to send // - TopicArn : the ARN of the Amazon SNS topic to which you want to publish var params = { Message: messageText, TopicArn: "arn:aws:sns:us-west-2:123456789012:MyIotSNSTopic" }; sns.publish(params, context.done); };

    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 corner of the Configuration page for the function, choose Test.

  2. In the Configure test event page:

    1. Choose Create new test event.

    2. Enter a name for the test event.

    3. In the text box, enter the follow JSON code:

      { "message" : "Hello, world" }
    4. Choose Create.

  3. Choose Test again to run the test. In the AWS Lambda console, scroll to the bottom of the page. The Execution Result tab displays the output the Lambda function has written to the console.

Test Your Lambda Rule

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

  2. Choose Publish to a topic.

  3. For the topic, enter my/lambda/topic.

  4. For the message, enter the following JSON:

    { "message" : "Hello, world" }

    You should receive a text message on the line you configured with SNS.