-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathdeploy-stack.ts
394 lines (338 loc) · 13.3 KB
/
deploy-stack.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
import * as cxapi from '@aws-cdk/cx-api';
import * as colors from 'colors/safe';
import * as uuid from 'uuid';
import { addMetadataAssetsToManifest } from '../assets';
import { Tag } from '../cdk-toolkit';
import { debug, error, print } from '../logging';
import { toYAML } from '../serialize';
import { AssetManifestBuilder } from '../util/asset-manifest-builder';
import { publishAssets } from '../util/asset-publishing';
import { contentHash } from '../util/content-hash';
import { ISDK, SdkProvider } from './aws-auth';
import { ToolkitInfo } from './toolkit-info';
import { changeSetHasNoChanges, CloudFormationStack, TemplateParameters, waitForChangeSet, waitForStack } from './util/cloudformation';
import { StackActivityMonitor } from './util/cloudformation/stack-activity-monitor';
type TemplateBodyParameter = {
TemplateBody?: string
TemplateURL?: string
};
/** @experimental */
export interface DeployStackResult {
readonly noOp: boolean;
readonly outputs: { [name: string]: string };
readonly stackArn: string;
readonly stackArtifact: cxapi.CloudFormationStackArtifact;
}
/** @experimental */
export interface DeployStackOptions {
/**
* The stack to be deployed
*/
stack: cxapi.CloudFormationStackArtifact;
/**
* The environment to deploy this stack in
*
* The environment on the stack artifact may be unresolved, this one
* must be resolved.
*/
resolvedEnvironment: cxapi.Environment;
/**
* The SDK to use for deploying the stack
*
* Should have been initialized with the correct role with which
* stack operations should be performed.
*/
sdk: ISDK;
/**
* SDK provider (seeded with default credentials)
*
* Will exclusively be used to assume publishing credentials (which must
* start out from current credentials regardless of whether we've assumed an
* action role to touch the stack or not).
*
* Used for the following purposes:
*
* - Publish legacy assets.
* - Upload large CloudFormation templates to the staging bucket.
*/
sdkProvider: SdkProvider;
/**
* Information about the bootstrap stack found in the target environment
*
* @default - Assume there is no bootstrap stack
*/
toolkitInfo?: ToolkitInfo;
/**
* Role to pass to CloudFormation to execute the change set
*
* @default - Role specified on stack, otherwise current
*/
roleArn?: string;
/**
* Notification ARNs to pass to CloudFormation to notify when the change set has completed
*
* @default - No notifications
*/
notificationArns?: string[];
/**
* Name to deploy the stack under
*
* @default - Name from assembly
*/
deployName?: string;
/**
* Quiet or verbose deployment
*
* @default false
*/
quiet?: boolean;
/**
* List of asset IDs which shouldn't be built
*
* @default - Build all assets
*/
reuseAssets?: string[];
/**
* Tags to pass to CloudFormation to add to stack
*
* @default - No tags
*/
tags?: Tag[];
/**
* Whether to execute the changeset or leave it in review.
*
* @default true
*/
execute?: boolean;
/**
* The collection of extra parameters
* (in addition to those used for assets)
* to pass to the deployed template.
* Note that parameters with `undefined` or empty values will be ignored,
* and not passed to the template.
*
* @default - no additional parameters will be passed to the template
*/
parameters?: { [name: string]: string | undefined };
/**
* Deploy even if the deployed template is identical to the one we are about to deploy.
* @default false
*/
force?: boolean;
}
const LARGE_TEMPLATE_SIZE_KB = 50;
/** @experimental */
export async function deployStack(options: DeployStackOptions): Promise<DeployStackResult> {
const stackArtifact = options.stack;
const stackEnv = options.resolvedEnvironment;
const cfn = options.sdk.cloudFormation();
const deployName = options.deployName || stackArtifact.stackName;
let cloudFormationStack = await CloudFormationStack.lookup(cfn, deployName);
if (await canSkipDeploy(options, cloudFormationStack)) {
debug(`${deployName}: skipping deployment (use --force to override)`);
return {
noOp: true,
outputs: cloudFormationStack.outputs,
stackArn: cloudFormationStack.stackId,
stackArtifact,
};
} else {
debug(`${deployName}: deploying...`);
}
// Detect "legacy" assets (which remain in the metadata) and publish them via
// an ad-hoc asset manifest, while passing their locations via template
// parameters.
const legacyAssets = new AssetManifestBuilder();
const assetParams = await addMetadataAssetsToManifest(stackArtifact, legacyAssets, options.toolkitInfo, options.reuseAssets);
const apiParameters = TemplateParameters.fromTemplate(stackArtifact.template).makeApiParameters({
...options.parameters,
...assetParams,
}, cloudFormationStack.parameterNames);
const executionId = uuid.v4();
const bodyParameter = await makeBodyParameter(stackArtifact, options.resolvedEnvironment, legacyAssets, options.toolkitInfo);
if (cloudFormationStack.stackStatus.isCreationFailure) {
debug(`Found existing stack ${deployName} that had previously failed creation. Deleting it before attempting to re-create it.`);
await cfn.deleteStack({ StackName: deployName }).promise();
const deletedStack = await waitForStack(cfn, deployName, false);
if (deletedStack && deletedStack.stackStatus.name !== 'DELETE_COMPLETE') {
throw new Error(`Failed deleting stack ${deployName} that had previously failed creation (current state: ${deletedStack.stackStatus})`);
}
}
await publishAssets(legacyAssets.toManifest(stackArtifact.assembly.directory), options.sdkProvider, stackEnv);
const changeSetName = `CDK-${executionId}`;
const update = cloudFormationStack.exists && cloudFormationStack.stackStatus.name !== 'REVIEW_IN_PROGRESS';
debug(`Attempting to create ChangeSet ${changeSetName} to ${update ? 'update' : 'create'} stack ${deployName}`);
print('%s: creating CloudFormation changeset...', colors.bold(deployName));
const changeSet = await cfn.createChangeSet({
StackName: deployName,
ChangeSetName: changeSetName,
ChangeSetType: update ? 'UPDATE' : 'CREATE',
Description: `CDK Changeset for execution ${executionId}`,
TemplateBody: bodyParameter.TemplateBody,
TemplateURL: bodyParameter.TemplateURL,
Parameters: apiParameters,
RoleARN: options.roleArn,
NotificationARNs: options.notificationArns,
Capabilities: [ 'CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM', 'CAPABILITY_AUTO_EXPAND' ],
Tags: options.tags,
}).promise();
debug('Initiated creation of changeset: %s; waiting for it to finish creating...', changeSet.Id);
const changeSetDescription = await waitForChangeSet(cfn, deployName, changeSetName);
if (changeSetHasNoChanges(changeSetDescription)) {
debug('No changes are to be performed on %s.', deployName);
await cfn.deleteChangeSet({ StackName: deployName, ChangeSetName: changeSetName }).promise();
return { noOp: true, outputs: cloudFormationStack.outputs, stackArn: changeSet.StackId!, stackArtifact };
}
const execute = options.execute === undefined ? true : options.execute;
if (execute) {
debug('Initiating execution of changeset %s on stack %s', changeSetName, deployName);
await cfn.executeChangeSet({StackName: deployName, ChangeSetName: changeSetName}).promise();
// tslint:disable-next-line:max-line-length
const monitor = options.quiet ? undefined : new StackActivityMonitor(cfn, deployName, stackArtifact, (changeSetDescription.Changes || []).length).start();
debug('Execution of changeset %s on stack %s has started; waiting for the update to complete...', changeSetName, deployName);
try {
const finalStack = await waitForStack(cfn, deployName);
// This shouldn't really happen, but catch it anyway. You never know.
if (!finalStack) { throw new Error('Stack deploy failed (the stack disappeared while we were deploying it)'); }
cloudFormationStack = finalStack;
} finally {
await monitor?.stop();
}
debug('Stack %s has completed updating', deployName);
} else {
print('Changeset %s created and waiting in review for manual execution (--no-execute)', changeSetName);
}
// Update termination protection only if it has changed.
const terminationProtection = stackArtifact.terminationProtection ?? false;
if (cloudFormationStack.terminationProtection !== terminationProtection) {
debug('Updating termination protection from %s to %s for stack %s', cloudFormationStack.terminationProtection, terminationProtection, deployName);
await cfn.updateTerminationProtection({
StackName: deployName,
EnableTerminationProtection: terminationProtection,
}).promise();
debug('Termination protection updated to %s for stack %s', terminationProtection, deployName);
}
return { noOp: false, outputs: cloudFormationStack.outputs, stackArn: changeSet.StackId!, stackArtifact };
}
/**
* Prepares the body parameter for +CreateChangeSet+.
*
* If the template is small enough to be inlined into the API call, just return
* it immediately.
*
* Otherwise, add it to the asset manifest to get uploaded to the staging
* bucket and return its coordinates. If there is no staging bucket, an error
* is thrown.
*
* @param stack the synthesized stack that provides the CloudFormation template
* @param toolkitInfo information about the toolkit stack
*/
async function makeBodyParameter(
stack: cxapi.CloudFormationStackArtifact,
resolvedEnvironment: cxapi.Environment,
assetManifest: AssetManifestBuilder,
toolkitInfo?: ToolkitInfo): Promise<TemplateBodyParameter> {
const templateJson = toYAML(stack.template);
if (templateJson.length <= LARGE_TEMPLATE_SIZE_KB * 1024) {
return { TemplateBody: templateJson };
}
if (!toolkitInfo) {
error(
`The template for stack "${stack.displayName}" is ${Math.round(templateJson.length / 1024)}KiB. ` +
`Templates larger than ${LARGE_TEMPLATE_SIZE_KB}KiB must be uploaded to S3.\n` +
'Run the following command in order to setup an S3 bucket in this environment, and then re-deploy:\n\n',
colors.blue(`\t$ cdk bootstrap ${resolvedEnvironment.name}\n`));
throw new Error('Template too large to deploy ("cdk bootstrap" is required)');
}
const templateHash = contentHash(templateJson);
const key = `cdk/${stack.id}/${templateHash}.yml`;
const templateURL = `${toolkitInfo.bucketUrl}/${key}`;
assetManifest.addFileAsset(templateHash, {
path: stack.templateFile,
}, {
bucketName: toolkitInfo.bucketName,
objectKey: key,
});
debug('Storing template in S3 at:', templateURL);
return { TemplateURL: templateURL };
}
/** @experimental */
export interface DestroyStackOptions {
/**
* The stack to be destroyed
*/
stack: cxapi.CloudFormationStackArtifact;
sdk: ISDK;
roleArn?: string;
deployName?: string;
quiet?: boolean;
}
/** @experimental */
export async function destroyStack(options: DestroyStackOptions) {
const deployName = options.deployName || options.stack.stackName;
const cfn = options.sdk.cloudFormation();
const currentStack = await CloudFormationStack.lookup(cfn, deployName);
if (!currentStack.exists) {
return;
}
const monitor = options.quiet ? undefined : new StackActivityMonitor(cfn, deployName, options.stack).start();
try {
await cfn.deleteStack({ StackName: deployName, RoleARN: options.roleArn }).promise();
const destroyedStack = await waitForStack(cfn, deployName, false);
if (destroyedStack && destroyedStack.stackStatus.name !== 'DELETE_COMPLETE') {
throw new Error(`Failed to destroy ${deployName}: ${destroyedStack.stackStatus}`);
}
} finally {
if (monitor) { await monitor.stop(); }
}
}
/**
* Checks whether we can skip deployment
*/
async function canSkipDeploy(deployStackOptions: DeployStackOptions, cloudFormationStack: CloudFormationStack): Promise<boolean> {
const deployName = deployStackOptions.deployName || deployStackOptions.stack.stackName;
debug(`${deployName}: checking if we can skip deploy`);
// Forced deploy
if (deployStackOptions.force) {
debug(`${deployName}: forced deployment`);
return false;
}
// No existing stack
if (!cloudFormationStack.exists) {
debug(`${deployName}: no existing stack`);
return false;
}
// Template has changed (assets taken into account here)
if (JSON.stringify(deployStackOptions.stack.template) !== JSON.stringify(await cloudFormationStack.template())) {
debug(`${deployName}: template has changed`);
return false;
}
// Tags have changed
if (!compareTags(cloudFormationStack.tags, deployStackOptions.tags ?? [])) {
debug(`${deployName}: tags have changed`);
return false;
}
// Termination protection has been updated
const terminationProtection = deployStackOptions.stack.terminationProtection ?? false; // cast to boolean for comparison
if (terminationProtection !== cloudFormationStack.terminationProtection) {
debug(`${deployName}: termination protection has been updated`);
return false;
}
// We can skip deploy
return true;
}
/**
* Compares two list of tags, returns true if identical.
*/
function compareTags(a: Tag[], b: Tag[]): boolean {
if (a.length !== b.length) {
return false;
}
for (const aTag of a) {
const bTag = b.find(tag => tag.Key === aTag.Key);
if (!bTag || bTag.Value !== aTag.Value) {
return false;
}
}
return true;
}