Configure the Lambda function for AWS IoT Greengrass - AWS IoT Greengrass

You are viewing the documentation for AWS IoT Greengrass Version 1. AWS IoT Greengrass Version 2 is the latest major version of AWS IoT Greengrass. For more information about using AWS IoT Greengrass V2, see the AWS IoT Greengrass Version 2 Developer Guide.

Configure the Lambda function for AWS IoT Greengrass

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

In this step, you:

  • Use the AWS IoT console to add the Lambda function to your Greengrass group.

  • Configure group-specific settings for the Lambda function.

  • Add a subscription to the group that allows the Lambda function to publish MQTT messages to AWS IoT.

  • Configure local log settings for the group.

 

  1. In the AWS IoT console, in the navigation pane, choose Greengrass, Classic (V1), Groups.

  2. Under Greengrass groups, choose the group that you created in Module 2.

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

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

    
                            Screenshot of Add a Lambda to your Greengrass Group with the Use
                                existing Lambda button highlighted.
  5. 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.
  6. 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.

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

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

    • Set Timeout to 25 seconds. This Lambda function sleeps for 5 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 (or pinned) Lambda function starts automatically after AWS IoT Greengrass starts and keeps running in its own container. This is in contrast to an on-demand Lambda function, which starts when invoked and stops when there are no tasks left to run. For more information, see Lifecycle configuration for Greengrass Lambda functions.

  9. 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 function to send MQTT messages to AWS IoT Core.

    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 Core.

    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.

    Note

    Currently, the subscription topic filter does not allow more than a single + character in a topic. The topic filter only allows a single # character at the end of a topic.

    The Greengrass_HelloWorld Lambda function sends messages only to the hello/world topic in AWS IoT Core, so you only need to create one subscription from the Lambda function to AWS IoT Core. You create this in the next step.

  10. 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.

    For an example that shows you how to create a subscription using the AWS CLI, see create-subscription-definition in the AWS CLI Command Reference.

  11. 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.
  12. 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.
  13. For Topic filter, enter hello/world, and then choose Next.

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

  15. Configure the group's logging settings. For this tutorial, you configure AWS IoT Greengrass system components and user-defined Lambda functions to write logs to the file system of the core device.

    1. On the group configuration page, choose Settings.

    2. For Local logs configuration, choose Edit.

    3. On the Configure Group logging page, choose Add another log type.

    4. For event source, choose User Lambdas and Greengrass system, and then choose Update.

    5. Keep the default values for logging level and disk space limit, and then choose Save.

    You can use logs to troubleshoot any issues you might encounter when running this tutorial. When troubleshooting issues, you can temporarily change the logging level to Debug. For more information, see Accessing file system logs.

  16. If the Java 8 runtime isn't installed on your core device, you must install it or disable stream manager.

    Note

    This tutorial doesn't use stream manager, but it does use the Default Group creation workflow that enables stream manager by default. If stream manager is enabled but Java 8 isn't installed, the group deployment fails. For more information, see the stream manager requirements.

    To disable stream manager:

    1. On the group settings page, under Stream manager, choose Edit.

    2. Choose Disable, and then choose Save.