RESTful Microservices with AWS Lambda, API Gateway and DynamoDB.

Sourav Dinda
5 min readMar 27, 2023

--

A tutorial with a complete working cloud-native and serverless CRD application.

Objectives:

I will demonstrate how to use boto3 and python to create a Lambda function in the AWS interface for CRUD (Create, Read, Update, Delete) operations. Then use the API gateway in the AWS interface to create an API. When the API is called, the API Gateway will direct the call to the Lambda function, which will in turn process data in the DynamoDB table. The response ought to subsequently be sent back via the API Gateway.

Setting up the Database

Log in to AWS and navigate to the DynamoDB service. Then click Create table. Choose a primary key (I used id), give your table a name (I called mine Students), keep table settings as “Default Settings” and then click Create. The primary key you choose must uniquely identify each item in the table so that no two objects can share the same key. This is why it is crucial.

Creating the Lambda Role

AWS roles essentially serve as a means of granting internal access between AWS services. We’ll create a role and assign it to our Lambda functions. These functions will be permitted access to our DynamoDB table with the help of this role.

Click Roles on the AWS IAM service. Click Create role. Select Lambda as the service that will use the role. Then click Next: Permissions. We now need to attach a permission policy to that role. Search for AmazonDynamoDBFullAccess, check the checkbox, and do the same for CloudWatchFullAccess and AWSLambda_FullAccess permission policies. Click Next and, on the review page provide a name for your role (I chose lambda-api-role). Finally, click Create role.

Creating Lambda Functions:

Navigate to the Lambda service in AWS and click Create Function. Because we’re creating this function from scratch, click Author from scratch. Choose Python 3.7 as the Runtime, name your function (I went with student-get-data), and utilize the role with permissions we previously generated for the execution role (lambda-api-role in my case). Finally, select Create function. Now Similarly create two more functions for post and delete (I went with student-put-data and student-delete-data).

We’ll replace the lambda code with the following python codes and click Deploy thereafter:

For the student-get-data function-

For the student-put-data function-

For student-delete-data function-

Creating the API

With the Lambda function created, we can begin creating our Serverless API. Navigate to AWS API Gateway and click Get started, select REST API, and click on Build. Name your API (I called mine student-data-api), select Regional, and click Create API.

We’ll go ahead with creating the resource. With the root path highlighted, click the Actions drop-down and then click Create Resource. we will name the resource as items.

We’ll select the items resource and create a GET method for the items path. Enable Use Lambda Proxy integration, select the Lambda function created earlier, and hit Save.

Similarly create felete method for the items path.or Post and Delete

Tadaa!!! It’s time to test our API. For the purpose of not making this blog too long and also not to bore you, I attached a few screenshots of the tests.

Click on the post method and test with the below data.

Click on the get method and test with the below data. Our data is getting back from the Students table.

Finally, Click on the delete method and test with the below data.

Check the Students table , the data has been deleted successfully .

Conclusion

Congratulations on making it this far! You can, among other things, do the following if you want to expand your new API:

· Add request validation

· Use AWS Cognito to require authentication to access your APIs

· Deploy the API to your own custom domain

· Use API keys to limit the API us.

There you have it. We were able to build a serverless CRUD API using Python, API Gateway, AWS Lambda, and AWS DynamoDB.

--

--

Sourav Dinda
Sourav Dinda

No responses yet