Menu
AWS Greengrass
Developer Guide

Create and Configure Lambda

The following Lambda function simulates, on every GG_TrafficLight shadow state change to G, the passing of a randomized number of cars (from 1 to 20). Basic statistics, such as min and max, are then passed to a DynamoDB table on every third G light change.

  1. Create a folder called car_aggregator. From the GitHub repository download the carAggregator.py Lambda function to the car_aggregator folder. Because the carAggregator.py Lambda function accesses other Amazon Web Services, you'll need to install the boto3 package (see the README) and its dependencies in the car_aggregator folder (containing the carAggregator.py file). On you computer, open a command-line window and run the following (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.

    Compress these files into one .zip file named car_aggregator.zip.

  2. In the Lambda console, create a function named GG_Car_Aggregator with the following properties:

    
                            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.

    After choosing Create function (prior image), upload the .zip file you just created. Be sure to update the function handler to carAggregator.function_handler, as shown:

    
                            Screenshot of GG_Car_Aggregator with the Handler field set to
                                carAggregator.function_handler.

    Publish the version of this Lambda function and create an alias for it named GG_CarAggregator. For step-by-step instructions, see Module 3 (Part 1) step 4g.

  3. In the AWS IoT console, under your group’s configuration page, add the Lambda function you just created to your group.

    1. Choose Lambdas followed by Add your first Lambda:

      
                                    Add your first Lambda highlighted.
    2. Choose Use existing Lambda:

      
                                    Use existing Lambda highlighted.
    3. Choose GG_Car_Aggregator followed by Next:

      
                                    GG_Car_Aggregator and Next highlighted.
    4. Choose the GG_CarAggregator version followed by Finish:

      
                                    Alias: GG_CarAggregator and Finish highlighted.

    The GG_CarAggregator alias should point to the GG_Car_Aggregator Lambda function as indicated by USING ALIAS: GG_CARAGGREGATOR:

    
                            Screenshot highlighting that the GG_Car_Aggregator Lambda
                                function is using the GG_CarAggregator alias.

    Note

    You may remove other Lambda functions from earlier modules.

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

    
                            TBD

    Under Lambda lifecycle, select Make this function long-lived and keep it running indefinitely, then choose Update:

    
                            Webpage showing GG_Car_Aggregator and the Make this function
                                long-lived and keep it running indefinitely radio button
                                selected.