
Why Serverless?
If you’ve ever deployed a backend app, you know the pain—spinning up servers, managing scaling, setting up cron jobs, monitoring uptime. Serverless architecture eliminates that mess. With AWS Lambda, you just write code—AWS takes care of the infrastructure. Combine that with DynamoDB (a blazing-fast NoSQL database), and you’ve got a lean, cost-effective backend that scales automatically.
What We’re Building
A simple API with two endpoints:
POST /user
: Accepts and stores user data.GET /user/{userId}
: Retrieves user data by ID.
No EC2, no Docker, no pain—just Lambda, DynamoDB, and API Gateway.
Tools You’ll Need
- An AWS account
- Node.js (for optional local testing)
- AWS CLI (configured with IAM credentials)
- Postman or
curl
- Basic JavaScript/Node.js knowledge
Step 1: Set Up Your DynamoDB Table
- Go to DynamoDB → Create Table
- Table name:
Users
- Partition key:
userId
(Type: String) - Leave sort key and indexes blank for now
Step 2: Create a Lambda Function to Store Users
- Go to AWS Lambda → Create Function → Author from scratch
- Name:
storeUser
- Runtime:
Node.js 18.x
- Replace default code with:
jsconst AWS = require('aws-sdk');
const db = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const body = JSON.parse(event.body);
const { userId, name, email } = body;
const params = {
TableName: 'Users',
Item: { userId, name, email }
};
try {
await db.put(params).promise();
return {
statusCode: 200,
body: JSON.stringify({ message: "User saved!" })
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({ error: err.message })
};
}
};
- Click Deploy
Step 3: Expose Lambda via API Gateway
- Go to API Gateway → Create API → HTTP API
- Add integration: Lambda →
storeUser
- Create route:
POST /user
- Deploy and note the endpoint URL
Test Your Endpoint
bashcurl -X POST https://your-api-id.amazonaws.com/user \
-H "Content-Type: application/json" \
-d '{"userId":"u123", "name":"Ankit", "email":"ankit@example.com"}'
You should see:
json{ "message": "User saved!" }
Step 4: Create a Lambda Function to Retrieve Users
Create another Lambda function named getUser
. Paste this code:
jsconst AWS = require('aws-sdk');
const db = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const userId = event.pathParameters.userId;
const params = {
TableName: 'Users',
Key: { userId }
};
try {
const data = await db.get(params).promise();
return {
statusCode: 200,
body: JSON.stringify(data.Item)
};
} catch (err) {
return {
statusCode: 500,
body: JSON.stringify({ error: err.message })
};
}
};
Add a GET Route in API Gateway
Route: GET /user/{userId}
- Integration:
getUser
Lambda - Deploy
Test GET Endpoint
bashcurl https://your-api-id.amazonaws.com/user/u123
Expected result:
json{ "userId": "u123", "name": "Ankit", "email": "ankit@example.com" }
Best Practices (That You Shouldn’t Skip)
- Environment Variables: Use
process.env
for table names, not hardcoded strings. - Validation: Check for missing fields before writing to the database.
- IAM Permissions: Ensure the Lambda execution role has DynamoDB read/write permissions.
- Monitoring: Use CloudWatch Logs to track errors and usage.
- Billing Awareness: DynamoDB pricing is based on reads/writes—start with on-demand mode.
Read more about tech blogs . To know more about and to work with industry experts visit internboot.com .
Wrapping Up
Congrats! You now have a fully functional serverless API with zero servers, infinite scalability, and minimal maintenance. Whether you’re building MVPs, mobile backends, or side projects—this stack is powerful, affordable, and future-ready.
Once you’ve mastered the basics, you can level up with:
- Authentication via Amazon Cognito
- File uploads via S3
- Workflows using Step Functions
- Email notifications with SES or SNS