How to run DynamoDB Offline

codaelux

Joseph Peculiar

Posted on March 23, 2020

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

💖 💪 🙅 🚩
codaelux
Joseph Peculiar

Posted on March 23, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to run DynamoDB Offline
serverless How to run DynamoDB Offline

March 23, 2020