aws

AWS Lambda and DynamoDB helps to build a Serverless Application

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

  1. Go to DynamoDB → Create Table
  2. Table name: Users
  3. Partition key: userId (Type: String)
  4. Leave sort key and indexes blank for now

Step 2: Create a Lambda Function to Store Users

  1. Go to AWS Lambda → Create Function → Author from scratch
  2. Name: storeUser
  3. Runtime: Node.js 18.x
  4. Replace default code with:
js
const 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 })
};
}
};
  1. Click Deploy

Step 3: Expose Lambda via API Gateway

  1. Go to API Gateway → Create API → HTTP API
  2. Add integration: Lambda → storeUser
  3. Create route: POST /user
  4. Deploy and note the endpoint URL

Test Your Endpoint

bash
curl -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:

js
const 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}

  1. Integration: getUser Lambda
  2. Deploy

Test GET Endpoint

bash
curl 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

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *