How to create an API for an IoT Application with AWS API Gateway

TABLE OF CONTENT
1. Overview2. Inject data into DynamoDB Tables To access DynamoDB4, create a Role for Lambda Create a Lambda function that will handle the AP5. Create API Gateway6. Test7. Conclusion8. CloudThat 9. FAQs
Overview
Data from IoT applications is stored in databases that can be accessed via APIs. This blog will discuss the methods of an API Gateway to create an API to retrieve data stored in DynamoDB databases tables. These APIs can also be used in mobile and web apps.

My previous blog, Step by step guide to store IoT data in S3 bucket, provides insight into cost reduction using AWS S3 bucket. Learn how to store IoT data using DynamoDB IoT rules.
We will create a DynamoDB Table and manually insert data into it. An API will be used to access the data in the next section.
Create DynamoDB tables and insert data:
Enter DynamoDB into the search box to access the DynamoDB Service within the AWS Console. To create a new table, click the Create Table option on DynamoDB’s left side blade.

Enter the Table name IoT_table and the primary key as the partition keys. The name is device_id. Next, choose the Number option. Enter the date in field of Sort key, and then choose the String option. Click the Create Table option at bottom.

It is now active and we can see that the table was created.

To insert the data into the database, use the Actions blade to select Create item on newly created IoT_table

Enter “device_id”: 1, “date”: “07-03-2022”,”temperature”:34,”humidity”:60,”co2”:670 in the Attributes area, then click Create item. You can also create objects with different values in the same manner.

Click on Explore table item to verify the injected data

All created items will be displayed at the bottom of the section on item returns.

To access DynamoDB, create a Role for Lambda
To access the IAM service, enter IAM in the AWS console’s search box. Click IAM Role in the left-hand side blade of IAM service. To create a new role click Create Role on console. Select Lambda from services and 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. It will take a few seconds for a role to be created.
To handle the API, create Lambda function
Type lambda in the AWS search box. Next, select Lambda from The services menu and navigate the lambda console. Click Create function to create a new lambda operation.

Enter Function name as IoT_API_lambda and select Runtime Python 3.9.

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

Copy the following code into the lambda_function.py files of the Code Source section. This code will handle the API request and provide the output.
import boto3import jsonimport loggingfrom decimal import Decimalclass CustomEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, Decimal): return float(obj) return json.JSONEncoder.default(self,obj)logger = logging.getLogger()logger.setLevel(logging.INFO)dynamodbTableName = ‘IoT_table’dynamodb = boto3.resource(‘dynamodb’)table = dynamodb.Table(dynamodbTableName)getMethod = ‘GET’def lambda_handler(event, context): logger.info(event) httpMethod = event[‘httpMethod’] if httpMethod == getMethod: response = getProducts() else: response = buildResponce(404, ‘Not Found from the AWS Lambda sir’) return responsedef getProducts(): try: response = table.scan() result = response[‘Items’] return buildResponce(200, result

Comments are closed