Create a Fast Node.js Serverless Backend Using AWS Lambda and DynamoDB

Hello, I am a full stack developer since 2010 focused for a long moment more on backend stuffs. I embraced cloud development since 2019.
Search for a command to run...

Hello, I am a full stack developer since 2010 focused for a long moment more on backend stuffs. I embraced cloud development since 2019.
No comments yet. Be the first to comment.
In this mini-series, we will quickly and effortlessly build a simple CRUD nodeJs serverless backend using Amazon web services Lambda, API gateway, DynamoDb, Cognito, etc.
Security in internet-facing or local applications is one of the most challenging parts of the software development life cycle. This post will provide a quick step-by-step guide to implementing access management for the previous REST API using Amazon ...
Security in internet-facing or local applications is one of the most challenging parts of the software development life cycle. This post will provide a quick step-by-step guide to implementing access management for the previous REST API using Amazon ...

In this post, we will create a REST API endpoint with GET and POST methods for the previous backend using Amazon API Gateway's HTTP API. Prerequisites All you need is just an AWS account: no worries, if you are new to AWS, AWS offers you a 12-month f...

There are many articles on how to build a real-world Spring Boot CRUD REST API or Application with MySQL as a Relational Database. In this post, we are not only going to show how to create a Spring Boot CRUD operations API, but we will also dive deep...

Before entering the AWS Cloud world, as a Java developer, it was often difficult for me to get Java-based applications online for my clients. Fortunately, I found out later that AWS provides many proven, fully managed services for deploying Java-bas...

In this article, we'll build a simple CRUD JavaScript-based backend with AWS. We'll use Lambda and DynamoDB to show how you can quickly create a secure and robust app backend.
To follow this tutorial, you need an AWS account. Thankfully, AWS offers a twelve-months free tier plan (isn't that cool? 😜).
We'll use the AWS console to create the Lambda function.



Once the function is ready, open the sample-crud-func function and test it.

The basic Lambda function is now ready. Before we add more logic to it, let's create the DynamoDB table for data storage.
DynamoDB is a fully managed, serverless NoSQL database service provided by AWS for modern applications. There are no upfront costs to launch DynamoDB, and you only pay for what you use.
Go to the DynamoDB page, and choose the region. Here, we select us-east-1: N. Virginia (1). In the page menu, select Tables (2), then click on Create table button(3).





In the Edit policy editor, add the below permission to the permissions list:
{
"Effect": "Allow",
"Action": [
"dynamodb:PartiQLInsert",
"dynamodb:PartiQLUpdate",
"dynamodb:PartiQLDelete",
"dynamodb:PartiQLSelect"
],
"Resource": [
"arn:aws:dynamodb:us-east-1:xxx:table/sampleCrudTable"
]
}
Note: Remember to replace xxx with your AWS account number.
Finally, the Policy editor should look like this:

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
DynamoDBDocumentClient,
ExecuteStatementCommand,
} from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);
const ITEM_PK = "ITEM";
//-------SAVE ITEM FUNCTION-----------
const saveItem = async (tableName, data) => {
const SK = ITEM_PK + "#" + data?.label; //format PK#label => manage default label sorting
let result;
try {
const command = new ExecuteStatementCommand({
Statement: `INSERT INTO ${tableName} value {'pk':?,'sk':?,'label':?, 'category':?, 'price':?}`,
Parameters: [ITEM_PK, SK, data?.label, data?.category, data?.price],
});
result = await docClient.send(command);
} catch (e) {
console.error(JSON.stringify(e));
return e;
}
return result;
};
export const handler = async (event) => {
const tableName="sampleCrudTable";
//Call save Item
await saveItem(tableName, event)
.then((res) => {
console.log(JSON.stringify(res))
})
.catch((e) => {
console.log(JSON.stringify(e.errorMessage));
});
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
{
"label": "Paquet de Crayons de couleurs",
"price": 30,
"category": {
"id": 1,
"label": "Fourniture scolaire"
}
}


In this article, we guided you through building a simple CRUD backend using AWS Lambda and DynamoDB. You learned how to set up a Lambda function, create a DynamoDB table, grant necessary permissions, and perform CRUD operations using JavaScript.
In the next articles, we'll learn how to secure and protect this backend using API Gateway and Amazon Cognito.
Please subscribe and share. Thanks!