forked from davidgf/serverless-plugin-canary-deployments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless-plugin-canary-deployments.js
212 lines (190 loc) · 7.85 KB
/
serverless-plugin-canary-deployments.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const _ = require('lodash/fp');
const flattenObject = require('flat');
const CfGenerators = require('./lib/CfTemplateGenerators');
class ServerlessCanaryDeployments {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.awsProvider = this.serverless.getProvider('aws');
this.naming = this.awsProvider.naming;
this.service = this.serverless.service;
this.hooks = {
'before:package:finalize': this.addCanaryDeploymentResources.bind(this)
};
}
get codeDeployAppName() {
const stackName = this.naming.getStackName();
const normalizedStackName = this.naming.normalizeNameToAlphaNumericOnly(stackName);
return `${normalizedStackName}DeploymentApplication`;
}
get compiledTpl() {
return this.service.provider.compiledCloudFormationTemplate;
}
get withDeploymentPreferencesFns() {
return this.serverless.service.getAllFunctions()
.filter(name => _.has('deploymentSettings', this.service.getFunction(name)));
}
addCanaryDeploymentResources() {
if (this.withDeploymentPreferencesFns.length > 0) {
const codeDeployApp = this.buildCodeDeployApp();
const codeDeployRole = this.buildCodeDeployRole();
const functionsResources = this.buildFunctionsResources();
Object.assign(
this.compiledTpl.Resources,
codeDeployApp,
codeDeployRole,
...functionsResources
);
}
}
buildFunctionsResources() {
return _.flatMap(
serverlessFunction => this.buildFunctionResources(serverlessFunction),
this.withDeploymentPreferencesFns
);
}
buildFunctionResources(serverlessFnName) {
const functionName = this.naming.getLambdaLogicalId(serverlessFnName);
const deploymentSettings = this.getDeploymentSettingsFor(serverlessFnName);
const deploymentGrTpl = this.buildFunctionDeploymentGroup({ deploymentSettings, functionName });
const deploymentGroup = this.getResourceLogicalName(deploymentGrTpl);
const aliasTpl = this.buildFunctionAlias({ deploymentSettings, functionName, deploymentGroup });
const functionAlias = this.getResourceLogicalName(aliasTpl);
const lambdaPermissions = this.buildPermissionsForAlias({ functionName, functionAlias });
const eventsWithAlias = this.buildEventsForAlias({ functionName, functionAlias });
return [deploymentGrTpl, aliasTpl, ...lambdaPermissions, ...eventsWithAlias];
}
buildCodeDeployApp() {
const logicalName = this.codeDeployAppName;
const template = CfGenerators.codeDeploy.buildApplication();
return { [logicalName]: template };
}
buildCodeDeployRole() {
const logicalName = 'CodeDeployServiceRole';
const template = CfGenerators.iam.buildCodeDeployRole();
return { [logicalName]: template };
}
buildFunctionDeploymentGroup({ deploymentSettings, functionName }) {
const logicalName = `${functionName}DeploymentGroup`;
const params = {
codeDeployAppName: this.codeDeployAppName,
deploymentSettings
};
const template = CfGenerators.codeDeploy.buildFnDeploymentGroup(params);
return { [logicalName]: template };
}
buildFunctionAlias({ deploymentSettings = {}, functionName, deploymentGroup }) {
const { alias } = deploymentSettings;
const functionVersion = this.getVersionNameFor(functionName);
const logicalName = `${functionName}Alias${alias}`;
const beforeHook = this.naming.getLambdaLogicalId(deploymentSettings.preTrafficHook);
const afterHook = this.naming.getLambdaLogicalId(deploymentSettings.postTrafficHook);
const trafficShiftingSettings = {
codeDeployApp: this.codeDeployAppName,
deploymentGroup,
afterHook,
beforeHook
};
const template = CfGenerators.lambda.buildAlias({
alias,
functionName,
functionVersion,
trafficShiftingSettings
});
return { [logicalName]: template };
}
buildPermissionsForAlias({ functionName, functionAlias }) {
const permissions = this.getLambdaPermissionsFor(functionName);
return Object.entries(permissions).map(([logicalName, template]) => {
const templateWithAlias = CfGenerators.lambda.replacePermissionFunctionWithAlias(template, functionAlias);
return { [logicalName]: templateWithAlias };
});
}
buildEventsForAlias({ functionName, functionAlias }) {
const replaceAliasStrategy = {
'AWS::Lambda::EventSourceMapping': CfGenerators.lambda.replaceEventMappingFunctionWithAlias,
'AWS::ApiGateway::Method': CfGenerators.apiGateway.replaceMethodUriWithAlias,
'AWS::SNS::Topic': CfGenerators.sns.replaceTopicSubscriptionFunctionWithAlias
};
const functionEvents = this.getEventsFor(functionName);
const functionEventsEntries = Object.entries(functionEvents);
const eventsWithAlias = functionEventsEntries.map(([logicalName, event]) => {
const evt = replaceAliasStrategy[event.Type](event, functionAlias, functionName);
return { [logicalName]: evt };
});
return eventsWithAlias;
}
getEventsFor(functionName) {
const apiGatewayMethods = this.getApiGatewayMethodsFor(functionName);
const eventSourceMappings = this.getEventSourceMappingsFor(functionName);
const snsTopics = this.getSnsTopicsFor(functionName);
return Object.assign({}, apiGatewayMethods, eventSourceMappings, snsTopics);
}
getApiGatewayMethodsFor(functionName) {
const isApiGMethod = _.matchesProperty('Type', 'AWS::ApiGateway::Method');
const isMethodForFunction = _.pipe(
_.prop('Properties.Integration'),
flattenObject,
_.includes(functionName)
);
const getMethodsForFunction = _.pipe(
_.pickBy(isApiGMethod),
_.pickBy(isMethodForFunction)
);
return getMethodsForFunction(this.compiledTpl.Resources);
}
getEventSourceMappingsFor(functionName) {
const isEventSourceMapping = _.matchesProperty('Type', 'AWS::Lambda::EventSourceMapping');
const isMappingForFunction = _.pipe(
_.prop('Properties.FunctionName'),
flattenObject,
_.includes(functionName)
);
const getMappingsForFunction = _.pipe(
_.pickBy(isEventSourceMapping),
_.pickBy(isMappingForFunction)
);
return getMappingsForFunction(this.compiledTpl.Resources);
}
getSnsTopicsFor(functionName) {
const isEventSourceMapping = _.matchesProperty('Type', 'AWS::SNS::Topic');
const isMappingForFunction = _.pipe(
_.prop('Properties.Subscription'),
_.map(_.prop('Endpoint.Fn::GetAtt')),
_.flatten,
_.includes(functionName)
);
const getMappingsForFunction = _.pipe(
_.pickBy(isEventSourceMapping),
_.pickBy(isMappingForFunction)
);
return getMappingsForFunction(this.compiledTpl.Resources);
}
getVersionNameFor(functionName) {
const isLambdaVersion = _.matchesProperty('Type', 'AWS::Lambda::Version');
const isVersionForFunction = _.matchesProperty('Properties.FunctionName.Ref', functionName);
const getVersionNameForFunction = _.pipe(
_.pickBy(isLambdaVersion),
_.findKey(isVersionForFunction)
);
return getVersionNameForFunction(this.compiledTpl.Resources);
}
getLambdaPermissionsFor(functionName) {
const isLambdaPermission = _.matchesProperty('Type', 'AWS::Lambda::Permission');
const isPermissionForFunction = _.matchesProperty('Properties.FunctionName.Fn::GetAtt[0]', functionName);
const getPermissionForFunction = _.pipe(
_.pickBy(isLambdaPermission),
_.pickBy(isPermissionForFunction)
);
return getPermissionForFunction(this.compiledTpl.Resources);
}
getResourceLogicalName(resource) {
return _.head(_.keys(resource));
}
getDeploymentSettingsFor(serverlessFunction) {
const globalSettings = _.cloneDeep(this.service.custom.deploymentSettings);
const fnDeploymentSetting = this.service.getFunction(serverlessFunction).deploymentSettings;
return Object.assign({}, globalSettings, fnDeploymentSetting);
}
}
module.exports = ServerlessCanaryDeployments;