How to Store API Request Data in DynamoDB Using AWS Lambda and API Gateway
In serverless apps, it's common to process incoming requests via API Gateway and store relevant data using AWS Lambda and DynamoDB. This setup allows you to create powerful, scalable APIs with zero infrastructure management. In this guide, we'll walk through capturing data from an API endpoint and saving it to DynamoDB using a Lambda function. Step 1: Create a DynamoDB Table Head to the DynamoDB console and create a new table: Table name: Requests Partition key: requestId (String) Step 2: Create a Lambda Function Go to AWS Lambda and create a new function: Runtime: Node.js 18.x Permissions: Attach AmazonDynamoDBFullAccess (for demo) Use the following code to handle and store incoming request data: const { v4: uuidv4 } = require('uuid'); const AWS = require('aws-sdk'); const dynamodb = new AWS.DynamoDB.DocumentClient(); exports.handler = async (event) => { const requestId = uuidv4(); const timestamp = new Date().toISOString(); const body = JSON.parse(event.body); const item = { requestId, timestamp, path: event.path, method: event.httpMethod, data: body, }; const params = { TableName: 'Requests', Item: item, }; try { await dynamodb.put(params).promise(); return { statusCode: 200, body: JSON.stringify({ message: 'Request stored', requestId }), }; } catch (error) { return { statusCode: 500, body: JSON.stringify({ error: error.message }), }; } }; Step 3: Connect API Gateway Create a new HTTP API using API Gateway and add a POST route (e.g., /track). Attach the Lambda function you created as the integration target. Step 4: Test the Flow Use Postman or curl to make a POST request to the endpoint with a JSON body: curl -X POST https://your-api-id.amazonaws.com/track \ -H "Content-Type: application/json" \ -d '{"event": "page_view", "user": "abc123"}' Then check the DynamoDB table for your new record. Conclusion With Lambda, API Gateway, and DynamoDB, you can create fully serverless, highly scalable backend APIs. This setup is perfect for logging, tracking, analytics, or lightweight data ingestion pipelines. Was this guide useful? Feel free to support my writing here: buymeacoffee.com/hexshift
In serverless apps, it's common to process incoming requests via API Gateway and store relevant data using AWS Lambda and DynamoDB. This setup allows you to create powerful, scalable APIs with zero infrastructure management. In this guide, we'll walk through capturing data from an API endpoint and saving it to DynamoDB using a Lambda function.
Step 1: Create a DynamoDB Table
Head to the DynamoDB console and create a new table:
-
Table name:
Requests
-
Partition key:
requestId
(String)
Step 2: Create a Lambda Function
Go to AWS Lambda and create a new function:
- Runtime: Node.js 18.x
- Permissions: Attach
AmazonDynamoDBFullAccess
(for demo)
Use the following code to handle and store incoming request data:
const { v4: uuidv4 } = require('uuid');
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const requestId = uuidv4();
const timestamp = new Date().toISOString();
const body = JSON.parse(event.body);
const item = {
requestId,
timestamp,
path: event.path,
method: event.httpMethod,
data: body,
};
const params = {
TableName: 'Requests',
Item: item,
};
try {
await dynamodb.put(params).promise();
return {
statusCode: 200,
body: JSON.stringify({ message: 'Request stored', requestId }),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: error.message }),
};
}
};
Step 3: Connect API Gateway
Create a new HTTP API using API Gateway and add a POST route (e.g., /track
). Attach the Lambda function you created as the integration target.
Step 4: Test the Flow
Use Postman or curl
to make a POST request to the endpoint with a JSON body:
curl -X POST https://your-api-id.amazonaws.com/track \
-H "Content-Type: application/json" \
-d '{"event": "page_view", "user": "abc123"}'
Then check the DynamoDB table for your new record.
Conclusion
With Lambda, API Gateway, and DynamoDB, you can create fully serverless, highly scalable backend APIs. This setup is perfect for logging, tracking, analytics, or lightweight data ingestion pipelines.
Was this guide useful? Feel free to support my writing here: buymeacoffee.com/hexshift