Tuesday, September 12, 2017

Learning AWS Lambda, API Gateway, and DynamoDB

The first step I'm taking in my IoT journey is to learn about Amazon Web Services (AWS). Many IoT products utilize (or can utilize) this service, which is why it seems like a good starting place. Cloud computing is essential to IoT, since very little processing is done at the local level (hence: "internet").

If you aren't aware, AWS is a serverless platform that allows developers to run code, build databases, and much more all in the cloud. There are several advantages to using AWS:


  • frees developers and businesses up from having to administer to their own servers, since Amazon takes care of all server maintenance
  • virtually unlimited scaling, since AWS will dedicate more or fewer servers as needed for your data
  • only pay for the time when your code is called (in the case of AWS Lambda) -- not idle time

To get started, I followed these tutorials. They walk you through how to set up an AWS account and the AWS CLI (Command Line Interface), as well as how to use the Lambda, API Gateway, and DynamoDB services. All of these are basic services that will be essential in many IoT projects

After completing the first step in the tutorial (I already had an AWS account), I have the AWS CLI installed, which means I can use the Windows Command Prompt to efficiently perform AWS-related tasks. So far I've used it to configure an IAM (Identity and Access Management) role, configure my root account, and list all of the Lambda functions in my account.

After completing the second step in the tutorial, creating a hello-world style Lambda function, I now understand the basics of working in the Lambda console. I can save and test function code, and modify and utilize key-value pairs as function input. I can also analyze the output of running a function.

While completing the third step in the tutorial, creating a microservice using Lambda and API Gateway, I came across a couple of issues. First of all, I was confused about the part that says "create a DynamoDB table with streams enabled". While setting up the DynamoDB table, there didn't seem to be an option to enable streams. I simply saw this screen:
There's nothing about "streams" on there. Wanting to get my table set up correctly, I scoured the internet for a solution, to no avail. I finally decided to go ahead and click "Create", although I had no idea if my database would be set up properly. It was then that I realized that streams can only be enabled after creating the database. In the screen below, you have to click "Manage Streams"...


And then choose an option. Any option will enable streams, but beyond that more research may be required to determine which option will be best for your purposes. I left it at the default option.







Other than that, I didn't have any trouble following AWS's tutorial. However, the end was quite anti-climactic. It shows you how to send an HTTP GET request to your function by pasting the request in the test event configuration. The result was basically nothing, since I didn't have anything in my DynamoDB Table.




So, I decided to do some research to see how to insert new rows via HTTP requests. (You can also insert new rows through the user interface, but I thought doing so programatically may be more useful for IoT projects.) It appears this is the request syntax for updating a DynamoDB table. After attempting to use it, I've decided to make a subsequent post dedicated to that topic solely. 


Another thing that didn't click for me at first was the role API Gateway played in the architecture. You can see the connection by going to the API Gatway console, selecting the ANY function, and clicking Test. 

Then, choose the GET method (or whichever method you prefer; results may vary). In Query Strings, you can set any key-value pairs that your function uses. For example, I typed TableName = MyTable. Select "Test", and your API will call your Lambda function and show you the same output you would see testing it in the Lambda console. 









Here is a video demonstrating my results: 


Overall, I think this was a very successful first step for me into the world of IoT. I'll be able to use these basic AWS technologies in countless projects as I learn about the new tools we developers have at our disposal today. 

No comments:

Post a Comment