Skip to content

Commit

Permalink
Added echo Lambda for python
Browse files Browse the repository at this point in the history
  • Loading branch information
miman committed Jul 1, 2020
1 parent 65fc7e3 commit 90718f7
Showing 2 changed files with 53 additions and 0 deletions.
51 changes: 51 additions & 0 deletions ggs-mqtt-echo-python/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Copyright 2010-2019 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 logging
import platform
import sys
import os
import json
import greengrasssdk

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

# Creating a greengrass core sdk client
client = greengrasssdk.client("iot-data")

device = os.environ['AWS_IOT_THING_NAME']

def post_echo(msg, topic):
text_to_send = {
"receivedMsg": msg,
"topic": topic,
"deviceName": device
}
try:
client.publish(
topic="echo/output",
queueFullPolicy="AllOrException",
payload=json.dumps(text_to_send)
)
except Exception as e:
logger.error("Failed to publish message: " + repr(e))

# This is a Lambda handler that will be called whenever a msg is posted on a topic routed to this Lambda
def lambda_handler(event, context):
print("power-controller> Msg received")
# Get the name of the topic this message was received on
topic = context.client_context.custom["subject"]
post_echo(event, topic)

2 changes: 2 additions & 0 deletions ggs-mqtt-echo-python/zip-ggs-module.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"C:\Program Files\7-Zip\7z.exe" a ggs-mqtt-echo-python.zip lambda_function.py
"C:\Program Files\7-Zip\7z.exe" a ggs-mqtt-echo-python.zip .\greengrasssdk

0 comments on commit 90718f7

Please sign in to comment.