Created
April 16, 2019 14:10
-
-
Save dave-malone/74acd58f0252556da7fd58b48784f078 to your computer and use it in GitHub Desktop.
Example AWS IoT Greengrass Lambda invoking another Lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
# | |
# greengrassHelloWorld.py | |
# Demonstrates a simple publish to a topic using Greengrass core sdk | |
# This lambda function will retrieve underlying platform information and send | |
# a hello world message along with the platform information to the topic 'hello/world' | |
# The function will sleep for five seconds, then repeat. Since the function is | |
# long-lived it will run forever when deployed to a Greengrass core. The handler | |
# will NOT be invoked in our example since the we are executing an infinite loop. | |
import greengrasssdk | |
import platform | |
from threading import Timer | |
import time | |
import json | |
# Invoke | |
# Creating a greengrass core sdk client | |
client = greengrasssdk.client('lambda') | |
# Retrieving platform information to send from Greengrass Core | |
my_platform = platform.platform() | |
# When deployed to a Greengrass core, this code will be executed immediately | |
# as a long-lived lambda function. The code will enter the infinite while loop | |
# below. | |
# If you execute a 'test' on the Lambda Console, this test will fail by hitting the | |
# execution timeout of three seconds. This is expected as this function never returns | |
# a result. | |
def greengrass_hello_world_run(): | |
payload = json.dumps({ | |
'message':"Hello world! Sent from Greengrass Core." | |
}) | |
response = client.invoke( | |
FunctionName='arn:aws:lambda:us-east-1:068311527115:function:Greengrass_HelloWorld_Counter', | |
Payload=payload | |
) | |
# Asynchronously schedule this function to be run again in 5 seconds | |
Timer(5, greengrass_hello_world_run).start() | |
# Start executing the function above | |
greengrass_hello_world_run() | |
# This is a dummy handler and will not be invoked | |
# Instead the code above will be executed in an infinite loop for our example | |
def function_handler(event, context): | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment