Skip to content

Commit ee6a9d9

Browse files
committed
Copy examples from aws-samples repo into here
1 parent c58db71 commit ee6a9d9

11 files changed

Lines changed: 619 additions & 110 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#
22
# Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
#
4-
import sys
54
import logging
5+
import sys
66

77
# Setup logging to stdout
88
logger = logging.getLogger(__name__)
99
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
1010

1111

1212
def handler(event, context):
13-
logger.info('Invoked with payload ' + str(event))
14-
return 'Invoked successfully'
13+
logger.info("Invoked with payload " + str(event))
14+
return "Invoked successfully"

examples/BinaryLambdaInvoke/invoker.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,33 @@
99
# be able to see 'Invoked successfully' returned by 'invokee' lambda. A lambda
1010
# function can support non-json payload, which is a new feature introduced in
1111
# GGC version 1.5.
12-
#
13-
import sys
12+
1413
import base64
15-
import logging
1614
import json
15+
import logging
16+
import sys
17+
1718
import greengrasssdk
1819

1920
# Setup logging to stdout
2021
logger = logging.getLogger(__name__)
2122
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
2223

23-
client = greengrasssdk.client('lambda')
24+
client = greengrasssdk.client("lambda")
2425

2526

2627
def handler(event, context):
27-
client_context = json.dumps({
28-
'custom': 'custom text'
29-
})
28+
client_context = json.dumps({"custom": "custom text"})
3029

3130
try:
3231
response = client.invoke(
3332
ClientContext=base64.b64encode(bytes(client_context)),
34-
FunctionName='arn:aws:lambda:<region>:<accountId>:function:<targetFunctionName>:<targetFunctionQualifier>',
35-
InvocationType='RequestResponse',
36-
Payload='Non-JSON Data',
37-
Qualifier='1'
33+
FunctionName="arn:aws:lambda:<region>:<accountId>:function:<targetFunctionName>:<targetFunctionQualifier>",
34+
InvocationType="RequestResponse",
35+
Payload="Non-JSON Data",
36+
Qualifier="1",
3837
)
3938

40-
logger.info(response['Payload'].read())
39+
logger.info(response["Payload"].read())
4140
except Exception as e:
4241
logger.error(e)

examples/HelloWorld/greengrassHelloWorld.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
# Greengrass core. The handler will NOT be invoked in our example since
1212
# the we are executing an infinite loop.
1313

14-
import greengrasssdk
1514
import logging
1615
import platform
1716
import sys
1817
from threading import Timer
1918

19+
import greengrasssdk
20+
2021
# Setup logging to stdout
2122
logger = logging.getLogger(__name__)
2223
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
2324

2425
# Creating a greengrass core sdk client
25-
client = greengrasssdk.client('iot-data')
26+
client = greengrasssdk.client("iot-data")
2627

2728
# Retrieving platform information to send from Greengrass Core
2829
my_platform = platform.platform()
@@ -35,22 +36,21 @@
3536
# hitting the execution timeout of three seconds. This is expected as
3637
# this function never returns a result.
3738

39+
3840
def greengrass_hello_world_run():
3941
try:
4042
if not my_platform:
4143
client.publish(
42-
topic='hello/world',
43-
queueFullPolicy='AllOrException',
44-
payload='Hello world! Sent from Greengrass Core.')
44+
topic="hello/world", queueFullPolicy="AllOrException", payload="Hello world! Sent from Greengrass Core."
45+
)
4546
else:
4647
client.publish(
47-
topic='hello/world',
48-
queueFullPolicy='AllOrException',
49-
payload='Hello world! Sent from '
50-
'Greengrass Core running on platform: {}'
51-
.format(my_platform))
48+
topic="hello/world",
49+
queueFullPolicy="AllOrException",
50+
payload="Hello world! Sent from " "Greengrass Core running on platform: {}".format(my_platform),
51+
)
5252
except Exception as e:
53-
logger.error('Failed to publish message: ' + repr(e))
53+
logger.error("Failed to publish message: " + repr(e))
5454

5555
# Asynchronously schedule this function to be run again in 5 seconds
5656
Timer(5, greengrass_hello_world_run).start()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#
2+
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
#
4+
5+
# greengrassHelloWorldCounter.py
6+
# Demonstrates a simple publish to a topic using Greengrass core sdk
7+
# This lambda function will retrieve underlying platform information and send a hello world message along with the
8+
# platform information to the topic 'hello/world/counter' along with a counter to keep track of invocations.
9+
#
10+
# This Lambda function requires the AWS Greengrass SDK to run on Greengrass devices.
11+
# This can be found on the AWS IoT Console.
12+
13+
import json
14+
import logging
15+
import platform
16+
import sys
17+
import time
18+
19+
import greengrasssdk
20+
21+
# Setup logging to stdout
22+
logger = logging.getLogger(__name__)
23+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
24+
25+
# Creating a greengrass core sdk client
26+
client = greengrasssdk.client("iot-data")
27+
28+
# Retrieving platform information to send from Greengrass Core
29+
my_platform = platform.platform()
30+
31+
# Counter to keep track of invocations of the function_handler
32+
my_counter = 0
33+
34+
35+
def function_handler(event, context):
36+
global my_counter
37+
my_counter = my_counter + 1
38+
try:
39+
if not my_platform:
40+
client.publish(
41+
topic="hello/world/counter",
42+
queueFullPolicy="AllOrException",
43+
payload=json.dumps(
44+
{"message": "Hello world! Sent from Greengrass Core. Invocation Count: {}".format(my_counter)}
45+
),
46+
)
47+
else:
48+
client.publish(
49+
topic="hello/world/counter",
50+
queueFullPolicy="AllOrException",
51+
payload=json.dumps(
52+
{
53+
"message": "Hello world! Sent from Greengrass Core running on platform: {}."
54+
+ " Invocation Count: {}".format(my_platform, my_counter)
55+
}
56+
),
57+
)
58+
except Exception as e:
59+
logger.error("Failed to publish message: " + repr(e))
60+
time.sleep(20)
61+
return

examples/Storyline_MessageLambda/messageLambda.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@
22
# Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
#
44

5-
import sys
65
import logging
6+
import sys
7+
78
import greengrasssdk
89

910
# Setup logging to stdout
1011
logger = logging.getLogger(__name__)
1112
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
1213

13-
client = greengrasssdk.client('iot-data')
14+
client = greengrasssdk.client("iot-data")
1415

1516

1617
def message_handler(event, context):
1718
logger.info("Received message!")
18-
if 'state' in event:
19-
if event['state'] == "on":
20-
client.update_thing_shadow(
21-
thingName="RobotArm_Thing",
22-
payload='{"state":{"desired":{"myState":"on"}}}')
23-
logger.info("Triggering publish to shadow "
24-
"topic to set state to ON")
25-
elif event['state'] == "off":
26-
client.update_thing_shadow(
27-
thingName="RobotArm_Thing",
28-
payload='{"state":{"desired":{"myState":"off"}}}')
29-
logger.info("Triggering publish to shadow "
30-
"topic to set state to OFF")
19+
if "state" in event:
20+
if event["state"] == "on":
21+
client.update_thing_shadow(thingName="RobotArm_Thing", payload='{"state":{"desired":{"myState":"on"}}}')
22+
logger.info("Triggering publish to shadow " "topic to set state to ON")
23+
elif event["state"] == "off":
24+
client.update_thing_shadow(thingName="RobotArm_Thing", payload='{"state":{"desired":{"myState":"off"}}}')
25+
logger.info("Triggering publish to shadow " "topic to set state to OFF")

examples/Storyline_UptimeLambda/uptimeLambda.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,32 @@
22
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
#
44

5-
import sys
65
import logging
6+
import sys
7+
78
import greengrasssdk
89

910
# Setup logging to stdout
1011
logger = logging.getLogger(__name__)
1112
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
1213

13-
client = greengrasssdk.client('iot-data')
14+
client = greengrasssdk.client("iot-data")
1415

1516

1617
def uptime_handler(event, context):
1718
logger.info("Received message!")
18-
if 'state' in event:
19-
if event['state'] == "on":
19+
if "state" in event:
20+
if event["state"] == "on":
2021
try:
21-
client.publish(
22-
topic='/topic/metering',
23-
queueFullPolicy='AllOrException',
24-
payload="Robot arm turned ON")
25-
logger.info("Triggering publish to topic "
26-
"/topic/metering with ON state")
22+
client.publish(topic="/topic/metering", queueFullPolicy="AllOrException", payload="Robot arm turned ON")
23+
logger.info("Triggering publish to topic " "/topic/metering with ON state")
2724
except Exception as e:
28-
logger.error("Failed to trigger publish to topic "
29-
"/topic/metering with ON state: " + repr(e))
30-
elif event['state'] == "off":
25+
logger.error("Failed to trigger publish to topic " "/topic/metering with ON state: " + repr(e))
26+
elif event["state"] == "off":
3127
try:
3228
client.publish(
33-
topic='/topic/metering',
34-
queueFullPolicy='AllOrException',
35-
payload="Robot arm turned OFF")
36-
logger.info("Triggering publish to topic "
37-
"/topic/metering with OFF state")
29+
topic="/topic/metering", queueFullPolicy="AllOrException", payload="Robot arm turned OFF"
30+
)
31+
logger.info("Triggering publish to topic " "/topic/metering with OFF state")
3832
except Exception as e:
39-
logger.error("Failed to trigger publish to topic "
40-
"/topic/metering with OFF state: " + repr(e))
33+
logger.error("Failed to trigger publish to topic " "/topic/metering with OFF state: " + repr(e))

examples/StreamManagerKinesis/stream_manager_kinesis.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
StreamManagerClient,
1414
)
1515

16-
1716
# This example will create a Greengrass StreamManager stream called "SomeStream".
1817
# It will then start writing data into that stream and StreamManager will
1918
# automatically export the written data to a Kinesis Data Stream called "MyKinesisStream".

examples/TES/lambda_function.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
# Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
#
44

5-
from botocore.session import Session
5+
import logging
6+
import sys
7+
68
import greengrasssdk
79

8-
import sys
9-
import logging
10+
from botocore.session import Session
1011

1112
# Setup logging to stdout
1213
logger = logging.getLogger(__name__)
1314
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
1415

15-
client = greengrasssdk.client('iot-data')
16+
client = greengrasssdk.client("iot-data")
1617

17-
logger.info('Hello from pinned lambda. Outside of handler.')
18+
logger.info("Hello from pinned lambda. Outside of handler.")
1819

1920
# Get creds from TES
2021
# Note: must make sure that creds are not available within local folder
@@ -24,7 +25,9 @@
2425
formatted_creds = """
2526
Access Key: {}\n
2627
Secret Key: {}\n
27-
Session Key: {}\n""".format(creds.access_key, creds.secret_key, creds.token)
28+
Session Key: {}\n""".format(
29+
creds.access_key, creds.secret_key, creds.token
30+
)
2831

2932
# Logging credential information is not recommended. This is for demonstration purposes only.
3033
# logger.info(formatted_creds)

0 commit comments

Comments
 (0)