Design a site like this with
Get started

[11] [ProjectX] – Using AWS DynamoDB NoSQL database for Persistence

Source code:

In our last blog post we created API for our book catalog. But all the data is hard-coded in lambda functions. So lets make changes to it so that we can use a database for persisting book catalog data.

Amazon has a service called DynamoDB, which is a NoSQL database hosted and managed by AWS.

We will be using this for our backend data persistence.


Let’s start with setting up our database. Login to console and open DynamoDB service. You will see the Dashboard page as show below

Click on Create Table button

After a while, a table will be created as follows

If you click Items tab, you will see that there are no items present right now

Now lets add one item here. Click on Create item button and add the following data

We created only one field/column in our table called “id”. That is because NoSQL databases have the option of having dynamic schema where each row/record can have different columns.

So add 3 other columns using + button > Append > String options

Click Save button. Now you should be able to see the new item like shown below

This complets our database setup. Now lets continue with our project setup


Create Blog11 folder in your IDE as shown below and copy the files from GitHub and place it here, as shown below

Changes have been made to these files so that they can now interact with our DynamoDB table we created above. We are still doing basic CRUD operations and hence have 4 handler files in handlers folder for each operation.

Index.js is our main handler file that receives API requests and routes it to appropriate handler

Now, lets take a look at get-book.js file

Here we can see the changes have been made to use the AWS.DynamoDB. DocumentClient library to interact with DynamoDB database from the code. If this code is passed parameter bookid, it will return one book. Otherwise it will return All books.


We will now deploy this api project using the easy deployment method we have seen till now,i.e. using Claudia.js framework.

npm init

Type yes and ENTER to confirm.

Install claudia.js framework

npm install -g claudia

Install claudia api builder

npm install claudia-api-builder -s

We also have to install aws sdk for javascript.

npm install aws-sdk

Now, lets deploy our API

claudia create –region us-east-1 –api-module index

This completes the deploymetn and we have got URL for accessing our API, as shown above


First lets check whether deployment was done correctly and artifacts are visible in AWS Console

Now open Postman API testing tool and hit /books URL

You will see that it was Not successful and we get the below error.

This is correct. We have Not given permission to our role “books-catalog-api-executor” to access DynamoDB and hence it cnanot access it. So lets fix it by creating the correct policy that has right permissions to access DynamoDB and attach that policy to our api role.


Go to AWS IAM and click Policies and then click Create policy button

Click JSON tab in below window and ignore the message shown.

Now paste the following policy statement in it

This policies allow CRUD operations on DynamoDb database. You can find this in a file called dynamodb.json in policies folder. Paste this in side JSON tab and click Review policy button

Then click Create policy button. Enter Name and Description and click Create policy button

Now we have to attach this policy to our Role.

Click Roles and find and open books-catalog-api-executor role

Click Attach policy button and select our newly created plolicy BasicDynamoDBPermissions and click Attach policy button

You can see now that our Role has two policies attached to it.

That’s it. Permissoins have been given so now let’s resume testing


1. Get all books

Method: GET



2. Add a new book

Method POST



We can verify whether the book is added by calling GET again

3. Get a single book

Method PUT


4. Update a book’s details.

Lets change price of Redemption book to 13.95

Method PUT



We can verify by going to DynamoDb database

5. Delete a book

Lets delete Redemption book



That’s it. We are done.

7. Clean up:

To cleanup, execute the following command

claudia destroy

This will Not cleanup (delete) table we created in DynamoDB (books-catalog). So make sure to delete the table manually.

8. Conclusion

So now, our API’s has ability to store and retrieve data usign a NoSQL database on AWS called DynamoDB.

Happy Clouding!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: