Build a Node Js CRUD API with AWS Lambda function and DynamoDB
Harish Palsande
Software Engineer | Full Stack Developer | React Js | Node Js | Microservices | AWS | Azure | Architecture
?? Thank you for your interest in this article, if you like the content feel free to follow me, clap???? and share it.??
Table of Contents
1. Create a DynamoDB table
You use a DynamoDB table to store data for your API.
Each user has a unique ID, which we use as the partition key for the table.
To create a DynamoDB table
2. Create a Lambda function
You create a Lambda function for the backend of your API. This Lambda function creates, reads, updates, and deletes users from DynamoDB. The function uses events from API Gateway to determine how to interact with DynamoDB. For simplicity, this tutorial uses a single Lambda function. As a best practice, you should create separate functions for each route.
To create a Lambda function
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
DynamoDBDocumentClient,
ScanCommand,
PutCommand,
GetCommand,
DeleteCommand,
} from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
const dynamo = DynamoDBDocumentClient.from(client);
const tableName = "http-crud-users";
export const handler = async (event, context) => {
let body;
let statusCode = 200;
const headers = {
"Content-Type": "application/json",
};
try {
switch (event.routeKey) {
case "DELETE /users/{userId}":
await dynamo.send(
new DeleteCommand({
TableName: tableName,
Key: {
userId: event.pathParameters.userId,
},
})
);
body = `Deleted user ${event.pathParameters.userId}`;
break;
case "GET /users/{userId}":
body = await dynamo.send(
new GetCommand({
TableName: tableName,
Key: {
id: event.pathParameters.userId,
},
})
);
body = body.user;
break;
case "GET /users":
body = await dynamo.send(
new ScanCommand({ TableName: tableName })
);
body = body.users;
break;
case "PUT /users":
let requestJSON = JSON.parse(event.body);
await dynamo.send(
new PutCommand({
TableName: tableName,
user: {
userId: requestJSON.userId,
price: requestJSON.price,
name: requestJSON.name,
},
})
);
body = `Put user ${requestJSON.id}`;
break;
default:
throw new Error(`Unsupported route: "${event.routeKey}"`);
}
} catch (err) {
statusCode = 400;
body = err.message;
} finally {
body = JSON.stringify(body);
}
return {
statusCode,
body,
headers,
};
};
3. Create an HTTP API
The HTTP API provides an HTTP endpoint for your Lambda function. In this step, you create an empty API. In the following steps, you configure routes and integrations to connect your API and your Lambda function.
To create an HTTP API
4. Create routes
Routes are a way to send incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource path, for example, GET /users. For this example API, we create four routes:
To create routes
领英推荐
5. Create an integration
You create an integration to connect a route to backend resources. For this example API, you create one Lambda integration that you use for all routes.
To create an integration
6. Attach your integration to routes
For this example API, you use the same Lambda integration for all routes. After you attach the integration to all of the API’s routes, your Lambda function is invoked when a client calls any of your routes.
To attach integrations to routes
All routes show that an AWS Lambda integration is attached.
7. Deploy API’s
8. Test your API
To get the URL to invoke your API
To create or update an user
To get all users
To get an user
To delete an user
?? Thank you for reading my article, if you like the content feel free to , clap follow me???? and share it.??
Js Full Stacker , AWS Architect , Scrum Master , GraphQl associate
4 个月i am not sure how many people have actually tested it .. All the steps are proper but this wont work directly unless we update lambda with ?????? Item : { ???????userid: requestJSON.userid, ???????price: requestJSON.price, ???????name: requestJSON.name, ??????} userid should be smallcase as mentioned in Partition id
ASSOCIATE SOFTWARE ENGINEER| AHEX TECHNOLOGIES| BE-CSE-RCET-21' GRADUATE
5 个月Helpful !
MSC (Computer Science) Pune Vidyarthi Griha, Pune.
5 个月Very helpful!
Frontend Developer at Vishwa Technologies Karad
5 个月Very helpful!
Software Engineer | Full Stack Developer | React Js | Node Js | Microservices | AWS | Azure | Architecture
5 个月Hi! I thought you might like to read my new article: Build a Node Js CRUD API with AWS Lambda function and DynamoDB https://www.dhirubhai.net/pulse/build-node-js-crud-api-aws-lambda-function-dynamodb-harish-palsande-zjpic