Menu
AWS Greengrass
Developer Guide

Create and Package a Lambda Function

In order for a Python Lambda function to run on an AWS Greengrass device, it must be packaged with specific folders from the Python AWS Greengrass core SDK. In the following, you will:

  • Download the Python AWS Greengrass core SDK to your computer (not the AWS Greengrass device).

  • Decompress the downloaded SDK file (either for Windows or macOS).

  • Obtain the Python Lambda function, called greengrassHelloWorld.py, from the (decompressed) SDK.

  • Package greengrassHelloWorld.py with the SDK folders (three in total) by creating a file called hello_world_python_lambda.zip.

  • Upload the hello_world_python_lambda.zip package to the Amazon Web Services Lambda console.

  • Use the AWS Greengrass console to transfer the package to the AWS Greengrass device.

  1. In the AWS IoT console, choose Software.

    
                            The left navigation pane of the AWS IoT console page with Software
                                highlighted.

    To get the AWS Greengrass Core SDK, on the home page, scroll down to SDKs, and choose Configure download.

    
                            The SDKs section with Configure download highlighted.

    From the drop-down list, choose Python 2.7 version 1.0.0, and then choose Download Greengrass Core SDK.

    
                            Python 2.7 version 1.0.0 and Download AWS Greengrass core SDK.
  2. Based on your operating system, choose a tab to decompress the downloaded SDK.

    WindowsmacOSUNIX-like system
    Windows

    Use a tool for decompressing .tar.gz files on Windows such as 7-Zip, WinZip, or similar. As an example, the 7-Zip tool can be used to decompress greengrass-core-python-sdk-1.0.0.tar.gz as follows:

    1. After installing 7-Zip, navigate to the greengrass-core-python-sdk-1.0.0.tar.gz file using Windows File Explorer (Windows logo key + E), right-click the file, choose 7-Zip, then choose Open archive.

    2. In the resulting 7-Zip window, double-click greegrass-core-python-sdk-1.0.0.tar, aws_greengrass_core_sdk, examples, HelloWorld, and then greengrassHelloWorld.zip.

    3. Optionally using the Ctrl key, select the three SDK folders greengrasssdk, greengrass_common, greengrass_ipc_python_sdk and the Python greengrassHelloWorld.py Lambda file. Next, choose Extract, pick a location to extract the files to, and choose OK.

    macOS
    1. Using Finder, navigated to the greengrass-core-python-sdk-1.0.0.tar.gz file and double-click it. The creates the aws_greengrass_core_sdk folder.

    2. Expand the aws_greengrass_core_sdk folder, tehn the examples folder, and then the HelloWorld folder.

    3. Double click the greengrassHelloWorld.zip file. This creates the greengrassHelloWorld folder – expand this folder.

    4. Note the three SDK subfolders greengrasssdk, greengrass_common, greengrass_ipc_python_sdk and the Python greengrassHelloWorld.py Lambda script file.

    UNIX-like system
    1. Open a Terminal window and navigate to the directory containing the greengrass-core-python-sdk-1.0.0.tar.gz file.

    2. Run the following command to decompress the file:

      sudo tar -xzf greengrass-core-python-sdk-1.0.0.tar.gz

      This creates the aws_greengrass_core_sdk directory. Next, run the following commands:

      cd /aws_greengrass_core_sdk/examples/HelloWorld sudo unzip greengrassHelloWorld.zip

      This creates the three SDK folders greengrass_common, greengrass_ipc_python_sdk, greengrasssdk and the Python greengrassHelloWorld.py Lambda file.

    Note that the greengrassHelloWorld.py Python Lambda function publishes one of two possible messages every 5 seconds to the hello/world topic, as shown next (to save space, all code comments have been removed):

    import greengrasssdk import platform from threading import Timer import time client = greengrasssdk.client('iot-data') my_platform = platform.platform() def greengrass_hello_world_run(): if not my_platform: client.publish(topic='hello/world', payload='Hello world! Sent from Greengrass Core.') else: client.publish(topic='hello/world', payload='Hello world! Sent from Greengrass Core running on platform: {}'.format(my_platform)) Timer(5, greengrass_hello_world_run).start() greengrass_hello_world_run() def function_handler(event, context): return
  3. In order to run the Python greengrassHelloWorld.py Lambda function in the cloud, you must package it with the AWS Greengrass core SDK.. Therefore, after you have extracted the SDK folders greengrass_common, greengrass_ipc_python_sdk, greengrasssdk and the greengrassHelloWorld.py Python Lambda file, package them into a compressed .zip file named hello_world_python_lambda.zip:

    
                            Screenshot showing greengrass_common, greengrass_ipc_python_sdk,
                                greengrasssdk, and greengrassHelloWorld.py getting compressed into
                                the file hello_world_python_lambda.zip.

    For UNIX-like systems (including the Mac terminal), this can be accomplished with the following command:

    sudo zip -r hello_world_python_lambda.zip greengrass_common/ greengrass_ipc_python_sdk/ greengrasssdk/ greengrassHelloWorld.py

    Note

    Depending on your distribution, you may need to install zip first. For example, sudo apt-get install zip (this installation command may differ for your distribution).

  4. You are now ready to upload your Lambda function .zip file to the AWS Lambda console:

    1. From the AWS Management Console, open the Lambda console.

    2. Choose Create a function.

    3. Choose Author from scratch (this option may alread by selected).

    4. Name your function Greengrass_HelloWorld and set the remaining fields as follows. You can create a security role for your Lambda function or use an existing one. If you create one, use the AWS IoT Button permissions role (as shown below). Next, choose Create function.

      
                                    The "Author from scratch" region with the "Name" field
                                        set to "Greengrass_HelloWorld", the "Runtime" field set to
                                        "Python 2.7", the "Role" field set to "Create new role from
                                        templates(s)", the "Role name" field set to
                                        "DefaultLambdaRole", and "Policy templates" set to "AWS IoT
                                        button permissions".
    5. On the Configuration tab, in the Function code region, under the Code entry type drop-down menu, choose Upload a .ZIP file. For Runtime, choose Python 2.7. For Handler, type greengrassHelloWorld.function_handler, and then upload the hello_world_python_lambda.zip file you created earlier, as shown:

      
                                    Screenshot of the Configuration tab with Upload a .ZIP
                                        file, Python 2.7, greengrassHelloWorld.function_handler, and
                                        Upload highlighted.

      Your hello_world_python_lambda.zip file size may vary (i.e., 15.7 kB in this example).

    6. Choose Save.

      
                                    Screenshot with the Save button highlighted.
    7. To publish this Lambda function, under Actions, choose Publish new version:

      
                                    Screenshot of the Actions menu with Publish new version
                                        highlighted.

    Write a description in the Version description field, such as First version, then select Publish:

    
                            Screenshot with the Version description field set to First
                                version and the Publish button highlighted.

    Note

    The above screenshot is a pop-up modal dialog box whose title is Publish new version from $LATEST.

    Now, create an alias for the Lambda function. Aliases create a single entity for your Lambda function that AWS Greengrass devices can subscribe to without having to update subscriptions with Lambda version numbers every time a function is modified. From the Actions drop-down menu, choose Create alias.

    
                            Screenshot of the Actions menu set to Create alias.

    Note

    If future versions of this Lambda are published, you must point the alias to the new version.

    Name the alias GG_HelloWorld, set the version to 1 ( 1 corresponds to the latest published version), and then choose Create. Note that AWS Greengrass does not support Lambda aliases for $LATEST.

    
                            Screenshot of Create a new alias with the Name field set to
                                GG_HelloWorld, the Version field set to 1, and the Create button
                                highlighted.