DEV Community

Joseph Peculiar
Joseph Peculiar

Posted on

How to run DynamoDB Offline

I am assuming you are already familiar with the Serverless Framework so I'll jump right into it

Quick overview

Amazon DynamoDB:- is a fully managed proprietary NoSQL database service that supports key-value and document data structures designed to deliver fast and predictable performance.

The Serverless Framework:- enables developers to deploy backend applications as independent functions that will be deployed to AWS Lambda.

Getting started

let's kick off by configuring AWS if not already configured doing this you will have to download the AWS CLI for Windows, macOS, or Linux

The serverless framework will need us to configure access to AWS - This is accomplished by running

aws configure
Enter fullscreen mode Exit fullscreen mode

Install Serverless globally if you haven't installed it already

npm i -g serverless
Enter fullscreen mode Exit fullscreen mode

Now let's setup up the serverless-offline plugin

npm i -g serverless-offline
Enter fullscreen mode Exit fullscreen mode

Install serverless-dynamodb-local plugin

npm i serverless-dynamodb-local
Enter fullscreen mode Exit fullscreen mode

Update your serverless.yml file - add this to the bottom of your serverless.yml file

plugins:
  - serverless-dynamodb-local
  - serverless-offline
Enter fullscreen mode Exit fullscreen mode

Initialize DynamoDB - this initializes DynamoDB offline in your project folder

sls dynamodb install
Enter fullscreen mode Exit fullscreen mode

Run serverless:-

sls offline start
Enter fullscreen mode Exit fullscreen mode

Be sure DynamoDB is running on port 8000 because serverless-offline will use that port as default

you should see the output below

Dynamodb Local Started, Visit: http://localhost:8089/shell
Enter fullscreen mode Exit fullscreen mode

Keep this console running.

Now let's setup dynamodb-admin: this gives us a good GUI to view our DynamoDB tables

npm install dynamodb-admin -g

export DYNAMO_ENDPOINT=http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

Now run

dynamodb-admin
Enter fullscreen mode Exit fullscreen mode

We should have our table displayed

Configuring serverless.yml for DynamoDB

dynamodb:
    stages: dev
    start:
      port: 8089 
      # set our custom DynamoDB port
      migrate: true
      # creates tables from serverless config
      seed: true 
      #determines which data to onload
    seed:
      domain:
        sources:
          - table: MyDB-dev
            sources: [./resources/seeds/myDB.json]
Enter fullscreen mode Exit fullscreen mode

I published a great cheat sheet with all commands needed for setting up DynamoDB offline you can find it here

https://github.com/codaelux/DynamoDB-offline-doc

Top comments (3)

Collapse
 
leandromarques profile image
Leandro Marques

Superb article!

I'm trying to create the tables when I start the serverless offline, but I'm using the amazon/dynamodb-local Docker container instead of the serverless-dynamodb-local plugin because I need to run this inside docker.

Actually, my serverless configuration is running an AWS Lambda locally, but I wanna migrate my dynamodb from there.

Is there some way to run the migrations using the serverless.yml and the dynamodb-local container in this case as well?

Collapse
 
silasogar2 profile image
silasogar2

Great, thanks for sharing.

Collapse
 
codaelux profile image
Joseph Peculiar

Sure :), thanks for reading