Create and configure the Lambda function - Amazon IoT Greengrass
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon IoT Greengrass Version 1 entered the extended life phase on June 30, 2023. For more information, see the Amazon IoT Greengrass V1 maintenance policy. After this date, Amazon IoT Greengrass V1 won't release updates that provide features, enhancements, bug fixes, or security patches. Devices that run on Amazon IoT Greengrass V1 won't be disrupted and will continue to operate and to connect to the cloud. We strongly recommend that you migrate to Amazon 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 Amazon IoT Greengrass Core SDK repository for convenience, but it doesn't use the Amazon 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 Amazon Web Services Region that's currently selected in the Amazon IoT console. For the list of supported Amazon Web Services Regions, see Amazon 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 Amazon SDK for Python (Boto3) package and its dependencies in the car_aggregator folder. Greengrass Lambda functions use the Amazon SDK to access other Amazon 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 Amazon 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 Amazon IoT console, add the Lambda function that you just created to your Amazon 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.