2024 AWS DynamoDB setup with Nodejs/JavaScript
step 1
we will try to setup AWS DynamoDB
1. First you have to a AWS account. try with free tier.
2. Create user/user group in IAM with giving possible user permissions


3. find your accessKeyId & secretAccessKey
4. find your accountId & region. its located in aws console top right .just click on the profile icon/your console name.
5. Created New Policies for a DynamoDB Access management (read/write etc)





please aware about this : “arn:aws:dynamodb:REGION:ACCOUNT_ID:table/DYNAMODB-TABLE-NAME”
you can replace with this code when you going to edit the policy.
{
“Version”: “2012–10–17”,
“Statement”: [
{
“Effect”: “Allow”,
“Action”: [
“dynamodb:GetItem”,
“dynamodb:Query”,
“dynamodb:Scan”
],
“Resource”: “arn:aws:dynamodb:us-east-1:718306285872:table/table1”
},
{
“Effect”: “Allow”,
“Action”: [
“dynamodb:PutItem”,
“dynamodb:UpdateItem”,
“dynamodb:DeleteItem”
],
“Resource”: “arn:aws:dynamodb:us-east-1:718306285872:table/table1”
},
{
“Effect”: “Allow”,
“Action”: “dynamodb:*”,
“Resource”: “*”
}
]
}
Follow this link : https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/iam-policy-example-data-crud.html
6. Add that policy to that user/user group

7. Add Permissions boundary to created policy for a DynamoDB Access management.

8. Enable the dynamoDB service and create table & create sample item.




Aws configuration are finished now.
step 2
we will try to setup NodeJS Project to connect with DynamoDB
1. Create nodejs + express project
2. npm install @aws-sdk/client-dynamodb — force
3. Coding time
import { DynamoDBClient, BatchExecuteStatementCommand,CreateTableCommand ,GetItemCommand,ScanCommand } from “@aws-sdk/client-dynamodb”;
const config = {
aws_table_name: ‘dynamodb-test’,
aws_local_config: {
//Provide details for local configuration
},
aws_remote_config: {
accessKeyId: ‘ — — — — — ’,
secretAccessKey: ‘ — — — — — — — — — — — ’,
region: ‘us-east-1’,
}
};
try{
const client = new DynamoDBClient(config.aws_remote_config);
console.log(“aws dynamodb connected”)
const params = {
TableName: “table1”,
Key: {
primary_key : 0
},
};
// const getItemCommand = new GetItemCommand(params);
// const data = await client.send(getItemCommand);
// console.log(“red Item:”, data.Item);
const scanCommand = new ScanCommand(params);
const data = await client.send(scanCommand);
console.log(“Items:”, data.Items);
console.log(“create table”,response)
}catch(err){
console.log(“error “,err)
}finally{
console.log(“final block .”)
}
app.listen(process.env.PORT, () => {
console.log(`Server running on http://localhost:${process.env.PORT}`);
});
4. Run the project

written by savindu pasintha