Test on-demand Lambda functions - AWS IoT Greengrass

AWS IoT Greengrass Version 1 entered the extended life phase on June 30, 2023. For more information, see the AWS IoT Greengrass V1 maintenance policy. After this date, AWS IoT Greengrass V1 won't release updates that provide features, enhancements, bug fixes, or security patches. Devices that run on AWS 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 AWS IoT Greengrass Version 2, which adds significant new features and support for additional platforms.

Test on-demand Lambda functions

An on-demand Lambda function is similar in functionality to a cloud-based AWS Lambda function. Multiple invocations of an on-demand Lambda function can run in parallel. An invocation of the Lambda function creates a separate container to process invocations or reuses an existing container, if resources permit. Any variables or preprocessing that are defined outside of the function handler are not retained when containers are created.

  1. On the group configuration page, choose the Lambda functions tab.

  2. Under My Lambda functions, choose the Greengrass_HelloWorld_Counter Lambda function.

  3. On the Greengrass_HelloWorld_Counter details page, choose Edit.

  4. For Pinned, choose False, and then choose Save.

  5. On the group configuration page, choose Deploy.

  6. After your deployment is complete, return to the AWS IoT console home page and choose Test.

  7. Configure the following fields:

    • For Subscription topic, enter hello/world/counter.

    • For Quality of Service, choose 0.

    • For MQTT payload display, choose Display payloads as strings.

    Screenshot of Subscriptions test page.
  8. Choose Subscribe.

    Note

    You should not see any messages after you subscribe.

  9. To test the on-demand lifecycle, invoke the function by publishing a message to the hello/world/counter/trigger topic. You can use the default message.

    1. Choose Publish three times quickly, within five seconds of each press of the button.

      Screenshot showing the Publish to topic button, which must be clicked rapidly three times.

      Each publish invokes the function handler and creates a container for each invocation. The invocation count is not incremented for the three times you triggered the function because each on-demand Lambda function has its own container/sandbox.

      Screenshot showing Invocation Count fixed at 1.
    2. After approximately 30 seconds, choose Publish to topic. The invocation count should be incremented to 2. This shows that a container created from an earlier invocation is being reused, and that preprocessing variables outside of the function handler were stored.

      Screenshot showing Invocation Count now at 2.

You should now understand the two types of Lambda functions that can run on the AWS IoT Greengrass core. The next module, Module 4, shows you how local IoT devices can interact in an AWS IoT Greengrass group.