/
deploy-cdk-stack-action.ts
390 lines (339 loc) · 12.4 KB
/
deploy-cdk-stack-action.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
import * as fs from 'fs';
import * as path from 'path';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as cpactions from '@aws-cdk/aws-codepipeline-actions';
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import { Aws, CfnCapabilities, Stack } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct, Node } from 'constructs';
import { appOf, assemblyBuilderOf } from '../../private/construct-internals';
import { toPosixPath } from '../../private/fs';
// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from '@aws-cdk/core';
/**
* Customization options for a DeployCdkStackAction
*
* @deprecated This class is part of the old API. Use the API based on the `CodePipeline` class instead
*/
export interface DeployCdkStackActionOptions {
/**
* Base name of the action
*
* @default stackName
*/
readonly baseActionName?: string;
/**
* The CodePipeline artifact that holds the Cloud Assembly.
*/
readonly cloudAssemblyInput: codepipeline.Artifact;
/**
* Run order for the Prepare action
*
* @default 1
*/
readonly prepareRunOrder?: number;
/**
* Run order for the Execute action
*
* @default - prepareRunOrder + 1
*/
readonly executeRunOrder?: number;
/**
* Artifact to write Stack Outputs to
*
* @default - No outputs
*/
readonly output?: codepipeline.Artifact;
/**
* Filename in output to write Stack outputs to
*
* @default - Required when 'output' is set
*/
readonly outputFileName?: string;
/**
* Name of the change set to create and deploy
*
* @default 'PipelineChange'
*/
readonly changeSetName?: string;
}
/**
* Properties for a DeployCdkStackAction
*
* @deprecated This class is part of the old API. Use the API based on the `CodePipeline` class instead
*/
export interface DeployCdkStackActionProps extends DeployCdkStackActionOptions {
/**
* Relative path of template in the input artifact
*/
readonly templatePath: string;
/**
* Role for the action to assume
*
* This controls the account to deploy into
*/
readonly actionRole: iam.IRole;
/**
* The name of the stack that should be created/updated
*/
readonly stackName: string;
/**
* Role to execute CloudFormation under
*
* @default - Execute CloudFormation using the action role
*/
readonly cloudFormationExecutionRole?: iam.IRole;
/**
* Region to deploy into
*
* @default - Same region as pipeline
*/
readonly region?: string;
/**
* Artifact ID for the stack deployed here
*
* Used for pipeline order checking.
*
* @default - Order will not be checked
*/
readonly stackArtifactId?: string;
/**
* Artifact ID for the stacks this stack depends on
*
* Used for pipeline order checking.
*
* @default - No dependencies
*/
readonly dependencyStackArtifactIds?: string[];
/**
* Template configuration path relative to the input artifact
*
* @default - No template configuration
*/
readonly templateConfigurationPath?: string;
}
/**
* Options for the 'fromStackArtifact' operation
*
* @deprecated This class is part of the old API. Use the API based on the `CodePipeline` class instead
*/
export interface CdkStackActionFromArtifactOptions extends DeployCdkStackActionOptions {
/**
* The name of the stack that should be created/updated
*
* @default - Same as stack artifact
*/
readonly stackName?: string;
}
/**
* Action to deploy a CDK Stack
*
* Adds two CodePipeline Actions to the pipeline: one to create a ChangeSet
* and one to execute it.
*
* You do not need to instantiate this action yourself -- it will automatically
* be added by the pipeline when you add stack artifacts or entire stages.
*
* @deprecated This class is part of the old API. Use the API based on the `CodePipeline` class instead
*/
export class DeployCdkStackAction implements codepipeline.IAction {
/**
* Construct a DeployCdkStackAction from a Stack artifact
*/
public static fromStackArtifact(scope: Construct, artifact: cxapi.CloudFormationStackArtifact, options: CdkStackActionFromArtifactOptions) {
if (!artifact.assumeRoleArn) {
throw new Error(`Stack '${artifact.stackName}' does not have deployment role information; use the 'DefaultStackSynthesizer' synthesizer, or set the '@aws-cdk/core:newStyleStackSynthesis' context key.`);
}
const artRegion = artifact.environment.region;
const region = artRegion === Stack.of(scope).region || artRegion === cxapi.UNKNOWN_REGION ? undefined : artRegion;
const artAccount = artifact.environment.account;
const account = artAccount === Stack.of(scope).account || artAccount === cxapi.UNKNOWN_ACCOUNT ? undefined : artAccount;
const actionRole = roleFromPlaceholderArn(scope, region, account, artifact.assumeRoleArn);
const cloudFormationExecutionRole = roleFromPlaceholderArn(scope, region, account, artifact.cloudFormationExecutionRoleArn);
// We need the path of the template relative to the root Cloud Assembly
// It should be easier to get this, but for now it is what it is.
const appAsmRoot = assemblyBuilderOf(appOf(scope as CoreConstruct)).outdir;
const fullTemplatePath = path.join(artifact.assembly.directory, artifact.templateFile);
let fullConfigPath;
if (Object.keys(artifact.tags).length > 0) {
fullConfigPath = `${fullTemplatePath}.config.json`;
// Write the template configuration file (for parameters into CreateChangeSet call that
// cannot be configured any other way). They must come from a file, and there's unfortunately
// no better hook to write this file (`construct.onSynthesize()` would have been the prime candidate
// but that is being deprecated--and DeployCdkStackAction isn't even a construct).
writeTemplateConfiguration(fullConfigPath, {
Tags: artifact.tags,
});
}
return new DeployCdkStackAction({
actionRole,
cloudFormationExecutionRole,
templatePath: toPosixPath(path.relative(appAsmRoot, fullTemplatePath)),
templateConfigurationPath: fullConfigPath ? toPosixPath(path.relative(appAsmRoot, fullConfigPath)) : undefined,
region,
stackArtifactId: artifact.id,
dependencyStackArtifactIds: artifact.dependencies.filter(isStackArtifact).map(s => s.id),
stackName: options.stackName ?? artifact.stackName,
...options,
});
}
/**
* The runorder for the prepare action
*/
public readonly prepareRunOrder: number;
/**
* The runorder for the execute action
*/
public readonly executeRunOrder: number;
/**
* Name of the deployed stack
*/
public readonly stackName: string;
/**
* Artifact id of the artifact this action was based on
*/
public readonly stackArtifactId?: string;
/**
* Artifact ids of the artifact this stack artifact depends on
*/
public readonly dependencyStackArtifactIds: string[];
private readonly prepareChangeSetAction: cpactions.CloudFormationCreateReplaceChangeSetAction;
private readonly executeChangeSetAction: cpactions.CloudFormationExecuteChangeSetAction;
constructor(props: DeployCdkStackActionProps) {
if (props.output && !props.outputFileName) {
throw new Error('If \'output\' is set, \'outputFileName\' is also required');
}
this.stackArtifactId = props.stackArtifactId;
this.dependencyStackArtifactIds = props.dependencyStackArtifactIds ?? [];
this.prepareRunOrder = props.prepareRunOrder ?? 1;
this.executeRunOrder = props.executeRunOrder ?? this.prepareRunOrder + 1;
this.stackName = props.stackName;
const baseActionName = props.baseActionName ?? this.stackName;
const changeSetName = props.changeSetName ?? 'PipelineChange';
this.prepareChangeSetAction = new cpactions.CloudFormationCreateReplaceChangeSetAction({
actionName: `${baseActionName}.Prepare`,
changeSetName,
runOrder: this.prepareRunOrder,
stackName: this.stackName,
templatePath: props.cloudAssemblyInput.atPath(props.templatePath),
adminPermissions: false,
role: props.actionRole,
deploymentRole: props.cloudFormationExecutionRole,
region: props.region,
cfnCapabilities: [CfnCapabilities.NAMED_IAM, CfnCapabilities.AUTO_EXPAND],
templateConfiguration: props.templateConfigurationPath ? props.cloudAssemblyInput.atPath(props.templateConfigurationPath) : undefined,
});
this.executeChangeSetAction = new cpactions.CloudFormationExecuteChangeSetAction({
actionName: `${baseActionName}.Deploy`,
changeSetName,
runOrder: this.executeRunOrder,
stackName: this.stackName,
role: props.actionRole,
region: props.region,
outputFileName: props.outputFileName,
output: props.output,
});
}
/**
* Exists to implement IAction
*/
public bind(scope: CoreConstruct, stage: codepipeline.IStage, options: codepipeline.ActionBindOptions):
codepipeline.ActionConfig {
stage.addAction(this.prepareChangeSetAction);
return this.executeChangeSetAction.bind(scope, stage, options);
}
/**
* Exists to implement IAction
*/
public onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule {
return this.executeChangeSetAction.onStateChange(name, target, options);
}
/**
* Exists to implement IAction
*/
public get actionProperties(): codepipeline.ActionProperties {
return this.executeChangeSetAction.actionProperties;
}
}
function roleFromPlaceholderArn(scope: Construct, region: string | undefined,
account: string | undefined, arn: string): iam.IRole;
function roleFromPlaceholderArn(scope: Construct, region: string | undefined,
account: string | undefined, arn: string | undefined): iam.IRole | undefined;
function roleFromPlaceholderArn(scope: Construct, region: string | undefined,
account: string | undefined, arn: string | undefined): iam.IRole | undefined {
if (!arn) { return undefined; }
// Use placeholdered arn as construct ID.
const id = arn;
// https://github.com/aws/aws-cdk/issues/7255
let existingRole = Node.of(scope).tryFindChild(`ImmutableRole${id}`) as iam.IRole;
if (existingRole) { return existingRole; }
// For when #7255 is fixed.
existingRole = Node.of(scope).tryFindChild(id) as iam.IRole;
if (existingRole) { return existingRole; }
const arnToImport = cxapi.EnvironmentPlaceholders.replace(arn, {
region: region ?? Aws.REGION,
accountId: account ?? Aws.ACCOUNT_ID,
partition: Aws.PARTITION,
});
return iam.Role.fromRoleArn(scope, id, arnToImport, { mutable: false, addGrantsToResources: true });
}
/**
* Options for CdkDeployAction.fromStackArtifact
*
* @deprecated This class is part of the old API. Use the API based on the `CodePipeline` class instead
*/
export interface FromStackArtifactOptions {
/**
* The CodePipeline artifact that holds the Cloud Assembly.
*/
readonly cloudAssemblyInput: codepipeline.Artifact;
/**
* Run order for the 2 actions that will be created
*
* @default 1
*/
readonly prepareRunOrder?: number;
/**
* Run order for the Execute action
*
* @default - prepareRunOrder + 1
*/
readonly executeRunOrder?: number;
/**
* Artifact to write Stack Outputs to
*
* @default - No outputs
*/
readonly output?: codepipeline.Artifact;
/**
* Filename in output to write Stack outputs to
*
* @default - Required when 'output' is set
*/
readonly outputFileName?: string;
}
function isStackArtifact(a: cxapi.CloudArtifact): a is cxapi.CloudFormationStackArtifact {
// instanceof is too risky, and we're at a too late stage to properly fix.
// return a instanceof cxapi.CloudFormationStackArtifact;
return a.constructor.name === 'CloudFormationStackArtifact';
}
/**
* Template configuration in a CodePipeline
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/continuous-delivery-codepipeline-cfn-artifacts.html#w2ab1c13c17c15
*/
interface TemplateConfiguration {
readonly Parameters?: Record<string, string>;
readonly Tags?: Record<string, string>;
readonly StackPolicy?: {
readonly Statements: Array<Record<string, string>>;
};
}
/**
* Write template configuration to the given file
*/
function writeTemplateConfiguration(filename: string, config: TemplateConfiguration) {
fs.writeFileSync(filename, JSON.stringify(config, undefined, 2), { encoding: 'utf-8' });
}