Disruptive Technologies Disruptive Developers

Integrating with Amazon AWS

This article will show you how to use a Data Connector to integrate with Amazon AWS by forwarding events to a Lambda Function.


Before you begin

  1. You need to have at least Project Administrator level access to a project in Studio.

  2. You need an account on Amazon AWS, which is available with a free tier.


Create a Lambda function

Create a Lambda function:

  1. Navigate to the Amazon AWS Console.

  2. Go to the Services menu and then select Lambda.

  3. Press Create function.

  4. Select Author from scratch and fill in:

    Name: disruptive

    Runtime: Node.js 8.10

    Role name: disruptiveLambdaRole

    Leave the rest as is.

  5. Click Create function.

Edit Lambda function code

Edit the code to log the incoming request body:

  1. Scroll down to the Function code.

  2. Change the code in the open index.js to be:

     exports.handler = async (event) => {
     console.log(event.body);
        
     /* Add further integration code here */
        
     return { statusCode: 200 };
     };
    
  3. Press Save in the top right corner.

Connect Lambda function to API Gateway

Add a API Gateway “trigger” to execute the Lambda function using a public HTTP request:

  1. Add a trigger by selecting the API Gateway underneath Add triggers on the left.

  2. Scroll down to Configure triggers and fill in:

    API: Create a new API

    Security: Open

  3. Press Add.

  4. Press Save at the top right.

  5. Scroll down to the API Gateway configuration.

  6. Expand Details and save the API endpoint URL for later.


Create a Data Connector

Create a Data Connector to send sensor events to the Lambda function:

  1. Go to Studio.

  2. Navigate to the project you want to integrate with.

  3. Open the Data Connector view for the project via the menu on the left.

  4. Click Create new.

  5. Set the Name to “Amazon AWS”.

  6. Take the URL for the Lambda function, saved previously, and enter this into Endpoint URL.

  7. Set the Data Connector to only send the Touch, Temperature and ObjectPresent events.

  8. Leave everything by default and click SAVE NEW DATA CONNECTOR.

N.B. Verify Secret: For production code it is recommended to use the Data Connector “secret” to sign each request and to verify the origin in the receiving code. See, Verify Secret.


Test the integration

To confirm that integration is up and running, we will both look at the Data Connector metrics in Studio and the Lambda function’s log.

Before proceeding, make sure your sensors has sent some data by pressing them a few times so that the Data Connector and the Lambda function has run at least once.

Data Connector metrics in Studio

Navigate back to the Data Connector that you created previously in Studio.

At the top of the page, you will find how many times your new Data Connector has been run.

The Success count shows the number of times the Lambda function has been called and returned HTTP status 200 OK the last 24h.

The Error count shows how many times it has returned an error code or timed out.

Lambda function log in Amazon AWS

Navigate back to your Lambda function in the Amazon AWS console and then:

  1. Go to the Monitor tab and then press View logs in CloudWatch.

  2. Next, open up the latest log stream, pressing the refresh button if none are yet shown.

  3. Expanding a log entry between a “START” and a “END” log entry will show the sensor even printed to the log as it was received by the Lambda function.


Success!

This simple integration is easy to set up and maintain, as Amazon AWS takes care of all the details surrounding how the function is run. It also scales incredibly well, as a new Lambda function will be run for each event sent by the Data Connector.


Next steps

The next steps is to replace the Add further integration code here with code to forward the events into any of Amazon’s vast selection of databases, event buses, stream processing or machine-learning tools.

See: