/
environment.ts
546 lines (474 loc) · 17.3 KB
/
environment.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import { Construct } from 'constructs';
import { CfnDeployment, CfnEnvironment } from './appconfig.generated';
import { IApplication } from './application';
import { IConfiguration } from './configuration';
import { ActionPoint, IEventDestination, ExtensionOptions, IExtension, IExtensible, ExtensibleBase } from './extension';
import { getHash } from './private/hash';
import * as cloudwatch from '../../aws-cloudwatch';
import * as iam from '../../aws-iam';
import { Resource, IResource, Stack, ArnFormat, PhysicalName, Names } from '../../core';
/**
* Attributes of an existing AWS AppConfig environment to import it.
*/
export interface EnvironmentAttributes {
/**
* The application associated with the environment.
*/
readonly application: IApplication;
/**
* The ID of the environment.
*/
readonly environmentId: string;
/**
* The name of the environment.
*
* @default - None.
*/
readonly name?: string;
/**
* The description of the environment.
*
* @default - None.
*/
readonly description?: string;
/**
* The monitors for the environment.
*
* @default - None.
*/
readonly monitors?: Monitor[];
}
abstract class EnvironmentBase extends Resource implements IEnvironment, IExtensible {
public abstract applicationId: string;
public abstract environmentId: string;
public abstract environmentArn: string;
public abstract name?: string;
protected extensible!: ExtensibleBase;
protected deploymentQueue: Array<CfnDeployment> = [];
public addDeployment(configuration: IConfiguration): void {
if (this.name === undefined) {
throw new Error('Environment name must be known to add a Deployment');
}
const queueSize = this.deploymentQueue.push(
new CfnDeployment(configuration, `Deployment${getHash(this.name)}`, {
applicationId: configuration.application.applicationId,
configurationProfileId: configuration.configurationProfileId,
deploymentStrategyId: configuration.deploymentStrategy!.deploymentStrategyId,
environmentId: this.environmentId,
configurationVersion: configuration.versionNumber!,
description: configuration.description,
kmsKeyIdentifier: configuration.deploymentKey?.keyArn,
}),
);
// This internal member is used to keep track of configuration deployments
// as they are requested. Each element in this queue will depend on its
// predecessor, ensuring that the deployments occur sequentially in Cfn.
if (queueSize > 1) {
this.deploymentQueue[queueSize - 1].addDependency(this.deploymentQueue[queueSize - 2]);
}
}
public addDeployments(...configurations: IConfiguration[]): void {
configurations.forEach((config) => this.addDeployment(config));
}
public on(actionPoint: ActionPoint, eventDestination: IEventDestination, options?: ExtensionOptions) {
this.extensible.on(actionPoint, eventDestination, options);
}
public preCreateHostedConfigurationVersion(eventDestination: IEventDestination, options?: ExtensionOptions) {
this.extensible.preCreateHostedConfigurationVersion(eventDestination, options);
}
public preStartDeployment(eventDestination: IEventDestination, options?: ExtensionOptions) {
this.extensible.preStartDeployment(eventDestination, options);
}
public onDeploymentStart(eventDestination: IEventDestination, options?: ExtensionOptions) {
this.extensible.onDeploymentStart(eventDestination, options);
}
public onDeploymentStep(eventDestination: IEventDestination, options?: ExtensionOptions) {
this.extensible.onDeploymentStep(eventDestination, options);
}
public onDeploymentBaking(eventDestination: IEventDestination, options?: ExtensionOptions) {
this.extensible.onDeploymentBaking(eventDestination, options);
}
public onDeploymentComplete(eventDestination: IEventDestination, options?: ExtensionOptions) {
this.extensible.onDeploymentComplete(eventDestination, options);
}
public onDeploymentRolledBack(eventDestination: IEventDestination, options?: ExtensionOptions) {
this.extensible.onDeploymentRolledBack(eventDestination, options);
}
public addExtension(extension: IExtension) {
this.extensible.addExtension(extension);
}
}
/**
* Options for the Environment construct.
*/
export interface EnvironmentOptions {
/**
* The name of the environment.
*
* @default - A name is generated.
*/
readonly environmentName?: string;
/**
* The description of the environment.
*
* @default - No description.
*/
readonly description?: string;
/**
* The monitors for the environment.
*
* @default - No monitors.
*/
readonly monitors?: Monitor[];
}
/**
* Properties for the Environment construct.
*/
export interface EnvironmentProps extends EnvironmentOptions {
/**
* The application to be associated with the environment.
*/
readonly application: IApplication;
}
/**
* An AWS AppConfig environment.
*
* @resource AWS::AppConfig::Environment
* @see https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-creating-environment.html
*/
export class Environment extends EnvironmentBase {
/**
* Imports an environment into the CDK using its Amazon Resource Name (ARN).
*
* @param scope The parent construct
* @param id The name of the environment construct
* @param environmentArn The Amazon Resource Name (ARN) of the environment
*/
public static fromEnvironmentArn(scope: Construct, id: string, environmentArn: string): IEnvironment {
const parsedArn = Stack.of(scope).splitArn(environmentArn, ArnFormat.SLASH_RESOURCE_NAME);
if (!parsedArn.resourceName) {
throw new Error(`Missing required /$/{applicationId}/environment//$/{environmentId} from environment ARN: ${parsedArn.resourceName}`);
}
const resourceName = parsedArn.resourceName.split('/');
if (resourceName.length != 3 || !resourceName[0] || !resourceName[2]) {
throw new Error('Missing required parameters for environment ARN: format should be /$/{applicationId}/environment//$/{environmentId}');
}
const applicationId = resourceName[0];
const environmentId = resourceName[2];
class Import extends EnvironmentBase {
public readonly applicationId = applicationId;
public readonly environmentId = environmentId;
public readonly environmentArn = environmentArn;
public readonly name?: string;
}
return new Import(scope, id, {
environmentFromArn: environmentArn,
});
}
/**
* Imports an environment into the CDK from its attributes.
*
* @param scope The parent construct
* @param id The name of the environment construct
* @param attrs The attributes of the environment
*/
public static fromEnvironmentAttributes(scope: Construct, id: string, attrs: EnvironmentAttributes): IEnvironment {
const applicationId = attrs.application.applicationId;
const environmentId = attrs.environmentId;
const stack = Stack.of(scope);
const environmentArn = stack.formatArn({
service: 'appconfig',
resource: 'application',
resourceName: `${applicationId}/environment/${environmentId}`,
});
class Import extends EnvironmentBase {
public readonly application = attrs.application;
public readonly applicationId = attrs.application.applicationId;
public readonly name = attrs.name;
public readonly environmentId = environmentId;
public readonly environmentArn = environmentArn;
public readonly description = attrs.description;
public readonly monitors = attrs.monitors;
}
return new Import(scope, id, {
environmentFromArn: environmentArn,
});
}
/**
* The application associated with the environment.
*/
public readonly application?: IApplication;
/**
* The name of the environment.
*/
public readonly name?: string;
/**
* The description of the environment.
*/
public readonly description?: string;
/**
* The monitors for the environment.
*/
public readonly monitors?: Monitor[];
/**
* The ID of the environment.
*
* @attribute
*/
public readonly environmentId: string;
/**
* The Amazon Resource Name (ARN) of the environment.
*
* @attribute
*/
public readonly environmentArn: string;
/**
* The ID of the environment.
*/
public readonly applicationId: string;
private readonly _cfnEnvironment: CfnEnvironment;
constructor(scope: Construct, id: string, props: EnvironmentProps) {
super(scope, id, {
physicalName: props.environmentName,
});
this.name = props.environmentName || Names.uniqueResourceName(this, {
maxLength: 64,
separator: '-',
});
this.application = props.application;
this.applicationId = this.application.applicationId;
this.description = props.description;
this.monitors = props.monitors;
const resource = new CfnEnvironment(this, 'Resource', {
applicationId: this.applicationId,
name: this.name,
description: this.description,
monitors: this.monitors?.map((monitor) => {
return {
alarmArn: monitor.alarmArn,
...(monitor.monitorType === MonitorType.CLOUDWATCH
? { alarmRoleArn: monitor.alarmRoleArn || this.createOrGetAlarmRole().roleArn }
: { alarmRoleArn: monitor.alarmRoleArn }),
};
}),
});
this._cfnEnvironment = resource;
this.environmentId = this._cfnEnvironment.ref;
this.environmentArn = this.stack.formatArn({
service: 'appconfig',
resource: 'application',
resourceName: `${this.applicationId}/environment/${this.environmentId}`,
});
this.extensible = new ExtensibleBase(this, this.environmentArn, this.name);
this.application.addExistingEnvironment(this);
}
private createOrGetAlarmRole(): iam.IRole {
// the name is guaranteed to be set in line 243
const logicalId = `Role${getHash(this.name!)}`;
const existingRole = this.node.tryFindChild(logicalId) as iam.IRole;
if (existingRole) {
return existingRole;
}
// this scope is fine for cloudwatch:DescribeAlarms since it is readonly
// and it is required for composite alarms
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_DescribeAlarms.html
const policy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['cloudwatch:DescribeAlarms'],
resources: ['*'],
});
const document = new iam.PolicyDocument({
statements: [policy],
});
const role = new iam.Role(this, logicalId, {
roleName: PhysicalName.GENERATE_IF_NEEDED,
assumedBy: new iam.ServicePrincipal('appconfig.amazonaws.com'),
inlinePolicies: {
['AllowAppConfigMonitorAlarmPolicy']: document,
},
});
return role;
}
}
/**
* The type of Monitor.
*/
export enum MonitorType {
/**
* A Monitor from a CloudWatch alarm.
*/
CLOUDWATCH,
/**
* A Monitor from a CfnEnvironment.MonitorsProperty construct.
*/
CFN_MONITORS_PROPERTY,
}
/**
* Defines monitors that will be associated with an AWS AppConfig environment.
*/
export abstract class Monitor {
/**
* Creates a Monitor from a CloudWatch alarm. If the alarm role is not specified, a role will
* be generated.
*
* @param alarm The Amazon CloudWatch alarm.
* @param alarmRole The IAM role for AWS AppConfig to view the alarm state.
*/
public static fromCloudWatchAlarm(alarm: cloudwatch.IAlarm, alarmRole?: iam.IRole): Monitor {
return {
alarmArn: alarm.alarmArn,
alarmRoleArn: alarmRole?.roleArn,
monitorType: MonitorType.CLOUDWATCH,
};
}
/**
* Creates a Monitor from a CfnEnvironment.MonitorsProperty construct.
*
* @param monitorsProperty The monitors property.
*/
public static fromCfnMonitorsProperty(monitorsProperty: CfnEnvironment.MonitorsProperty): Monitor {
if (monitorsProperty.alarmArn === undefined) {
throw new Error('You must specify an alarmArn property to use "fromCfnMonitorsProperty".');
}
return {
alarmArn: monitorsProperty.alarmArn,
alarmRoleArn: monitorsProperty.alarmRoleArn,
monitorType: MonitorType.CFN_MONITORS_PROPERTY,
};
}
/**
* The alarm ARN for AWS AppConfig to monitor.
*/
public abstract readonly alarmArn: string;
/**
* The type of monitor.
*/
public abstract readonly monitorType: MonitorType;
/**
* The IAM role ARN for AWS AppConfig to view the alarm state.
*/
public abstract readonly alarmRoleArn?: string;
/**
* Indicates whether a CloudWatch alarm is a composite alarm.
*/
public abstract readonly isCompositeAlarm?: boolean;
}
export interface IEnvironment extends IResource {
/**
* The application associated with the environment.
*/
readonly application?: IApplication;
/**
* The ID of the application associated to the environment.
*/
readonly applicationId: string;
/**
* The name of the environment.
*/
readonly name?: string;
/**
* The description of the environment.
*/
readonly description?: string;
/**
* The monitors for the environment.
*/
readonly monitors?: Monitor[];
/**
* The ID of the environment.
* @attribute
*/
readonly environmentId: string;
/**
* The Amazon Resource Name (ARN) of the environment.
* @attribute
*/
readonly environmentArn: string;
/**
* Creates a deployment of the supplied configuration to this environment.
* Note that you can only deploy one configuration at a time to an environment.
* However, you can deploy one configuration each to different environments at the same time.
* If more than one deployment is requested for this environment, they will occur in the same order they were provided.
*
* @param configuration The configuration that will be deployed to this environment.
*/
addDeployment(configuration: IConfiguration): void;
/**
* Creates a deployment for each of the supplied configurations to this environment.
* These configurations will be deployed in the same order as the input array.
*
* @param configurations The configurations that will be deployed to this environment.
*/
addDeployments(...configurations: Array<IConfiguration>): void;
/**
* Adds an extension defined by the action point and event destination and also
* creates an extension association to the environment.
*
* @param actionPoint The action point which triggers the event
* @param eventDestination The event that occurs during the extension
* @param options Options for the extension
*/
on(actionPoint: ActionPoint, eventDestination: IEventDestination, options?: ExtensionOptions): void;
/**
* Adds a PRE_CREATE_HOSTED_CONFIGURATION_VERSION extension with the provided event destination
* and also creates an extension association to the environment.
*
* @param eventDestination The event that occurs during the extension
* @param options Options for the extension
*/
preCreateHostedConfigurationVersion(eventDestination: IEventDestination, options?: ExtensionOptions): void;
/**
* Adds a PRE_START_DEPLOYMENT extension with the provided event destination and also creates
* an extension association to the environment.
*
* @param eventDestination The event that occurs during the extension
* @param options Options for the extension
*/
preStartDeployment(eventDestination: IEventDestination, options?: ExtensionOptions): void;
/**
* Adds an ON_DEPLOYMENT_START extension with the provided event destination and also creates
* an extension association to the environment.
*
* @param eventDestination The event that occurs during the extension
* @param options Options for the extension
*/
onDeploymentStart(eventDestination: IEventDestination, options?: ExtensionOptions): void;
/**
* Adds an ON_DEPLOYMENT_STEP extension with the provided event destination and also
* creates an extension association to the environment.
*
* @param eventDestination The event that occurs during the extension
* @param options Options for the extension
*/
onDeploymentStep(eventDestination: IEventDestination, options?: ExtensionOptions): void;
/**
* Adds an ON_DEPLOYMENT_BAKING extension with the provided event destination and
* also creates an extension association to the environment.
*
* @param eventDestination The event that occurs during the extension
* @param options Options for the extension
*/
onDeploymentBaking(eventDestination: IEventDestination, options?: ExtensionOptions): void;
/**
* Adds an ON_DEPLOYMENT_COMPLETE extension with the provided event destination and
* also creates an extension association to the environment.
*
* @param eventDestination The event that occurs during the extension
* @param options Options for the extension
*/
onDeploymentComplete(eventDestination: IEventDestination, options?: ExtensionOptions): void;
/**
* Adds an ON_DEPLOYMENT_ROLLED_BACK extension with the provided event destination and
* also creates an extension association to the environment.
*
* @param eventDestination The event that occurs during the extension
* @param options Options for the extension
*/
onDeploymentRolledBack(eventDestination: IEventDestination, options?: ExtensionOptions): void;
/**
* Adds an extension association to the environment.
*
* @param extension The extension to create an association for
*/
addExtension(extension: IExtension): void;
}