Handle AWS IoT Messages using AWS Lambda

   Go back to the Task List

  [« 3: Register the Device ]    [5: Create IoT Rule »]

4: Create Lambda Function

In this task, you create a Lambda function which is used to process the device messages. You will create a very simple Lambda function which takes messages from the IoT Core and logs into the CloudWatch as dummy processing. It will also help us understand in what format the device messages are received by Lambda for processing.

  1. In the Lambda Console, click on the Functions menu in the left and then click on the Create function button.

    Lambda

  2. On the next screen, select Author from scratch as the option. Type in dojolambdafunction as the name. Select Python 3.8 as the runtime. Select Create a new role with basic Lambda permissions as the option for the permissions. Finally click on the Create function button.

    Lambda

  3. Next, you goto the Function code area and update the code with the code provided below.

    Lambda

    import json
    
    def lambda_handler(event, context):
        print(event)
    

    `

  4. In the code above, the Lambda function receives the messages (event parameter) from the IoT Core and logs into CloudWatch log.

  5. Click on the Deploy button to save and upload the lambda function code.

    Lambda

  6. The Lambda function is ready. Let’s now configure AWS IoT Core to call the Lambda function for the message processing.