Creating a deployment package

In this step, you will create a deployment package for your Lambda function. Cloud9 is used as the development environment.

Creating function code

  1. Execute the following command to create a template of the Lambda function
mkdir -p ~/environment/lambda && touch ~/environment/lambda/

obtain your ATS endpoint using following AWS CLI command:

aws iot describe-endpoint \
    --endpoint-type iot:Data-ATS
  1. Copy and Paste the following source code into the created empty file (change endpoint_url to your account AWS IoT Core endpoint by using the endpoint details obtained above)
# Copyright, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import json
import boto3

iot = boto3.client('iot-data', endpoint_url='')
def lambda_handler(event, context):

    topic = 'my/topic/alert'
    threshold = 26.0

    if event['value'] >= threshold:
        payload = event
        payload['threshold'] = threshold
                payload=json.dumps(payload, ensure_ascii=False)
  1. Rewrite the 4th line of the pasted source code, endpoint_url ='https: //xxxxxxxxxxx part to your own endpoint (you can also obtain this using CLI as described in Step 1)
    1. Open a new tab and click IoT Core from the management console
    2. Click Settings from the left menu
    3. Copy and rewrite the endpoint displayed in Device data endpoint

Please note that the endpoint URL displayed on the custom endpoint must be pasted into xxxxxx section in endpoint_url ='https: // xxxxxx. If you specify without https: //, an error will occur.

Package download and zip

  1. Run the following command to download the packages needed to execute the function
cd ~/environment/lambda
docker run --rm -v $(pwd):/var/task amazon/aws-sam-cli-build-image-python3.8:latest \
pip install boto3 -t ./
  1. Run the following command to zip the script files and packages you created
zip -r ./ 
  1. Download zip file

From the cloud9 file tree, right-click the created zip file and download it to your local environment.


If you encounter any issues during the workshop, first see if you can find your solution in Troubleshooting section