Add DynamoDB to Store User Data

by Jan König on Sep 10, 2018

Learn how to use DynamoDB for certain development environments to store user data for your Alexa Skills and Google Actions with Jovo.

Introduction

Jovo offers a database interface that allows you to persist user specific data in a variety of different databases. For additional information on which user specific data is stored, take a look at the Jovo User object.

For local development, it is recommended to use the Jovo File DB that stores data in a db/db.json file for easy debugging. For hosting on AWS Lambda, most people use DynamoDB.

Switching between these different database types for local development and deployment to Lambda can be tedious. In this guide, you will learn how to use individual config files to use different databases or tables for different stages.

Add DynamoDB as Database

Find the full docs about our DynamoDB integration here.

Install the Integration

To add DynamoDB as a database, you first need to download the package from npm:

Then, enable it in your app.js file in the src folder:

You can then define the tableName in your app configuration in the src folder:

However, this will add DynamoDB to the app no matter which stage it is currently in, disabling the FileDB that is helpful for local debugging.

This is why we recommend setting up different staging environments. Learn more about this in the next step.

Add Stages

With staging in your config.js. you can add different app configurations for each environment your app is running in.

You can use individual config files for each stage (config.dev.js, config.prod.js, etc.), that get merged into the default configuration.

For example, a config.prod.js could just define the configurations for the production stage (when the app is live) and use DynamoDB:

The config.js would still look like this:

Use Environment Variables

Additionally, you can also use environment variables to give you more flexibility about the naming of your DynamoDB once the source code is deployed to Lambda:

Additional Steps

After you have added updated the config files, you can upload your code to AWS Lambda (see our guide on how to deploy to Lambda for more information).

There are a few more steps to do to make DynamoDB work:

Specify Stage on AWS Lambda

To be able to use different configurations for different stages, you need to let your code know which stage it is currently in.

You can do so by adding either NODE_ENV or STAGE to the environment variables on AWS Lambda:

Staging environment variable in AWS Lambda

This will be used to determine, which config file should be used, in case you've set it.

Add Permissions to Lambda Role

To make your Lambda function able to create database tables, and edit their content, you need to give the IAM role that's associated with it the right permissions. You can find out more in the official documentation by Amazon: AWS Lambda Permissions Model.

The easiest way to go is to give your role full permission by attaching the AmazonDynamoDBFullAcess policy:

AWS Lambda DynamoDB Full Access

However, if you prefer to only give it the permissions needed, you can also create a new policy. Jovo needs the following permissions to be able to use the DynamoDB database integration:

  • Read
    • GetItem
  • Write
    • CreateTable
    • PutItem

Here is a policy summary you can use for it:

After attaching this policy to your role, you should be good to go.

Test on Lambda

After you have uploaded your code to Lambda, given your role the right permissions, you can test the function.

The initial test should create a fresh new DynamoDB table.


Jan König

Co-founder at Jovo

Comments and Questions

Any specific questions? Just drop them below or join the Jovo Community Forum.

Join Our Newsletter

Be the first to get our free tutorials, courses, and other resources for voice app developers.