In this post we'll see how we can run AWS DynamoDB locally using Docker and configure an ASP.NET Core application to use the local instance in development.
This Plugin Requires
- Serverless V0.5 or newer
- Java Runtime Engine (JRE) version 6.x or newer
Features
- Install DynamoDB Local
- Start DynamoDB Local with all the parameters supported (e.g port, inMemory, sharedDb)
- Create, Manage and Execute DynamoDB Migration Scripts(Table Creation/ Data Seeds) for DynamoDB Local and Online
Install Plugin
npm install --save serverless-dynamodb-local
Then in
s-project.json
add following entry to the plugins array: serverless-dynamodb-local
e.g 'plugins': ['serverless-dynamodb-local']
Using the Plugin
- Install DynamoDB Local
sls dynamodb install
- Start DynamoDB Local (DynamoDB will process incoming requests until you stop it. To stop DynamoDB, type Ctrl+C in the command prompt window). Make sure above command is executed before this.
sls dynamodb start
- Create/Execute DynamoDB (Migrations)
- Create a new migration file (Default directory path /dynamodb). Make sure DynamoDB Local is started in another shell.
sls dynamodb create -n <filename>
- Execute a single migration. Make sure DynamoDB Local is started in another shell.
sls dynamodb execute -n <filename>
- Execute all migrations for DynamoDB Local.
sls dynamodb executeAll
- Execute migration(s) in remote DynamoDB use additional parameters(region and stage) after execute/executeAll. e.g.
sls dynamodb executeAll -r us-west-1 -s dev
Note: Read the detailed section for more information on advanced options and configurations. Open a browser and go to the url http://localhost:8000/shell to access the web shell for dynamodb local.
Install: sls dynamodb install
To remove the installed dynamodb local, run:
sls dynamodb remove
Note: This is useful if the sls dynamodb install failed in between to completely remove and install a new copy of DynamoDB local.Start: sls dynamodb start
All CLI options are optional:
All the above options can be added to s-project.json to set default configuration: e.g
Migrations: sls dynamodb create/execute/executeAll
Configurations
In
s-project.json
add following to customize DynamoDB Migrations file directory and table prefixes/suffixesIn
s-project.json
add following to execute all the migration upon DynamoDB Local StartMigration Template
Before modifying the migration template, refer the (Dynamodb Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property and (Dynamodb Document Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property links.
Using DynamoDB Local in your code
You need to add the following parameters to the AWS NODE SDK dynamodb constructor
e.g. for dynamodb document client sdk
e.g. for dynamodb document client sdk
Using with serverless-offline plugin
When using this plugin with serverless-offline, it is difficult to use above syntax since the code should use DynamoDB Local for development, and use DynamoDB Online after provisioning in AWS. Therefore we suggest you to use serverless-dynamodb-client plugin in your code.