DynamoDB 101 — Connect to DynamoDB with Node.js

Preparation

Create a User and a Group

The answer is you have to create a user and password for your DynamoDB in IAM (Identity and Access Management). You can imagine IAM as a central system that manages all the credentials like username, access key, secret key, etc. for each of the services you use in AWS.

  1. Sign in to AWS console.
  2. Enter “IAM” in the search box under Find Services, click on it or press ENTER key.
  3. In the left sidebar: click on Groups under Access management -> click on Create New Group -> give your group a name (you can edit it any time) and click on Next Step -> enter DynamoDB in the search box, then all related policies will be shown in the table below it, check the one/ones that suits your need (if you want to limit a group to be able to only access a specific table, refer to Read More section below), e.g. AmazonDynamoDBFullAccess, and click Next Step -> click on Create Group.
  4. In the left sidebar: click on Users under Access management -> click on Add user -> give your user a name, check Programmatic access, click on Next: Permissions -> check the group/groups you want to add this user to (this will apply different permission to this user based on your selection), click on Next: Tags -> add some tags (key value pairs)to this user if you want, for example enter description in Key and This user is a test account. under Value, you can add as many tags as you want, after you are done, click on Next: Review -> if everything looks good, click on Create user.
  5. IMPORTANT: Copy & paste Access key ID and Secret access key in this page or download the .csv file since you won’t be able to see this information again, unless you go through this whole process above and create a new user, and you’ll need them later to connect to DynamoDB.

Install aws-sdk

npm i aws-sdk

Install dotenv

npm i dotenv

Then you can create a .env file (remember to add this file name to .gitignore) to store the credential information as environment variables. If you are deploying your project on platform like Heroku, you can configure environment variables in settings and not necessarily to create this .env file.

Connect to DynamoDB

DYNAMODB_ACCESS_KEY_ID=IAMADUMMYACCESSKEY
DYNAMODB_SECRET_ACCESS_KEY=iamAdummySecretkey

Then in your index.js (or other file you want to connect to DynamoDB), specify region (you can find your region on the top right corner of the navbar after login), accessKeyId and secretAccessKey:

require("dotenv").config(); // read .env
const AWS = require("aws-sdk");
AWS.config.update({
region: "us-west-1",
accessKeyId: process.env.DYNAMODB_ACCESS_KEY_ID,
secretAccessKey: process.env.DYNAMODB_SECRET_ACCESS_KEY,
});
const docClient = new AWS.DynamoDB.DocumentClient();

Cool! Now you are able to perform CRUD operations by calling different methods on docClient.

Read More

DynamoDB 101 — Allow Access to a Specific Table

A lifelong learner who has enthusiasm for sharing knowledge; a developer who loves bringing ideas to life.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store