Building a Serverless API with AWS Lambda, API Gateway, and DynamoDB

Serverless architecture simplifies API development by eliminating the need for server management, allowing developers to focus on writing code without worrying about infrastructure. In this blog, we’ll build a Random Quote Generator API using four key AWS services: AWS Lambda, which will handle API requests efficiently; Amazon API Gateway, which will make the API publicly accessible and manage traffic; and DynamoDB, a fast and flexible NoSQL database that will store our collection of quotes and AWS CloudWatch which will monitor the API performance. By the end of this blog, we’ll have a fully functional, scalable, and cost-effective API that returns random quotes in JSON format, demonstrating the power and convenience of serverless computing.

Step 1: Setting Up DynamoDB to Store Quotes

DynamoDB is a fully managed NoSQL database that provides fast and scalable data storage. We’ll use it to store our quotes.

Creating a DynamoDB Table

Go to AWS ConsoleDynamoDBTablesCreate table
Table name: QuotesTable
Partition key: quote_id (Type: String)

Click Create Table

Once, the table is created, create items in the table (i.e quotes)

Step 2: Creating the AWS Lambda Function

What is AWS Lambda?

AWS Lambda is a serverless compute service that runs code in response to HTTP requests, eliminating infrastructure management.

Creating the Lambda Function

Go to AWS LambdaCreate function
Function name: RandomQuoteLambda
Runtime: Python 3.9

Execution role: Create a new role with basic permissions

Also add the AWSLambdaBasicExecutionRole

After creating these roles, go back to Lambda and click on Create Function

Granting DynamoDB Read Access

Lambda needs permission to access DynamoDB:
Go to IAMRoles
Find the role for RandomQuoteLambda
Click Attach Policies
Search for AmazonDynamoDBReadOnlyAccess → Attach

Writing the Lambda Function Code

After writing your application click on Deploy.

Step 3: Setting Up API Gateway

Amazon API Gateway lets you expose the Lambda function as a publicly accessible REST API.

Creating API Gateway

Go to API GatewayCreate API
Select REST APIBuild

API name: RandomQuoteAPI

Click Create API

Creating an API Resource

Click ActionsCreate Resource
Resource name: /
Resource path: /random-quote

Click Create Resource

Creating a GET Method

Click ActionsCreate MethodGET

Integration type: Lambda Function
Enter Lambda function name: RandomQuoteLambda
Click SaveOK

Step 4: Deploying & Testing the API

Deploying API Gateway

Click ActionsDeploy API

Stage name: prod
Click Deploy

Step 5: Testing the API

Copy the Invoke URL

Step 6: Monitoring API Performance

We will be using CloudWatch to monitor the health and performance of our API
Go to AWS CloudWatch
Click Logs → Find RandomQuoteLambda logs

Analyze logs for debugging.

In conclusion, building a serverless API with AWS Lambda, API Gateway, and DynamoDB offers a streamlined approach to application development by removing the complexities of server management. This architecture not only enhances scalability and cost-effectiveness but also allows to concentrate on crafting efficient code.