Create and configure the Lambda function - AWS IoT Greengrass

AWS IoT Greengrass Version 1 no longer receives feature updates, and will receive only security patches and bug fixes until June 30, 2023. For more information, see the AWS IoT Greengrass V1 maintenance policy. We strongly recommend that you migrate to AWS IoT Greengrass Version 2, which adds significant new features and support for additional platforms.

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 TrafficLight examples folder on GitHub, download the file to the car_aggregator folder. This is your Lambda function code.


    This example Python file is stored in the AWS IoT Greengrass Core SDK repository for convenience, but it doesn't use the AWS IoT Greengrass Core SDK.

  3. If you aren't working in the US East (N. Virgina) Region, open and change region_name in the following line to the AWS Region that's currently selected in the AWS IoT console. For the list of supported AWS Regions, see AWS IoT Greengrass in the Amazon Web Services General Reference.

    dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
  4. Run the following command in a command-line window to install the AWS SDK for Python (Boto3) package and its dependencies in the car_aggregator folder. Greengrass Lambda functions use the AWS SDK to access other AWS services. (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
  5. 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.

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

    • For Runtime, choose Python 3.7.

    • For Permissions, keep the default setting. This creates an execution role that grants basic Lambda permissions. This role isn't used by AWS IoT Greengrass.

    Choose Create function.

                            Basic information section with Function name set to
                                GG_Car_Aggregator and Runtime set to Python 3.7.
  7. Upload your Lambda function deployment package:

    1. On the Code tab, under Code source, choose Upload from. From the dropdown, choose .zip file.

                The Upload from dropdown with .zip file highlighted.
    2. Choose upload, and then choose your deployment package. Then, choose Save.

    3. On the Code tab for the function, under Runtime settings, choose Edit, and then enter the following values.

      • For Runtime, choose Python 3.7.

      • For Handler, enter carAggregator.function_handler

    4. Choose Save.

  8. 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).

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

    1. On the group configuration page, choose Lambda functions, and then under My Lambda functions, choose Add.

    2. For Lambda function, choose GG_Car_Aggregator.

    3. For Lambda function version, choose the alias to the version that you published.

    4. For Memory limit, enter 64 MB.

    5. For Pinned, choose True.

    6. Choose Add Lambda function.


    You can remove other Lambda functions from earlier modules.