AWS Builder Center

Building a Serverless REST API in 10 Minutes Using AWS Lambda and API Gateway

Learn how to create and deploy a scalable, cost-effective serverless REST API using AWS Lambda, Amazon API Gateway, and DynamoDB with minimal setup

Building a Serverless REST API in 10 Minutes Using AWS Lambda and API Gateway

When starting out with cloud development, setting up and managing traditional virtual servers can feel overwhelming. Serverless architecture changes the game. By using AWS managed services, you can deploy applications that scale automatically from zero to millions of requests without managing any underlying infrastructure. 
In this guide, we will build a basic serverless REST API that accepts data and stores it in a database. 



🛠️ The Architecture

Our simple architecture consists of three core components: 
  1. Amazon API Gateway: Acts as the front door, exposing public HTTP endpoints. 
  1. AWS Lambda: The serverless compute engine that runs our backend logic when triggered. 
  1. Amazon DynamoDB: A fully managed, NoSQL database to store our application data. 



🚀 Step-by-Step Implementation

Step 1: Create the DynamoDB Table

First, we need a place to store our data. 
  1. Open the Amazon DynamoDB Console. 
  1. Click Create table. 
  1. Name your table  UserTable  and set the Partition key to  userId  (String). 
  1. Leave the rest as default and click Create table. 

Step 2: Create the IAM Execution Role

Your Lambda function needs explicit permission to write data to DynamoDB. 
  1. Open the IAM Console and go to Roles -> Create role. 
  1. Choose Lambda as the trusted entity. 
  1. Attach the managed policy  AmazonDynamoDBFullAccess  (Note: For production, always use least-privilege permissions). 
  1. Name the role  LambdaDynamoDBRole  and complete the creation. 

Step 3: Write the Lambda Function

Now, let's deploy the backend logic using Python. 
  1. Open the AWS Lambda Console and click Create function. 
  1. Select Author from scratch, name it  SaveUserData , and choose Python 3.12 as the runtime. 
  1. Under Change default execution role, select Use an existing role and choose  LambdaDynamoDBRole . 
  1. Replace the default code with the script below: 
python
import json
import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('UserTable')

def lambda_handler(event, context):
try:
# Parse the incoming JSON request body
body = json.loads(event['body'])
user_id = body['userId']
name = body['name']
# Write data to DynamoDB
table.put_item(
Item={
'userId': user_id,
'name': name
}
)
return {
'statusCode': 200,
'body': json.dumps({'message': 'User data saved successfully!'})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}

Use code with caution.
  1. Click Deploy. 

Step 4: Expose the API via API Gateway

Finally, let's create an HTTP endpoint to trigger our function. 
  1. Open the Amazon API Gateway Console and choose HTTP API (or REST API) and click Build. 
  1. Click Add integration, select Lambda, and choose your  SaveUserData  function. 
  1. Configure a POST route and name it  /user . 
  1. Review and click Create. You will receive an Invoke URL. 



🧪 Testing Your Serverless API

You can test your new live endpoint using tools like Postman, Curl, or an online API client. Send a POST request to your Invoke URL ( https://your-api-id.execute-api.region.amazonaws.com/user ) with the following JSON body: 
json
{
"userId": "1271",
"name": "Bhanu"
}

Use code with caution.
If successful, you will receive a  200 OK  response, and checking your DynamoDB console will show the new entry recorded safely in the cloud! 



🎯 Conclusion

You have just built a production-ready, highly available REST API. Because this architecture is serverless, you only pay exactly for the milliseconds your code runs. 
What features will you add next? Try expanding this project by adding a GET method to fetch user data!
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article