AWS IoT Greengrass
Developer Guide

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 function simulates the passing of a random number of cars (from 1 to 20). On every third G light change, the Lambda function sends basic statistics, such as min and max, to a DynamoDB table.

  1. On your computer, create a folder named car_aggregator.

  2. From the AWS IoT Greengrass samples repository on GitHub, download the function to the car_aggregator folder.

  3. Run the following command in a command-line window to install the boto3 (AWS SDK for Python) package and its dependencies in the car_aggregator folder. (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:

                            Screenshot of directory listing showing

    Greengrass Lambda functions use the AWS SDK to access other AWS services. For more information, see Boto 3 - The AWS SDK for Python.

  4. Compress the contents of the car_aggregator folder into a .zip file named (Compress the folder's contents, not the folder.) This is your Lambda function deployment package.

  5. In the Lambda console, create a function named GG_Car_Aggregator, and set the remaining fields as follows:

    • For Runtime, choose Python 2.7.

    • For Role, choose Choose an existing role.

    • For Existing role, choose Lambda_DynamoDB_Role.

    Choose Create function.

                            Author from scratch panel with Name set to GG_Car_Aggregator,
                                Runtime to Python 2.7, Role to Choose an existing role and Existing
                                role to Lambda_DynamoDB_Role.
  6. Upload your Lambda function deployment package:

    1. On the Configuration tab, under Function code, set the following fields:

      • For Code entry type, choose Upload a .zip file.

      • For Runtime, choose Python 2.7.

      • For Handler, enter carAggregator.function_handler

    2. Choose Upload, and then choose

    3. Choose Save.

                            Screenshot of GG_Car_Aggregator with the Handler field set to
  7. Publish the Lambda function, and then create an alias named GG_CarAggregator. For step-by-step instructions, see the steps to publish the Lambda function and create an alias in Module 3 (Part 1).

  8. In the AWS IoT Core console, add the Lambda function that you just created to your AWS IoT Greengrass group:

    1. On the group configuration page, choose Lambdas, and then choose Add Lambda.

    2. Choose Use existing Lambda.

                                    Use existing Lambda highlighted.
    3. Choose GG_Car_Aggregator, and then choose Next.

                                    GG_Car_Aggregator and Next highlighted.
    4. Choose Alias: GG_CarAggregator, and then choose Finish.

                                    Alias: GG_CarAggregator and Finish highlighted.


    You can remove other Lambda functions from earlier modules.

  9. Edit the Lambda function configuration:

    1. Choose the ellipsis () associated with the Lambda function, and then choose Edit Configuration.

                            The Edit Configuration option highlighted for the Lambda function.
    2. Under Lambda lifecycle, choose Make this function long-lived and keep it running indefinitely, and then choose Update.

                            GG_Car_Aggregator configuration page with Make this function
                                long-lived and keep it running indefinitely highlighted.