Configuring Aws SDK V3 for Javascript

Aws released the Aws SDK v3 for javascript in Dec 2020, with new features such as a new middleware stack and first class support for typescript. Aws has also announced end of support of Aws SDK v2 for Javascript as of Sep 2025. It is best if users were to install and use Aws SDK V3 going forward. This blog post outlines the steps to use Aws SDK V3.

Aws SDK version 3.201.0 and higher requires Node Js 14 or higher. Below are the steps to use Aws SDK v3 to write a program that interacts with Amazon DynamoDB

Steps to use Aws SDK V3

Create a new project

mkdir njsapp

cd njsapp

npm init

npm install @aws-sdk/client-dynamodb

npm install @aws-sdk/lib-dynamodb

Create a node.js program

First make sure that you can run .js programs as a javascript module, add the following line to your package.json file

{

  “name”: “njsddb”,

  “version”: “1.0.0”,

  “main”: “index.js”,

  “scripts”: {

    “test”: “echo \”Error: no test specified\” && exit 1″

  },

  “author”: “”,

  “license”: “ISC”,

  “description”: “”,

  “dependencies”: {

    “@aws-sdk/client-dynamodb”: “^3.590.0”,

    “@aws-sdk/lib-dynamodb”: “^3.590.0”

  }

}

You can use the sample code now to create a javascript program that interacts with Amazon DynamoDB. Create a .js file with the following code.

import { DynamoDBClient } from “@aws-sdk/client-dynamodb”;
import { PutCommand, DynamoDBDocumentClient } from “@aws-sdk/lib-dynamodb”;

const client = new DynamoDBClient({region:’us-east-2′});

const docClient = DynamoDBDocumentClient.from(client);

export const main = async () => {

  const command = new GetCommand({

    TableName: “ProductCatalog”,

    Key: {

      Id: 101,

    },

  });

  const response = await docClient.send(command);

  console.log(response);

  return response;

};

Make sure you add, main() as the last line in the file. You can then execute the code as follows.

node <filename>.js

Leave a Reply

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