-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathHelloWorldPythonWithCloudFormation.py
71 lines (55 loc) · 2.43 KB
/
HelloWorldPythonWithCloudFormation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#
# Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# HelloWorldPython.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 json
import logging
import os
import platform
from threading import Timer
import greengrasssdk
# Creating a greengrass core sdk client
client = greengrasssdk.client('iot-data')
# Retrieving platform information to send from Greengrass Core
my_platform = platform.platform()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
# 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.
GROUP_ID = os.environ['GROUP_ID']
THING_NAME = os.environ['AWS_IOT_THING_NAME']
THING_ARN = os.environ['AWS_IOT_THING_ARN']
payload = {}
payload['group_id'] = GROUP_ID
payload['thing_name'] = THING_NAME
payload['thing_arn'] = THING_ARN
def greengrass_hello_world_run():
if not my_platform:
payload['message'] = 'Hello world! Sent from Greengrass Core from Python'
else:
payload['message'] = 'Hello world! Sent from Greengrass Core from Python running on platform {}'.format(
my_platform)
client.publish(topic=THING_NAME + '/python/hello/world',
payload=json.dumps(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