Using Amazon Lambda with Amazon IoT - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using Amazon Lambda with Amazon IoT

Amazon IoT provides secure communication between internet-connected devices (such as sensors) and the Amazon Cloud. This makes it possible for you to collect, store, and analyze telemetry data from multiple devices.

You can create Amazon IoT rules for your devices to interact with Amazon services. The Amazon IoT Rules Engine provides a SQL-based language to select data from message payloads and send the data to other services, such as Amazon S3, Amazon DynamoDB, and Amazon Lambda. You define a rule to invoke a Lambda function when you want to invoke another Amazon service or a third-party service.

When an incoming IoT message triggers the rule, Amazon IoT invokes your Lambda function asynchronously and passes data from the IoT message to the function.

The following example shows a moisture reading from a greenhouse sensor. The row and pos values identify the location of the sensor. This example event is based on the greenhouse type in the Amazon IoT Rules tutorials.

Example Amazon IoT message event
{ "row" : "10", "pos" : "23", "moisture" : "75" }

For asynchronous invocation, Lambda queues the message and retries if your function returns an error. Configure your function with a destination to retain events that your function could not process.

You need to grant permission for the Amazon IoT service to invoke your Lambda function. Use the add-permission command to add a permission statement to your function's resource-based policy.

aws lambda add-permission --function-name my-function \ --statement-id iot-events --action "lambda:InvokeFunction" --principal iot.amazonaws.com.cn

You should see the following output:

{ "Statement": "{\"Sid\":\"iot-events\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"iot.amazonaws.com.cn\"},\"Action\":\"lambda:InvokeFunction\",\"Resource\":\"arn:aws-cn:lambda:cn-north-1:123456789012:function:my-function\"}" }

For more information about how to use Lambda with Amazon IoT, see Creating an Amazon Lambda rule.