Skip to content

Commit 42bf35e

Browse files
authored
Merge pull request #72 from msjonker/serverless-config
DynamoDB tables created directly from Resources declaration
2 parents 120c46a + 5ad35d6 commit 42bf35e

File tree

3 files changed

+93
-211
lines changed

3 files changed

+93
-211
lines changed

README.md

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ serverless-dynamodb-local
1313
## Features
1414
* Install DynamoDB Local
1515
* Start DynamoDB Local with all the parameters supported (e.g port, inMemory, sharedDb)
16-
* Create, Manage and Execute DynamoDB Migration Scripts(Table Creation/ Data Seeds) for DynamoDB Local and Online
16+
* Table Creation for DynamoDB Local
1717

1818
## Install Plugin
1919
`npm install --save serverless-dynamodb-local`
@@ -28,18 +28,11 @@ plugins:
2828
1) Install DynamoDB Local
2929
`sls dynamodb install`
3030

31-
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.
32-
`sls dynamodb start`
31+
2) Add DynamoDB Resource definitions to your Serverless configuration, as defined here: https://serverless.com/framework/docs/providers/aws/guide/resources/#configuration
3332

34-
3) Create/Execute DynamoDB (Migrations)
35-
* Create a new migration file (Default directory path /dynamodb). Make sure DynamoDB Local is started in another shell.
36-
`sls dynamodb create -n <filename>`
33+
3) Start DynamoDB Local and migrate (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.
34+
`sls dynamodb start --migrate`
3735

38-
* Execute a single migration. Make sure DynamoDB Local is started in another shell.
39-
`sls dynamodb execute -n <filename>`
40-
41-
* Execute all migrations on the remote dynamodb.
42-
`sls dynamodb executeAll`
4336

4437
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.
4538

@@ -59,7 +52,7 @@ All CLI options are optional:
5952
--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.
6053
--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.)
6154
--optimizeDbBeforeStartup -o Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.
62-
--migration -m After starting dynamodb local, run dynamodb migrations.
55+
--migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration..
6356
```
6457

6558
All the above options can be added to serverless.yml to set default configuration: e.g.
@@ -71,92 +64,35 @@ custom:
7164
port: 8000
7265
inMemory: true
7366
migration: true
74-
migration:
75-
dir: ./offline/migrations
7667
```
7768

78-
## Migrations: sls dynamodb create/execute/executeAll
69+
## Migrations: sls dynamodb migrate
7970
### Configurations
80-
In `serverless.yml` add following to customize DynamoDB Migrations file directory and table prefixes/suffixes
81-
```yml
82-
custom:
83-
dynamodb:
84-
migration:
85-
dir: dynamodbMigrations
86-
table_prefix: prefix
87-
table_suffix": suffix
88-
```
89-
9071
In `serverless.yml` add following to execute all the migration upon DynamoDB Local Start
9172
```yml
9273
custom:
9374
dynamodb:
9475
start:
95-
migration: true
76+
migrate: true
9677
```
97-
### Migration Template
98-
```json
99-
{
100-
"Table": {
101-
"TableName": "TableName",
102-
"KeySchema": [{
103-
"AttributeName": "attr_1",
104-
"KeyType": "HASH"
105-
}, {
106-
"AttributeName": "attr_2",
107-
"KeyType": "RANGE"
108-
}],
109-
"AttributeDefinitions": [{
110-
"AttributeName": "attr_1",
111-
"AttributeType": "S"
112-
}, {
113-
"AttributeName": "attr_2",
114-
"AttributeType": "S"
115-
}],
116-
"LocalSecondaryIndexes": [{
117-
"IndexName": "local_index_1",
118-
"KeySchema": [{
119-
"AttributeName": "attr_1",
120-
"KeyType": "HASH"
121-
}, {
122-
"AttributeName": "attr_2",
123-
"KeyType": "RANGE"
124-
}],
125-
"Projection": {
126-
"NonKeyAttributes": ["attr_1", "attr_2"],
127-
"ProjectionType": "INCLUDE"
128-
}
129-
}],
130-
"GlobalSecondaryIndexes": [{
131-
"IndexName": "global_index_1",
132-
"KeySchema": [{
133-
"AttributeName": "attr_1",
134-
"KeyType": "HASH"
135-
}, {
136-
"AttributeName": "attr_2",
137-
"KeyType": "RANGE"
138-
}],
139-
"Projection": {
140-
"NonKeyAttributes": ["attr_1", "attr_2"],
141-
"ProjectionType": "INCLUDE"
142-
},
143-
"ProvisionedThroughput": {
144-
"ReadCapacityUnits": 1,
145-
"WriteCapacityUnits": 1
146-
}
147-
}],
148-
"ProvisionedThroughput": {
149-
"ReadCapacityUnits": 1,
150-
"WriteCapacityUnits": 1
151-
}
152-
},
153-
"Seeds": [{
154-
"attr_1": "attr_1_value",
155-
"attr_2": "attr_2_value"
156-
}]
157-
}
78+
### AWS::DynamoDB::Table Resource Template for serverless.yml
79+
```yml
80+
resources:
81+
Resources:
82+
usersTable:
83+
Type: AWS::DynamoDB::Table
84+
Properties:
85+
TableName: usersTable
86+
AttributeDefinitions:
87+
- AttributeName: email
88+
AttributeType: S
89+
KeySchema:
90+
- AttributeName: email
91+
KeyType: HASH
92+
ProvisionedThroughput:
93+
ReadCapacityUnits: 1
94+
WriteCapacityUnits: 1
15895
```
159-
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.
16096

16197
## Using DynamoDB Local in your code
16298
You need to add the following parameters to the AWS NODE SDK dynamodb constructor
@@ -205,4 +141,4 @@ Now your local DynamoDB database will be automatically started before running `s
205141
* [NPM Registry](https://www.npmjs.com/package/serverless-dynamodb-local)
206142

207143
## License
208-
[MIT](LICENSE)
144+
[MIT](LICENSE)

0 commit comments

Comments
 (0)