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.

Amazon 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 Amazon IoT Greengrass V1 maintenance policy. 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 carAggregator.py file to the car_aggregator folder. This is your Lambda function code.

    Note

    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 carAggregator.py 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
                                carAggregator.py.
  5. Compress the contents of the car_aggregator folder into a .zip file named car_aggregator.zip. (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 car_aggregator.zip 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.

    Note

    You can remove other Lambda functions from earlier modules.