AWS, Cloud Computing

6 Mins Read

Best ways to Create an API for an IoT Application using AWS API Gateway

TABLE OF CONTENT

1. Overview
2. Create DynamoDB Table and inject data
3. Create a Role for Lambda to access DynamoDB
4. Create Lambda function to handle the AP
5. Create API Gateway
6. Test
7. Conclusion
8. About CloudThat
9. FAQs

 

Overview

Most data from IoT applications are stored in databases for convenient access via APIs. This blog talks about the methods for an API Gateway to construct an API to retrieve data from DynamoDB database tables. These APIs are also utilized in mobile and web applications.

AWS API Gateway

In my previous blog, Step-by-Step Guide to Store IoT data into S3 Bucket, we get an insight on cost reduction by using AWS S3 bucket. You will be able to learn techniques to store IoT data in DynamoDB using IoT rules.

We will create a DynamoDB table and manually inject data into it and an API to access the data in the next segment.

Create DynamoDB Table and inject data:

Go to the DynamoDB Service in the AWS Console by typing DynamoDB in the search box. Click the Tables option on DynamoDB’s left side blade, then the Create Table option to create a new table.

AWS API Gateway

Enter the Table name as IoT_table, the primary key as the partition key, the name as device_id, and choose the Number option next to the field. Enter the date in the field of the Sort key and pick the String option next to it. Finally, click create table option at the bottom.

AWS API Gateway

We can see that the table has been created and is Active.

AWS API Gateway

To inject the data into the database, go to the Actions blade and select Create item on the newly formed IoT_table.

AWS API Gateway

Enter {“device_id”: 1, “date”: “07-03-2022″,”temperature”:34,”humidity”:60,”co2″:670} in the Attributes area, then click Create item. Create objects with varying values in the same way.

AWS API Gateway

To verify the injected data, click on Explore table item

AWS API Gateway

At the bottom of the item returned section, all created items will be displayed.

AWS API Gateway

Create a Role for Lambda to access DynamoDB:

Enter IAM in the search box of the AWS console to access the IAM service. Click IAM Role on the left side blade of the IAM service, to create a new Role click Create Role on the console and select Lambda from the services then click Next. Look for DynamoDB in the permission policy area and select AmazonDynamoDBReadOnlyAcces this policy grants lambda to access the DynamoDB table’s data, then click Next and enter the Role name as dynamodb-and-lambda-role finally click Create role. As a matter of time, a role will be created.

Create Lambda function to handle the API:

Type lambda into the AWS search box, then select Lambda from the services menu and navigate to the lambda console. To create a new lambda function, Click Create function.

AWS API Gateway

Enter Function name as IoT_API_lambda, select Runtime as Python 3.9.

AWS API Gateway

In the Execution role section, select the Use an existing role option, dynamodb-and-lambda-role, and finally click Create function

AWS API Gateway

Paste the following code in the lambda_function.py files in the Code Source section. This code will handle the incoming API and will provide the output.

Create API Gateway:

Go to the AWS API Gateway console by searching API Gateway in the AWS console’s search box. Click the Create API option to build a new API Gateway.

AWS API Gateway

Select the REST API option and click Build.

AWS API Gateway

Choose REST as the protocol and the New API option in the Create New API section, then enter IoT_api as the API name. Create an API by clicking create API the button.

AWS API Gateway

After creating an API, you will need to create a method. To do so, go to Actions and then to the Create Method option on the Actions blade.

AWS API Gateway

We need the GET method, so choose that and click the true option next.

AWS API Gateway

Select Lambda Function as the integration type and select the Use Lambda Proxy integration option. Enter and pick the previously generated lambda function, then save the modifications.

AWS API Gateway

To use this API, we must first create a Resource. To do so, go to the Actions blade, click Create Resource, and then input the Resource Name IOT_API_RESOURCE. After checking the CORS box, select the Create Resource option.

AWS API Gateway

After building the method and resources, we must deploy them. To do so, go to Actions and select Deploy API from the Actions blade.

AWS API Gateway

Select Deployment stage as New Stage. After that, enter the Stage name as IOT_API_stagr, then click the Deploy.

AWS API Gateway

API is deployed and the URL will be displayed. Make note of the Invoked URL which we will use in the testing section.

AWS API Gateway

Test:

Select the GET method in the POSTMAN software, paste the previously copied URL into the params section, and click send. We will soon be able to see DynamoDB data in the Body section and the 200 OK status code.

AWS API Gateway

Conclusion:

Amazon API Gateway allows you to create, deploy, manage, audit, and secure APIs. As a result, the API Gateway facilitates the creation of APIs for IoT applications and other web and mobile applications. API gateway helps to create HTTP, RESTful, and WebSocket APIs. These will be used based on the customer application use cases.

About CloudThat:

CloudThat is the official AWS Advanced Consulting Partner, Microsoft Gold Partner, and Training partner helping people develop knowledge on the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

If you have any queries about Amazon MQ setup with Java Application, Amazon Messaging Broker, or any other AWS services, feel free to drop in a comment and we will get back to you quickly.

FAQs:

  1. What is the benefit of AWS API Gateway?

Ans: API Gateway is a fully managed service by AWS that allows developers to create, deploy, manage, audit, and secure APIs at any scale.

  1. What kind of APIs are supported by the API Gateway?

Ans: API Gateway supports HTTP, REST, and WebSocket APIs.

WRITTEN BY Vasanth Kumar R

Vasanth Kumar R works as a Sr. Research Associate at CloudThat. He is highly focused and passionate about learning new cutting-edge technologies including Cloud Computing, AI/ML & IoT/IIOT. He has experience with AWS and Azure Cloud Services, Embedded Software, and IoT/IIOT Development, and also worked with various sensors and actuators as well as electrical panels for Greenhouse Automation.

SHARE

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!