AWS IoT Greengrass
Developer Guide

Configure the Lambda Function for AWS IoT Greengrass

You are now ready to configure your Lambda function for AWS IoT Greengrass.

  1. In the AWS IoT Core console, under Greengrass, choose Groups, and then choose the group that you created in Module 2.

  2. On the group configuration page, choose Lambdas, and then choose Add Lambda.

    
                            The group page with Lambdas and Add Lambda highlighted.
  3. Choose Use existing Lambda.

    
                            Screenshot of Add a Lambda to your Greengrass Group with the Use
                                existing Lambda button highlighted.
  4. Search for the name of the Lambda you created in the previous step (Greengrass_HelloWorld, not the alias name), select it, and then choose Next:

    
                            Screenshot of Use existing Lambda with Greengrass_HelloWorld and
                                the Next button highlighted.
  5. For the version, choose Alias: GG_HelloWorld, and then choose Finish. You should see the Greengrass_HelloWorld Lambda function in your group, using the GG_HelloWorld alias.

  6. Choose the ellipsis (), and then choose Edit Configuration:

    
                            Screenshot of MyFirstGroup with the ellipsis and Edit
                                Configuration highlighted.
  7. On the Group-specific Lambda configuration page, make the following changes:

    • Set Timeout to 25 seconds. This Lambda function sleeps for 20 seconds before each invocation.

    • For Lambda lifecycle, choose Make this function long-lived and keep it running indefinitely.

     

    
                            Screenshot of the configuration page with 25 (seconds) and the
                                Make this function long-lived and keep it running indefinitely radio
                                button selected.

    Note

    A long-lived Lambda function starts automatically after AWS IoT Greengrass starts and keeps running in its own container (or sandbox). This is in contrast to an on-demand Lambda function, which starts when invoked and stops when there are no tasks left to execute. For more information, see Lifecycle Configuration for Greengrass Lambda Functions.

  8. Keep the default values for all other fields, such as Run as, Containerization, and Input payload data type, and choose Update to save your changes. For information about Lambda function properties, see Controlling Execution of Greengrass Lambda Functions by Using Group-Specific Configuration.

    Next, create a subscription that allows the Lambda to send MQTT messages to AWS IoT.

    A Greengrass Lambda function can exchange MQTT messages with:

    The group uses subscriptions to control how these entities can communicate with each other. Subscriptions provide predictable interactions and a layer of security.

    A subscription consists of a source, target, and topic. The source is the originator of the message. The target is the destination of the message. The topic allows you to filter the data that is sent from the source to the target. The source or target can be a Greengrass device, Lambda function, connector, device shadow, or AWS IoT.

    Note

    A subscription is directed in the sense that messages flow in a specific direction: from the source to the target. To allow two-way communication, you must set up two subscriptions.

    The Greengrass_HelloWorld Lambda function sends messages only to the hello/world topic in AWS IoT, as shown in the following greengrassHelloWorld.py code snippet:

    def greengrass_hello_world_run(): if not my_platform: client.publish(topic='hello/world', payload='Hello world! Sent from Greengrass Core.') else: client.publish(topic='hello/world', payload='Hello world! Sent from Greengrass Core running on platform: {}'.format(my_platform)) Timer(5, greengrass_hello_world_run).start()

    Because the Greengrass_HelloWorld Lambda function sends messages only to the hello/world topic in AWS IoT, you only need to create one subscription from the Lambda function to AWS IoT, as shown next.

  9. On the group configuration page, choose Subscriptions, and then choose Add your first Subscription.

    
                            Screenshot of the Group configuration page, with Subscription
                                and Add your first Subscription highlighted.
  10. In Select a source, choose Select. Then, on the Lambdas tab, choose Greengrass_HelloWorld as the source.

    
                            Screenshot of the Select a source page with Lambdas and
                                Greengrass_HelloWorld highlighted.
  11. For Select a target, choose Select. Then, on the Service tab, choose IoT Cloud, and then choose Next.

    
                            The Select a target with the Services tab, IoT Cloud and the
                                Next button highlighted.
  12. For Topic filter, enter hello/world, and then choose Next.

    
                            Screenshot with hello/world highlighted under Topic
                                filter.
  13. Choose Finish.