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
Install Serverless globally if you haven't installed it already
npm i -g serverless
Now let's setup up the serverless-offline plugin
npm i -g serverless-offline
Install serverless-dynamodb-local plugin
npm i serverless-dynamodb-local
Update your serverless.yml file - add this to the bottom of your serverless.yml file
plugins:
- serverless-dynamodb-local
- serverless-offline
Initialize DynamoDB - this initializes DynamoDB offline in your project folder
sls dynamodb install
Run serverless:-
sls offline start
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
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
Now run
dynamodb-admin
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]
I published a great cheat sheet with all commands needed for setting up DynamoDB offline you can find it here
Top comments (3)
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?
Great, thanks for sharing.
Sure :), thanks for reading