Serverless DynamoDB Local

Serverless Dynamodb Local Plugin - Allows to run dynamodb locally for serverless

user  

99xt

serverless-dynamodb-local

Join the chat at https://gitter.im/99xt/serverless-dynamodb-local serverless npm version license

For serverless@v1-rc.1 installation and documentation please see v1 branch.

Following instructions are for serverless@0.5.x

This Plugin Requires

  • Serverless V0.5.x
  • 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@0.2.10

Then in s-project.json add following entry to the plugins array: serverless-dynamodb-local e.g "plugins": ["serverless-dynamodb-local"]

Using the Plugin

1) Install DynamoDB Local sls dynamodb install

2) 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

3) 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:

--port                    -p  Port to listen on. Default: 8000
--cors                    -c  Enable CORS support (cross-origin resource sharing) for JavaScript. You must provide a comma-separated "allow" list of specific domains. The default setting for -cors is an asterisk (*), which allows public access.
--inMemory                -i  DynamoDB; will run in memory, instead of using a database file. When you stop DynamoDB;, none of the data will be saved. Note that you cannot specify both -dbPath and -inMemory at once.
--dbPath                  -d  The directory where DynamoDB will write its database file. If you do not specify this option, the file will be written to the current directory. Note that you cannot specify both -dbPath and -inMemory at once. For the path, current working directory is <projectroot>/node_modules/serverless-dynamodb-local/dynamob. For example to create <projectroot>/node_modules/serverless-dynamodb-local/dynamob/<mypath> you should specify -d <mypath>/ or --dbPath <mypath>/ with a forwardslash at the end.
--sharedDb                -h  DynamoDB will use a single database file, instead of using separate files for each credential and region. If you specify -sharedDb, all DynamoDB clients will interact with the same set of tables regardless of their region and credential configuration.
--delayTransientStatuses  -t  Causes DynamoDB to introduce delays for certain operations. DynamoDB can perform some tasks almost instantaneously, such as create/update/delete operations on tables and indexes; however, the actual DynamoDB service requires more time for these tasks. Setting this parameter helps DynamoDB simulate the behavior of the Amazon DynamoDB web service more closely. (Currently, this parameter introduces delays only for global secondary indexes that are in either CREATING or DELETING status.)
--optimizeDbBeforeStartup -o  Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.
--migration               -m  After starting dynamodb local, run dynamodb migrations.

All the above options can be added to s-project.json to set default configuration: e.g

"custom": {
  "dynamodb": {
    "start": {
      "port": "8000",
      "inMemory": true,
      "migration": true
    }
  }
}

Migrations: sls dynamodb

Migration-Commands create, execute, executeAll

Configurations

In s-project.json add following to customize DynamoDB Migrations file directory and table prefixes/suffixes

"custom": {
  "dynamodb": {
    "migration": {
      "dir": "dynamodbMigrations",
      "table_prefix": "",
      "table_suffix": ""
    }
  }
}

In s-project.json add following to execute all the migration upon DynamoDB Local Start

"custom": {
  "dynamodb": {
    "start": {
      "migration": true
    }
  }
}

For Migration-Commands execute/executeAll following optional parameters can be used

--region                  -r  Region that dynamodb should be remotely executed.
--stage                   -s  Stage that dynamodb should be remotely executed.
--table_prefix            -t  Dynamodb Table name prefixs (E.g. for prefix = production- and abstract table-name = users, after adding the prefix it will be production-users)
--table_suffix            -x  Table name suffix (E.g for suffix = -test and abstract table-name = users, after adding the suffix it will be users-test )
--profile                 -p  Use another AWS Profile to execute migration
--name                    -n  Execute a migration template with the given name (This is only for execute command and not applicable for executeAll).

Migration Template

{
    "Table": {
        "TableName": "TableName",
        "KeySchema": [{
            "AttributeName": "attr_1",
            "KeyType": "HASH"
		}, {
            "AttributeName": "attr_2",
            "KeyType": "RANGE"
		}],
        "AttributeDefinitions": [{
            "AttributeName": "attr_1",
            "AttributeType": "S"
		}, {
            "AttributeName": "attr_2",
            "AttributeType": "S"
		}],
        "LocalSecondaryIndexes": [{
            "IndexName": "local_index_1",
            "KeySchema": [{
                "AttributeName": "attr_1",
                "KeyType": "HASH"
			}, {
                "AttributeName": "attr_2",
                "KeyType": "RANGE"
			}],
            "Projection": {
                "NonKeyAttributes": ["attr_1", "attr_2"],
                "ProjectionType": "INCLUDE"
            }
		}],
        "GlobalSecondaryIndexes": [{
            "IndexName": "global_index_1",
            "KeySchema": [{
                "AttributeName": "attr_1",
                "KeyType": "HASH"
			}, {
                "AttributeName": "attr_2",
                "KeyType": "RANGE"
			}],
            "Projection": {
                "NonKeyAttributes": ["attr_1", "attr_2"],
                "ProjectionType": "INCLUDE"
            },
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 1,
                "WriteCapacityUnits": 1
            }
		}],
        "ProvisionedThroughput": {
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        }
    },
    "Seeds": [{
        "attr_1": "attr_1_value",
        "attr_2": "attr_2_value"
    }]
}

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

var AWS = require('aws-sdk');
new AWS.DynamoDB.DocumentClient({
    region: 'localhost',
    endpoint: 'http://localhost:8000'
})

e.g. for dynamodb document client sdk

new AWS.DynamoDB({
    region: 'localhost',
    endpoint: 'http://localhost:8000'
})

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.

Links

License

MIT

view on Github

Latest commit b2f54ec on Sep 24, 2017

New to serverless?

To get started, pop open your terminal & run:

npm install serverless -g