AWS 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 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.

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

  2. From the GitHub repository download the carAggregator.py Lambda function to the car_aggregator folder.

  3. 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:

    
                            Screenshot of directory listing showing
                                carAggregator.py.

    Greengrass Lambda functions use the AWS SDK to access other Amazon Web 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 car_aggregator.zip. 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:

    • Runtime - choose Python 2.7.

    • Role - choose Choose an existing role.

    • Existing role - choose Lambda_DynamoDB_Role.

    Then, 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, as follows:

    1. 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.

    2. Choose Upload, and then choose car_aggregator.zip.

    3. Choose Save.

      
                            Screenshot of GG_Car_Aggregator with the Handler field set to
                                carAggregator.function_handler.
  7. 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).

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

    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.

    Note

    You can remove other Lambda functions from earlier modules.

  9. Edit the Lambda function configuration, as follows:

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

      
                            The Edit Configuration option highlighted for the Lambda function.
    2. Under Lambda lifecycle, select 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.