Create and Configure the Lambda Function
In this step, you create a Lambda function that tracks the number of cars
that pass the traffic light. Every time that the GG_TrafficLight
shadow state changes to G
, the Lambda simulates the passing of a randomized number of cars
(from 1 to 20). On every third G
light change, the Lambda sends basic statistics, such as
min and max, to a DynamoDB table.
-
On your computer, create a folder named
car_aggregator
. -
From the GitHub repository download the
carAggregator.py
Lambda function to thecar_aggregator
folder. -
Install the boto3 package (AWS SDK for Python) and its dependencies in the
car_aggregator
folder by running the following command in a command-line window (for Windows, use an elevated command prompt):pip install boto3 -t
path-to-car_aggregator-folder
This results in a directory listing similar to the following:
Greengrass Lambda functions use the AWS SDK to access other Amazon Web Services. For more information, see Boto 3 - The AWS SDK for Python.
-
Compress the contents of the
car_aggregator
folder into a.zip
file namedcar_aggregator.zip
. This is your Lambda function deployment package. -
In the Lambda console, create a function named
GG_Car_Aggregator
, and set the remaining fields as follows:-
Runtime - choose Python 2.7.
-
Role - choose Choose an existing role.
-
Existing role - choose Lambda_DynamoDB_Role.
Then, choose Create function.
-
-
Upload your Lambda function deployment package, as follows:
-
On the Configuration tab, under Function code, set the following fields:
-
Code entry type - choose Upload a .ZIP file.
-
Runtime - choose Python 2.7.
-
Handler - type
carAggregator.function_handler
.
-
-
Choose Upload, and then choose
car_aggregator.zip
. -
Choose Save.
-
-
Publish the Lambda function, and then create an alias named
GG_CarAggregator
. For step-by-step instructions, see the Publish the Lambda function and Create an alias steps in Module 3 (Part 1). -
In the AWS IoT console, add the Lambda function that you just created to your AWS Greengrass group, as follows:
-
On the group configuration page, choose Lambdas, and then choose Add Lambda:
-
Choose Use existing Lambda:
-
Choose GG_Car_Aggregator, and then choose Next:
-
Choose Alias: GG_CarAggregator, and then choose Finish:
Note
You can remove other Lambda functions from earlier modules.
-
-
Edit the Lambda function configuration, as follows:
-
Choose the ellipsis (…) associated with the Lambda function, then choose Edit Configuration:
-
Under Lambda lifecycle, select Make this function long-lived and keep it running indefinitely, and then choose Update:
-