Menu
AWS IoT
Developer Guide

Creating a 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. You will create a Lambda function that publishes a message to the Amazon SNS topic you created in the AWS IoT Getting Started Tutorial. You will 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, we also assume you are using an AWS IoT button to trigger the Lambda rule. If you do not have an AWS IoT button, you can buy one here or you can use an MQTT client to send an MQTT message that triggers the rule.

Create the Lambda Function

To create the Lambda function:

  1. In the AWS Lambda console, choose Get Started Now or, if you have created a Lambda function before, choose Create a Lambda function.

  2. On the Select blueprint page, in the Filter field, type hello-world, and then choose the hello-world blueprint.

  3. On the Configure triggers page, select the box to the left of the Lambda icon, and select AWS IoT from the drop-down menu.

  4. In the Device Serial Number field, type your button's device serial number (DSN). The DSN is printed on the back of your AWS IoT button. If you have not already generated a certificate and private key for your AWS IoT button, choose Generate certificate and keys. Otherwise, skip to step 6.

  5. Choose the links to download your certificate PEM and private key. Save these files in a secure location on your computer.

    Follow the online instructions to configure your AWS IoT button.

  6. Make sure that the Enable trigger check box is selected, and then choose Next.

  7. On the Configure function page, type a name and description for the Lambda function. In Runtime, choose Node.js 6.10.

  8. Scroll down to the Lambda 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.clickType + " message from button ID: " + event.serialNumber; // 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-east-1:123456789012:MyIoTButtonSNSTopic" }; sns.publish(params, context.done); };

    Note

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

  9. Scroll down to the Lambda function handler and role section of the page. For Role, choose Create a custom role. The IAM console opens, allowing you to create an IAM role that Lambda can assume when executing the Lambda function.

    To edit the role's policy to give it permission to publish to your Amazon SNS topic:

    1. Choose View Policy Document.

    2. Choose Edit to edit the role's policy.

    3. 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-east-1:123456789012:MyIoTButtonSNSTopic" } ] }

      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 previously.

  10. Choose Allow.

  11. Leave the settings on the Advanced settings page at their defaults, and choose Next.

  12. On the Review page, choose Create function.

Test Your Lambda Function

To test the Lambda function:

  1. From the Actions menu, choose Configure test event.

  2. Copy and paste the following JSON into the Input test event page, and then choose Save and test.

    { "serialNumber": "ABCDEFG12345", "clickType": "SINGLE", "batteryVoltage": "2000 mV" }
  3. In the AWS Lambda console, scroll to the bottom of the page. The Log output section displays the output the Lambda function has written to the console.

Creating a Lambda Rule

Now that you have created a Lambda function, you can create a rule that invokes the Lambda function.

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

  2. On the Rules page, choose Create.

  3. Type a name and description for the rule.

  4. Enter the following settings for the rule:

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

  6. On the Select an action page, select Invoke a Lambda function passing the message data, and then choose Configure action.

  7. From the Function name drop-down list, choose your Lambda function name, then choose Add action.

  8. Choose Create rule to create your Lambda function.

Test Your Lambda Rule

In this tutorial, we assume you have completed the AWS IoT Getting Started Tutorial, which covers:

  • Configuring an AWS IoT button.

  • Creating and subscribing to an Amazon SNS topic with a cell phone number.

Now that your button is configured and connected to Wi-Fi and you have configured an Amazon SNS topic, you can press the button to test your Lambda rule. You should receive an SMS text message on your phone that contains:

  • The serial number of your button.

  • The type of button press (SINGLE or DOUBLE).

  • The battery voltage.

The message should look like the following:

IOT BUTTON> { "serialNumber" : "ABCDEFG12345", "clickType" : "SINGLE", "batteryVoltage" : "2000 mV" }

If you do not have a button, you can buy one here or you can use the AWS IoT MQTT client instead.

  1. In the AWS IoT console, choose Test.

  2. On the MQTT client page, in the Publish section, in Specify a topic, type iotbutton/ABCDEFG12345.

    In Payload, type the following JSON, and then choose Publish to topic.

    { "serialNumber" : "ABCDEFG12345", "clickType" : "SINGLE", "batteryVoltage" : "2000 mV" }
  3. You should receive a message on your cell phone.