Tutorial: Use AWS Lambda with MQTT - Amazon Location Service

Tutorial: Use AWS Lambda with MQTT

In order to create a connection between AWS IoT and Amazon Location, you need a Lambda function to process messages forwarded by EventBridge CloudWatch events. This function will extract any positional data, format it for Amazon Location, and submit it through the Amazon Location Tracker API.

The following procedure shows you how to create this function through the Lambda console:

  1. Open the console.

  2. From the left navigation, choose Functions.

  3. Then choose Create Function, and make sure that the Author from scratch option is selected.

  4. provide a Function name, and for the Runtime option, choose Node.js 16.x.

  5. Choose Create function.

  6. Open the Code tab to access the editor.

  7. Overwrite the placeholder code in the index.js file with the following:

    const AWS = require('aws-sdk') const iot = new AWS.Iot(); exports.handler =  function(event) {   console.log("event===>>>", JSON.stringify(event));   var param = {     endpointType: "iot:Data-ATS"   };   iot.describeEndpoint(param, function(err, data) {     if (err) {       console.log("error===>>>", err, err.stack); // an error occurred     } else {       var endp = data['endpointAddress'];       const iotdata = new AWS.IotData({endpoint: endp});           const trackerEvent = event["detail"]["EventType"];       const src = event["source"];       const time = event["time"];       const gfId = event["detail"]["GeofenceId"];       const resources = event["resources"][0];         const splitResources = resources.split(".");         const geofenceCollection = splitResources[splitResources.length - 1];       const coordinates = event["detail"]["Position"];                                     const deviceId = event["detail"]["DeviceId"];       console.log("deviceId===>>>", deviceId);       const msg =  {           "trackerEventType" : trackerEvent,           "source" : src,           "eventTime" : time,           "geofenceId" : gfId,           "coordinates": coordinates,           "geofenceCollection": geofenceCollection         };       const params = {         topic: `${deviceId}/tracker`,         payload: JSON.stringify(msg),         qos: 0       };       iotdata.publish(params, function(err, data) {           if (err) {             console.log("error===>>>", err, err.stack); // an error occurred           } else {             console.log("Ladmbda triggered===>>>", trackerEvent);  // successful response           }       });     }   }); }
  8. Press the Deploy to save the updated function.

  9. Next open the Configuration tab.

  10. In the Triggers section, press the Add Trigger button.

  11. Select EventBridge (CloudWatch Events) in Source field.

  12. Select the Existing Rules option.

  13. Enter the rule name, for example AmazonLocationMonitor-GEOFENCECOLLECTION_NAME.

  14. Press the Add button.

  15. This will also attach Resource-based policy statements in the permissions tab

Now you will set up the AWS IoT MQTT Test Client, use the following procedure:

  1. Open the https://console.aws.amazon.com/iot/.

  2. In the left navigation pane, select the MQTT test client.

  3. You'll see a section titled MQTT test client where you can configure your MQTT connection.

  4. After configuring the necessary settings, click on the Connect button to establish a connection to the MQTT broker using the provided parameters.

  5. Record endpoint, as it is used later in the tutoiral.

    Once connected to the test client, you can subscribe to MQTT topics or publish messages to topics using the respective input fields provided in the MQTT test client interface. Next you will create an AWS IoT policy.

  6. On the left side menu, under Manage expand Security option and click on Policies.

  7. Click on Create Policy button.

  8. Enter a policy name.

  9. On Policy Document select JSON tab.

  10. Copy paste the policy shown below, but make sure to update all elements with your REGION and ACCOUNT_ID:

    { "Version": "2012-10-17", "Statement": [ { "Action": [ "iot:Connect", "iot:Publish", "iot:Subscribe", "iot:Receive" ], "Resource": [ "arn:aws:iot:REGION:ACCOUNT_ID:client/${cognito-identity.amazonaws.com:sub}", "arn:aws:iot:REGION:ACCOUNT_ID:topic/${cognito-identity.amazonaws.com:sub}", "arn:aws:iot:REGION:ACCOUNT_ID:topicfilter/${cognito-identity.amazonaws.com:sub}/*", "arn:aws:iot:REGION:ACCOUNT_ID:topic/${cognito-identity.amazonaws.com:sub}/tracker" ], "Effect": "Allow" } ] }
  11. Select the Create button to finish.