[AWS IoT Connector] How to configure the AWS IoT Connector to receive messages from devices

[AWS IoT Connector] How to configure the AWS IoT Connector to receive messages from devices

  
Forge Component
(5)
Published on 7 Jul by Miguel João
5 votes
Published on 7 Jul by Miguel João

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)