Building a Serverless Architecture: A Guide to Creating an API Gateway with Lambda and DynamoDB…

Building a Serverless Architecture: A Guide to Creating an API Gateway with Lambda and DynamoDB…

Introduction

In today’s digital world, it’s crucial for businesses to have a fast, scalable, and reliable system for handling their application’s requests. Utilizing the serverless architecture offered by cloud service providers like Amazon Web Services is one of the most well-liked ways to do this (AWS). An API Gateway is a crucial part of a serverless architecture since it serves as the entrance point for the requests made by your application. We’ll walk you through setting up an API Gateway that integrates AWS Lambda and DynamoDB in this post.

The architecture of the API Gateway Integration with Lambda

AWS Lambda is a serverless computing service that automatically manages the underlying infrastructure while running your code in response to events. On the other side, DynamoDB is a NoSQL database service offered by AWS that is quick and adaptable. You may quickly create a highly scalable and economical solution to handle the requests for your application by merging these two services.

What is Serverless?

Serverless in AWS refers to a cloud computing execution model where the customer writes and uploads their application code to AWS, and the cloud provider manages the underlying infrastructure and operational tasks. This allows customers to build and run applications and services without having to worry about managing the infrastructure. The AWS services that support serverless computing include AWS Lambda for running code, API Gateway for building and deploying APIs, and DynamoDB for NoSQL database storage. In this model, customers pay only for the exact amount of compute time their code uses, making it a cost-effective and scalable solution.

AWS Services used in this article :

  1. API Gateway
  2. Lambda
  3. Cloudwatch
  4. Dynamo DB

Here are the steps:

  1. Create a DynamoDB table:

  2. Log in to your AWS account and navigate to the DynamoDB console.

  3. Click on “Create table”.
  4. Give your table a name and specify the primary key.
  5. Click on “Create”.

Create Table in Dynamo DB

2. Create IAM role for the lambda Function:

  • Navigate to the IAM console.
  • Click on “Roles” from the left navigation pane.
  • Click on “Create Role” button.
  • Select “AWS service” as the type of trusted entity and “Lambda” as the service that will use this role.
  • Click on “Next: Permissions”.
  • Search for and select the policies that you want to attach to the role. Select the “AmazonDynamoDBFullAccess” and “CloudWatchFullAccess” policy to give your Lambda function full access to your DynamoDB table.
  • Click on “Next: Tags” to add any tags you want to the role.
  • Click on “Next: Review”.
  • Give your role a name and review the settings.
  • Click Create Role

Permission attach to the Lambda Role

3. Create a Lambda function:

  • Log in to your AWS account and navigate to the Lambda console.
  • Click on “Create function”.
  • Select “Author from scratch”.
  • Give your function a name and select “Python3.9” as the runtime.
  • Select the Execution role as Use an existing role. Select the Role created before the lambda.
  • Click on “Create function”.

Create Lambda Function

4. Add Code to Lambda Function

lambda_function.py Source code : Click here

5. Configure API Gateway:

  • Navigate to the API Gateway console.
  • Click on “Create API”.
  • Select “REST API”.
  • Click on “Build”.
  • Create a resource and method for each of the HTTP methods you want to support (GET, POST, DELETE).

Creating the Resource

  • For each method, select “Lambda Function” as the integration type and select your Lambda function.

Creating The Get Method inside Resource

  • Click on “Save” and “Deploy API”.

That’s it! You should now have an API Gateway with Lambda and DynamoDB integration set up. You can test your API by using the “Invoke URL” provided by API Gateway.

Invoke URL of the API

6. Let’s Test the API On the Postman