Skip to content

Commit 7edf0c9

Browse files
author
Imran
committed
add the option to disable the plugin
1 parent 5386a7f commit 7edf0c9

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ DynamoDB currently imposes some strict limitations when [creating global tables]
77
- The table names must match
88
- Streams must be enabled
99
- All tables must be empty
10-
- If a replica table is removed from a global table it can not be readded. You can however drop the table and recreate it to add it to global replication.
10+
- If a replica table is removed from a global table it can not be added again. You can however drop the table and recreate it to add it to global replication.
1111

1212
The plugin handles these limitation by enabling global replication for all tables defined in the serverless stack and thus tying the global tables lifecycle to the table resources defined in the stack. Adding and removing tables from the stack will add and remove them from replication.
1313

@@ -33,4 +33,16 @@ serverless deploy --region us-east-1
3333
serverless deploy --region eu-west-1
3434
```
3535

36-
When you add/remove table resources from your serverless stack they will also be added/removed from global replication but you will need to redeploy your stack to each region.
36+
When you add/remove table resources from your serverless stack they will also be added/removed from global replication but you will need to redeploy your stack to each region.
37+
38+
## Disabling
39+
40+
The plugin is enabled by default. You can disable the plugin via config. You may want to do this in certain environments for example.
41+
42+
```yml
43+
custom:
44+
createDynamoDBGlobalTables:
45+
enabled: ${self:custom.variables.${self:custom.variables.stage}.enabled}
46+
```
47+
48+
Disabling the plugin after deploying the stack will have no effect on global tables that were previously deployed. This is due to the previously mentioned limitations of dyanmodb global tables.

package-lock.json

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"aws-sdk": "^2.382.0",
2525
"chalk": "^2.4.1",
26+
"lodash.get": "^4.4.2",
2627
"lodash.values": "^4.3.0"
2728
},
2829
"devDependencies": {

src/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
const AWS = require('aws-sdk');
22
const _values = require('lodash.values');
3+
const _get = require('lodash.get');
34
const chalk = require('chalk');
45

56
class CreateDynamoDBGlobalTables {
67
constructor(serverless, options) {
78
this.serverless = serverless;
89
this.options = options;
10+
this.enabled = _get(serverless, 'service.custom.dynamoDBGlobalTables.enabled', true);
911
this.hooks = {
1012
'after:deploy:deploy': this.createGlobalTables.bind(this),
1113
};
1214
}
1315

1416
async createGlobalTables() {
17+
if(this.enabled == false) {
18+
this.log('Plugin disabled');
19+
return;
20+
}
21+
1522
const provider = this.serverless.getProvider('aws');
1623
const region = provider.getRegion();
1724
const tableNames = this.getTableNames();

test/creating-global-tables.js

+34
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ describe('Creating a global dynamodb tables', () => {
159159
plugin.createGlobalTables().should.be.rejected;
160160
});
161161
});
162+
163+
describe('when the plugin is disabled', () => {
164+
165+
before(async () => {
166+
given_aws_dynamodb_is_mocked();
167+
serverless = given_the_plugin_is_disabled();
168+
plugin = new CreateDynamoDBGlobalTables(serverless);
169+
await plugin.createGlobalTables();
170+
});
171+
172+
after(() => {
173+
AWSMock.restore();
174+
});
175+
176+
it('does not create any global tables', () => {
177+
createGlobalTables.should.not.have.been.called;
178+
});
179+
180+
it('does not add any replicas', () => {
181+
addReplicas.should.not.have.been.called;
182+
});
183+
});
162184
});
163185

164186
const given_aws_dynamodb_is_mocked = () => {
@@ -201,4 +223,16 @@ const given_a_serverless_stack_with_some_tables = () => ({
201223
}
202224
}
203225
}
226+
});
227+
228+
const given_the_plugin_is_disabled = () => ({
229+
getProvider: () => ({ getRegion: () => 'eu-west-2' }),
230+
cli: { consoleLog: () => { } },
231+
service: {
232+
custom: {
233+
dynamoDBGlobalTables : {
234+
enabled: false
235+
}
236+
}
237+
}
204238
});

0 commit comments

Comments
 (0)