Using the AWS IoT connector to receive messages from the devices through the AWS IoT Platform, requires the setup of an additional service to invoke a REST endpoint in an OutSystems application.
In the sample app that goes along with the connector, there's a REST endpoint to receive the messages. To receive the messages via REST API, one can deploy a lambda function that is invoked by the AWS IoT rule whenever a message arrives from a device in a specific topic, and associate it with the rule in the AWS IoT.
The source code of the Lambda function is available below.
from __future__ import print_function import json import boto3 import requests import os import traceback from base64 import b64decode BASE_URL = os.environ['endpoint'] def lambda_handler(event, context): try: message = event['message'] #message = event #message = json.dumps(event) #message = ("Received event: " + json.dumps(event, indent=2)) #message = event print("Message received: " + message) print("Publishing message to: " + BASE_URL) parameters = { "Message": message, } myResponse = requests.get(BASE_URL, verify=True, params=parameters) except Exception, err: print("Error: "+err)
Hi everyone,
I would like to add some details on top of what Miguel shared, based on an example I did recently.
When creating the lamdba function make sure you choose as type Python3.6 and make also sure that you add to the environment variables a new one called endpoint, like the following screenshot:
In the value of the variable something like:
https://<YOUR.ENVIRONMENT.com>/AwsIotSample/rest/Event/Receive
If you find an error testing the lambda function mentioning that the requests module was not found and cannot be included, make sure you change the lambda function to the following:
from __future__ import print_function from botocore.vendored import requests import json import boto3 import os import traceback from base64 import b64decode BASE_URL = os.environ['endpoint'] def lambda_handler(event, context): try: print("Starting the lambda function!") message = event['message'] #message = event #message = json.dumps(event) #message = ("Received event: " + json.dumps(event, indent=2)) #message = event print("Message received: " + message) print("Publishing message to: " + BASE_URL) parameters = { "Message": message, } myResponse = requests.get(BASE_URL, verify=True, params=parameters) except Exception as err: print("Error: {0}".format(err))