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:
- Amazon API Gateway: Acts as the front door, exposing public HTTP endpoints.
- AWS Lambda: The serverless compute engine that runs our backend logic when triggered.
- 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.
- Open the Amazon DynamoDB Console.
- Click Create table.
- Name your table
UserTableand set the Partition key touserId(String).
- 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.
- Open the IAM Console and go to Roles -> Create role.
- Choose Lambda as the trusted entity.
- Attach the managed policy
AmazonDynamoDBFullAccess(Note: For production, always use least-privilege permissions).
- Name the role
LambdaDynamoDBRoleand complete the creation.
Step 3: Write the Lambda Function
Now, let's deploy the backend logic using Python.
- Open the AWS Lambda Console and click Create function.
- Select Author from scratch, name it
SaveUserData, and choose Python 3.12 as the runtime.
- Under Change default execution role, select Use an existing role and choose
LambdaDynamoDBRole.
- Replace the default code with the script below:
python
import jsonimport 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.
- Click Deploy.
Step 4: Expose the API via API Gateway
Finally, let's create an HTTP endpoint to trigger our function.
- Open the Amazon API Gateway Console and choose HTTP API (or REST API) and click Build.
- Click Add integration, select Lambda, and choose your
SaveUserDatafunction.
- Configure a POST route and name it
/user.
- 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!
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article