-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserverless.js
174 lines (141 loc) · 5.18 KB
/
serverless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
const { mergeDeepRight, pick } = require('ramda');
// eslint-disable-next-line import/no-extraneous-dependencies
const AWS = require('aws-sdk');
// eslint-disable-next-line import/no-unresolved
const { Component } = require('@serverless/core');
const {
log,
createTable,
deleteTable,
describeTable,
updateTable,
updateTimeToLive,
} = require('./utils');
const outputsList = ['name', 'arn', 'region'];
const defaults = {
attributeDefinitions: [
{
AttributeName: 'id',
AttributeType: 'S',
},
],
keySchema: [
{
AttributeName: 'id',
KeyType: 'HASH',
},
],
globalSecondaryIndexes: [],
localSecondaryIndexes: [],
name: null,
region: 'us-east-1',
deletionPolicy: 'delete',
timeToLiveSpecification: undefined,
};
class AwsDynamoDb extends Component {
async deploy(inputs = {}) {
// this error message assumes that the user is running via the CLI though...
if (Object.keys(this.credentials.aws).length === 0) {
const msg =
'Credentials not found. Make sure you have a .env file in the cwd. - Docs: https://git.io/JvArp';
throw new Error(msg);
}
const config = mergeDeepRight(defaults, inputs);
config.name = config.name || this.name;
// If first deploy and no name is found, set default name..
if (!config.name && !this.state.name) {
config.name = `dynamodb-table-${Math.random().toString(36).substring(6)}`;
this.state.name = config.name;
}
// If first deploy, and a name is set...
else if (config.name && !this.state.name) {
this.state.name = config.name;
}
// If subequent deploy, and name is different from a previously used name, throw error.
else if (config.name && this.state.name && config.name !== this.state.name) {
throw new Error(
'You cannot change the name of your DynamoDB table once it has been deployed (or this will deploy a new table). Please remove this Component Instance first by running "serverless remove", then redeploy it with "serverless deploy".'
);
}
console.log(`Starting deployment of table ${config.name} in the ${config.region} region.`);
const dynamodb = new AWS.DynamoDB({
region: config.region,
credentials: this.credentials.aws,
});
console.log(`Checking if table ${config.name} already exists in the ${config.region} region.`);
const prevTable = await describeTable({ dynamodb, name: config.name });
if (!prevTable) {
log(`Table ${config.name} does not exist. Creating...`);
config.arn = await createTable({ dynamodb, ...config });
} else {
console.log(`Table ${config.name} already exists. Comparing config changes...`);
// Check region
if (config.region && this.state.region && config.region !== this.state.region) {
throw new Error(
'You cannot change the region of a DynamoDB Table. Please remove it and redeploy in your desired region.'
);
}
config.arn = prevTable.arn;
const prevGlobalSecondaryIndexes = prevTable.globalSecondaryIndexes || [];
await updateTable.call(this, { dynamodb, prevGlobalSecondaryIndexes, ...config });
}
if (config.timeToLiveSpecification) {
await updateTimeToLive({ dynamodb, ...config });
}
log(`Table ${config.name} was successfully deployed to the ${config.region} region.`);
this.state.arn = config.arn;
this.state.name = config.name;
this.state.region = config.region;
this.state.deletionPolicy = config.deletionPolicy;
const outputs = pick(outputsList, config);
// Add indexes to outputs as objects, which are easier to reference as serverless variables
if (config.globalSecondaryIndexes) {
outputs.indexes = outputs.indexes || {};
config.globalSecondaryIndexes.forEach((index) => {
outputs.indexes[index.IndexName] = {
name: index.IndexName,
arn: `${outputs.arn}/index/${index.IndexName}`,
};
});
}
if (config.localSecondaryIndexes) {
outputs.indexes = outputs.indexes || {};
config.localSecondaryIndexes.forEach((index) => {
outputs.indexes[index.IndexName] = {
name: index.IndexName,
arn: `${outputs.arn}/index/${index.IndexName}`,
};
});
}
return outputs;
}
/**
* Remove
*/
async remove() {
console.log('Removing');
// If "delete: false", don't delete the table, and warn instead
if (this.state.deletionPolicy && this.state.deletionPolicy === 'retain') {
console.log('Skipping table removal because "deletionPolicy" is set to "retain".');
this.state = {};
return {};
}
const { name, region } = this.state;
if (!name) {
console.log('Aborting removal. Table name not found in state.');
return null;
}
const dynamodb = new AWS.DynamoDB({
region,
credentials: this.credentials.aws,
});
console.log(`Removing table ${name} from the ${region} region.`);
await deleteTable({ dynamodb, name });
const outputs = pick(outputsList, this.state);
console.log(`Table ${name} was successfully removed from the ${region} region.`);
this.state = {};
return outputs;
}
}
module.exports = AwsDynamoDb;