/
deployment-group.ts
292 lines (257 loc) · 9.85 KB
/
deployment-group.ts
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import iam = require('@aws-cdk/aws-iam');
import lambda = require('@aws-cdk/aws-lambda');
import cdk = require('@aws-cdk/core');
import { CfnDeploymentGroup } from '../codedeploy.generated';
import { AutoRollbackConfig } from '../rollback-config';
import { arnForDeploymentGroup, renderAlarmConfiguration, renderAutoRollbackConfiguration } from '../utils';
import { ILambdaApplication, LambdaApplication } from './application';
import { ILambdaDeploymentConfig, LambdaDeploymentConfig } from './deployment-config';
/**
* Interface for a Lambda deployment groups.
*/
export interface ILambdaDeploymentGroup extends cdk.IResource {
/**
* The reference to the CodeDeploy Lambda Application that this Deployment Group belongs to.
*/
readonly application: ILambdaApplication;
/**
* The physical name of the CodeDeploy Deployment Group.
* @attribute
*/
readonly deploymentGroupName: string;
/**
* The ARN of this Deployment Group.
* @attribute
*/
readonly deploymentGroupArn: string;
/**
* The Deployment Configuration this Group uses.
*/
readonly deploymentConfig: ILambdaDeploymentConfig;
}
/**
* Construction properties for {@link LambdaDeploymentGroup}.
*/
export interface LambdaDeploymentGroupProps {
/**
* The reference to the CodeDeploy Lambda Application that this Deployment Group belongs to.
*
* @default - One will be created for you.
*/
readonly application?: ILambdaApplication;
/**
* The physical, human-readable name of the CodeDeploy Deployment Group.
*
* @default - An auto-generated name will be used.
*/
readonly deploymentGroupName?: string;
/**
* The Deployment Configuration this Deployment Group uses.
*
* @default LambdaDeploymentConfig.CANARY_10PERCENT_5MINUTES
*/
readonly deploymentConfig?: ILambdaDeploymentConfig;
/**
* The CloudWatch alarms associated with this Deployment Group.
* CodeDeploy will stop (and optionally roll back)
* a deployment if during it any of the alarms trigger.
*
* Alarms can also be added after the Deployment Group is created using the {@link #addAlarm} method.
*
* @default []
* @see https://docs.aws.amazon.com/codedeploy/latest/userguide/monitoring-create-alarms.html
*/
readonly alarms?: cloudwatch.IAlarm[];
/**
* The service Role of this Deployment Group.
*
* @default - A new Role will be created.
*/
readonly role?: iam.IRole;
/**
* Lambda Alias to shift traffic. Updating the version
* of the alias will trigger a CodeDeploy deployment.
*
* [disable-awslint:ref-via-interface] since we need to modify the alias CFN resource update policy
*/
readonly alias: lambda.Alias;
/**
* The Lambda function to run before traffic routing starts.
*
* @default - None.
*/
readonly preHook?: lambda.IFunction;
/**
* The Lambda function to run after traffic routing starts.
*
* @default - None.
*/
readonly postHook?: lambda.IFunction;
/**
* Whether to continue a deployment even if fetching the alarm status from CloudWatch failed.
*
* @default false
*/
readonly ignorePollAlarmsFailure?: boolean;
/**
* The auto-rollback configuration for this Deployment Group.
*
* @default - default AutoRollbackConfig.
*/
readonly autoRollback?: AutoRollbackConfig;
}
/**
* @resource AWS::CodeDeploy::DeploymentGroup
*/
export class LambdaDeploymentGroup extends cdk.Resource implements ILambdaDeploymentGroup {
/**
* Import an Lambda Deployment Group defined either outside the CDK app, or in a different AWS region.
*
* @param scope the parent Construct for this new Construct
* @param id the logical ID of this new Construct
* @param attrs the properties of the referenced Deployment Group
* @returns a Construct representing a reference to an existing Deployment Group
*/
public static fromLambdaDeploymentGroupAttributes(
scope: cdk.Construct,
id: string,
attrs: LambdaDeploymentGroupAttributes): ILambdaDeploymentGroup {
return new ImportedLambdaDeploymentGroup(scope, id, attrs);
}
public readonly application: ILambdaApplication;
public readonly deploymentGroupName: string;
public readonly deploymentGroupArn: string;
public readonly deploymentConfig: ILambdaDeploymentConfig;
public readonly role: iam.IRole;
private readonly alarms: cloudwatch.IAlarm[];
private preHook?: lambda.IFunction;
private postHook?: lambda.IFunction;
constructor(scope: cdk.Construct, id: string, props: LambdaDeploymentGroupProps) {
super(scope, id, {
physicalName: props.deploymentGroupName,
});
this.application = props.application || new LambdaApplication(this, 'Application');
this.alarms = props.alarms || [];
this.role = props.role || new iam.Role(this, 'ServiceRole', {
assumedBy: new iam.ServicePrincipal('codedeploy.amazonaws.com')
});
this.role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSCodeDeployRoleForLambda'));
this.deploymentConfig = props.deploymentConfig || LambdaDeploymentConfig.CANARY_10PERCENT_5MINUTES;
const resource = new CfnDeploymentGroup(this, 'Resource', {
applicationName: this.application.applicationName,
serviceRoleArn: this.role.roleArn,
deploymentGroupName: this.physicalName,
deploymentConfigName: this.deploymentConfig.deploymentConfigName,
deploymentStyle: {
deploymentType: 'BLUE_GREEN',
deploymentOption: 'WITH_TRAFFIC_CONTROL'
},
alarmConfiguration: cdk.Lazy.anyValue({ produce: () => renderAlarmConfiguration(this.alarms, props.ignorePollAlarmsFailure) }),
autoRollbackConfiguration: cdk.Lazy.anyValue({ produce: () => renderAutoRollbackConfiguration(this.alarms, props.autoRollback) }),
});
this.deploymentGroupName = this.getResourceNameAttribute(resource.ref);
this.deploymentGroupArn = this.getResourceArnAttribute(arnForDeploymentGroup(this.application.applicationName, resource.ref), {
service: 'codedeploy',
resource: 'deploymentgroup',
resourceName: `${this.application.applicationName}/${this.physicalName}`,
sep: ':',
});
if (props.preHook) {
this.addPreHook(props.preHook);
}
if (props.postHook) {
this.addPostHook(props.postHook);
}
(props.alias.node.defaultChild as lambda.CfnAlias).cfnOptions.updatePolicy = {
codeDeployLambdaAliasUpdate: {
applicationName: this.application.applicationName,
deploymentGroupName: resource.ref,
beforeAllowTrafficHook: cdk.Lazy.stringValue({ produce: () => this.preHook && this.preHook.functionName }),
afterAllowTrafficHook: cdk.Lazy.stringValue({ produce: () => this.postHook && this.postHook.functionName }),
}
};
}
/**
* Associates an additional alarm with this Deployment Group.
*
* @param alarm the alarm to associate with this Deployment Group
*/
public addAlarm(alarm: cloudwatch.IAlarm): void {
this.alarms.push(alarm);
}
/**
* Associate a function to run before deployment begins.
* @param preHook function to run before deployment beings
* @throws an error if a pre-hook function is already configured
*/
public addPreHook(preHook: lambda.IFunction): void {
if (this.preHook !== undefined) {
throw new Error('A pre-hook function is already defined for this deployment group');
}
this.preHook = preHook;
this.grantPutLifecycleEventHookExecutionStatus(this.preHook);
this.preHook.grantInvoke(this.role);
}
/**
* Associate a function to run after deployment completes.
* @param postHook function to run after deployment completes
* @throws an error if a post-hook function is already configured
*/
public addPostHook(postHook: lambda.IFunction): void {
if (this.postHook !== undefined) {
throw new Error('A post-hook function is already defined for this deployment group');
}
this.postHook = postHook;
this.grantPutLifecycleEventHookExecutionStatus(this.postHook);
this.postHook.grantInvoke(this.role);
}
/**
* Grant a principal permission to codedeploy:PutLifecycleEventHookExecutionStatus
* on this deployment group resource.
* @param grantee to grant permission to
*/
public grantPutLifecycleEventHookExecutionStatus(grantee: iam.IGrantable): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
resourceArns: [this.deploymentGroupArn],
actions: ['codedeploy:PutLifecycleEventHookExecutionStatus'],
});
}
}
/**
* Properties of a reference to a CodeDeploy Lambda Deployment Group.
*
* @see LambdaDeploymentGroup#fromLambdaDeploymentGroupAttributes
*/
export interface LambdaDeploymentGroupAttributes {
/**
* The reference to the CodeDeploy Lambda Application
* that this Deployment Group belongs to.
*/
readonly application: ILambdaApplication;
/**
* The physical, human-readable name of the CodeDeploy Lambda Deployment Group
* that we are referencing.
*/
readonly deploymentGroupName: string;
/**
* The Deployment Configuration this Deployment Group uses.
*
* @default LambdaDeploymentConfig.CANARY_10PERCENT_5MINUTES
*/
readonly deploymentConfig?: ILambdaDeploymentConfig;
}
class ImportedLambdaDeploymentGroup extends cdk.Resource implements ILambdaDeploymentGroup {
public readonly application: ILambdaApplication;
public readonly deploymentGroupName: string;
public readonly deploymentGroupArn: string;
public readonly deploymentConfig: ILambdaDeploymentConfig;
constructor(scope: cdk.Construct, id: string, props: LambdaDeploymentGroupAttributes) {
super(scope, id);
this.application = props.application;
this.deploymentGroupName = props.deploymentGroupName;
this.deploymentGroupArn = arnForDeploymentGroup(props.application.applicationName, props.deploymentGroupName);
this.deploymentConfig = props.deploymentConfig || LambdaDeploymentConfig.CANARY_10PERCENT_5MINUTES;
}
}