Configure the Lambda function for Amazon IoT Greengrass - 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.

Configure the Lambda function for Amazon IoT Greengrass

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

In this step, you:

  • Use the Amazon 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 Amazon IoT.

  • Configure local log settings for the group.

 

  1. In the Amazon IoT console navigation pane, under Manage, expand Greengrass devices, and then choose Groups (V1).

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

  3. On the group configuration page, choose the Lambda functions tab, and then scroll down to the My Lambda functions section and choose Add Lambda function.

  4. Select the name of the Lambda function you created in the previous step (Greengrass_HelloWorld, not the alias name).

  5. For the version, choose Alias: GG_HelloWorld.

  6. In the Lambda function configuration section, make the following changes:

    • Set the System user and group to Use group default.

    • Set the Lambda function containerization to Use group default.

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

    • For Pinned, choose True.

     

    Note

    A long-lived (or pinned) Lambda function starts automatically after Amazon 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.

  7. Choose Add Lambda function 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 Amazon 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 Amazon 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 Amazon IoT Core, so you only need to create one subscription from the Lambda function to Amazon IoT Core. You create this in the next step.

  8. On the group configuration page, choose the Subscriptions tab, and then choose Add subscription.

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

  9. In the Source type, choose Lambda function and, for the Source, choose Greengrass_HelloWorld.

  10. For the Target type, choose Service and, for the Target select IoT Cloud.

  11. For Topic filter, enter hello/world, and then choose Create subscription.

  12. Configure the group's logging settings. For this tutorial, you configure Amazon 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 the Logs tab.

    2. In the Local logs configuration section, choose Edit.

    3. On the Edit local logs configuration dialog box, keep the default values for both log levels and storage sizes, 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.

  13. 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, choose the Lambda functions tab.

    2. Under the System Lambda functions section, select Stream manager and choose Edit.

    3. Choose Disable, and then choose Save.