In this step, you will create a deployment package for your Lambda function. Cloud9 is used as the development environment.
mkdir -p ~/environment/lambda && touch ~/environment/lambda/lambda_function.py
obtain your ATS endpoint using following AWS CLI command:
aws iot describe-endpoint \ --endpoint-type iot:Data-ATS
endpoint_urlto your account AWS IoT Core endpoint by using the endpoint details obtained above)
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 import json import boto3 iot = boto3.client('iot-data', endpoint_url='https://xxxxxxxxxxx-ats.iot.eu-west-1.amazonaws.com') def lambda_handler(event, context): print(event) topic = 'my/topic/alert' threshold = 26.0 if event['value'] >= threshold: payload = event payload['threshold'] = threshold iot.publish( topic=topic, qos=0, payload=json.dumps(payload, ensure_ascii=False) )
endpoint_url ='https: //xxxxxxxxxxxpart to your own endpoint (you can also obtain this using CLI as described in Step 1)
IoT Corefrom the management console
Settingsfrom the left menu
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.
cd ~/environment/lambda docker run --rm -v $(pwd):/var/task amazon/aws-sam-cli-build-image-python3.8:latest \ pip install boto3 -t ./
zip -r lambda_artifact.zip ./
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