Test long-lived Lambda functions - 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.

Test long-lived Lambda functions

A long-lived Lambda function starts automatically when the Amazon IoT Greengrass core starts and runs in a single container (or sandbox). Any variables and preprocessing logic defined outside of the function handler are retained for every invocation of the function handler. Multiple invocations of the function handler are queued until earlier invocations have been executed.

The greengrassHelloWorldCounter.py code used in this module defines a my_counter variable outside of the function handler.

Note

You can view the code in the Amazon Lambda console or in the Amazon IoT Greengrass Core SDK for Python on GitHub.

In this step, you create subscriptions that allow the Lambda function and Amazon IoT to exchange MQTT messages. Then you deploy the group and test the function.

  1. On the group configuration page, choose Subscriptions, and then choose Add.

  2. Under Source type, choose Lambda function, and then choose Greengrass_HelloWorld_Counter.

  3. Under Target type, choose Service, choose IoT Cloud.

  4. For Topic filter, enter hello/world/counter.

  5. Choose Create subscription.

    This single subscription goes in one direction only: from the Greengrass_HelloWorld_Counter Lambda function to Amazon IoT. To invoke (or trigger) this Lambda function from the cloud, you must create a subscription in the opposite direction.

  6. Follow steps 1 - 5 to add another subscription that uses the following values. This subscription allows the Lambda function to receive messages from Amazon IoT. You use this subscription when you send a message from the Amazon IoT console that invokes the function.

    • For the source, choose Service, and then choose IoT Cloud.

    • For the target, choose Lambda function, and then choose Greengrass_HelloWorld_Counter.

    • For the topic filter, enter hello/world/counter/trigger.

    The /trigger extension is used in this topic filter because you created two subscriptions and don't want them to interfere with each other.

  7. Make sure that the Greengrass daemon is running, as described in Deploy cloud configurations to a core device.

  8. On the group configuration page, choose Deploy.

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

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

  11. Choose Subscribe.

    Unlike Part 1 of this module, you shouldn't see any messages after you subscribe to hello/world/counter. This is because the greengrassHelloWorldCounter.py code that publishes to the hello/world/counter topic is inside the function handler, which runs only when the function is invoked.

    In this module, you configured the Greengrass_HelloWorld_Counter Lambda function to be invoked when it receives an MQTT message on the hello/world/counter/trigger topic.

    The Greengrass_HelloWorld_Counter to IoT Cloud subscription allows the function to send messages to Amazon IoT on the hello/world/counter topic. The IoT Cloud to Greengrass_HelloWorld_Counter subscription allows Amazon IoT to send messages to the function on the hello/world/counter/trigger topic.

  12. To test the long-lived lifecycle, invoke the Lambda function by publishing a message to the hello/world/counter/trigger topic. You can use the default message.

    
                            Default Hello from Amazon IoT console message sent to
                                hello/world/counter/trigger with the Publish to topic button
                                highlighted.
    Note

    The Greengrass_HelloWorld_Counter function ignores the content of received messages. It just runs the code in function_handler, which sends a message to the hello/world/counter topic. You can review this code from the Amazon IoT Greengrass Core SDK for Python on GitHub.

Every time a message is published to the hello/world/counter/trigger topic, the my_counter variable is incremented. This invocation count is shown in the messages sent from the Lambda function. Because the function handler includes a 20-second sleep cycle (time.sleep(20)), repeatedly triggering the handler queues up responses from the Amazon IoT Greengrass core.


                    Screenshot showing the incrementing of Invocation Count from 1, 2, and
                        3.