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:

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

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

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

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.

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.

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

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

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.

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:

Test Your Lambda
Rule

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: