Menu
AWS Greengrass
Developer Guide

Testing On-Demand Lambda Functions

An on-demand Lambda function is similar in functionality to an AWS cloud Lambda function. Multiple invocations of an on-demand Lambda function can run in parallel. Each invocation of the Lambda function creates a new, separate container to process invocations. The container can be reused for future invocations if resources permit. For information about container reuse, see Understanding Container Reuse in AWS Lambda. Any variables or preprocessing that are defined outside of the function handler are not retained when new containers are created. As a best practice, we recommend that you use on-demand Lambda functions instead of long-lived functions whenever possible because they are less resource-intensive.

  1. On the configuration page for your group, choose Lambdas. For the Greengrass_HelloWorld_Counter Lambda function, choose Edit Configuration.

    
                            Screenshot with Lambdas and Edit Configuration
                                highlighted.
  2. Under Lambda lifecycle, select On-demand function.

    
                            Screenshot with the On-demand function radio button
                                selected.

    Next, choose Update.

  3. Make sure that the AWS Greengrass core daemon is running (i.e., /greengrass/ggc/packages/x.x.x sudo ./greengrassd start). Then, on the group configuration page, under Actions, choose Deploy to deploy your updated configurations to the AWS Greengrass core device.

    
                            Screenshot of Deploy highlighted under the Actions drop-down
                                menu.
  4. After your deployment is complete, in the AWS IoT console, choose Test. For Subscription topic, type hello/world/counter. For Quality of Service, select 0. For MQTT payload display, select Display payloads as strings and then choose Subscribe to topic.

    
                            Screenshot of Subscriptions page.

    Again, you should not be able to see any messages after you subscribe. Trigger the function to the hello/world/counter/trigger topic by sending any message (the default message is fine), then choose Publish to topic three times, 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 is triggering the function handler and creating a new container for each invocation. The invocation count is not incremented for each of 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.

    Wait approximately thirty seconds or more, and then choose Publish to topic. This time you should see an incremented invocation count.

    
                            Screenshot showing Invocation Count now at 2.

    This shows that a container, first created from a prior invocation, is being reused, and preprocessing variables outside of the function handler have been stored.

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