Skip to content

Commit 4284aa2

Browse files
selvathiruarulmergify[bot]
authored andcommitted
feat(cli): add tags to CDKToolkit stack through bootstrap cli command (#4320)
* feat(cli): add tags to bootstrap cli(#4227) * feat(cli): add unit test and integration test and default value for tags in bootstrap cli(#4227) * feat(cli): reverted test case(#4227) * feat(cli): fix review comments on toolkit stack tags,removed unwanted test scenarios(#4227) * feat(cli): fix test cases for bootstrap cli tags(#4227) * feat(cli): add integration test for tags based on peer review(#4227) * feat(cli): change toolkit tag integration test message and condition(#4227)
1 parent d16080a commit 4284aa2

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

packages/aws-cdk/bin/cdk.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ async function parseCommandLineArguments() {
5050
.option('exclusively', { type: 'boolean', alias: 'e', desc: 'Only deploy requested stacks, don\'t include dependencies' }))
5151
.command('bootstrap [ENVIRONMENTS..]', 'Deploys the CDK toolkit stack into an AWS environment', yargs => yargs
5252
.option('bootstrap-bucket-name', { type: 'string', alias: ['b', 'toolkit-bucket-name'], desc: 'The name of the CDK toolkit bucket', default: undefined })
53-
.option('bootstrap-kms-key-id', { type: 'string', desc: 'AWS KMS master key ID used for the SSE-KMS encryption', default: undefined }))
53+
.option('bootstrap-kms-key-id', { type: 'string', desc: 'AWS KMS master key ID used for the SSE-KMS encryption', default: undefined })
54+
.option('tags', { type: 'array', alias: 't', desc: 'Tags to add for the stack (KEY=VALUE)', nargs: 1, requiresArg: true, default: undefined }))
5455
.command('deploy [STACKS..]', 'Deploys the stack(s) named STACKS into your AWS account', yargs => yargs
5556
.option('build-exclude', { type: 'array', alias: 'E', nargs: 1, desc: 'Do not rebuild asset with the given ID. Can be specified multiple times.', default: [] })
5657
.option('exclusively', { type: 'boolean', alias: 'e', desc: 'Only deploy requested stacks, don\'t include dependencies' })
@@ -188,6 +189,7 @@ async function initCommandLine() {
188189
return await cliBootstrap(args.ENVIRONMENTS, toolkitStackName, args.roleArn, {
189190
bucketName: configuration.settings.get(['toolkitBucket', 'bucketName']),
190191
kmsKeyId: configuration.settings.get(['toolkitBucket', 'kmsKeyId']),
192+
tags: configuration.settings.get(['tags'])
191193
});
192194

193195
case 'deploy':

packages/aws-cdk/lib/api/bootstrap-environment.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import cxapi = require('@aws-cdk/cx-api');
22
import fs = require('fs-extra');
33
import os = require('os');
44
import path = require('path');
5+
import {Tag} from "./cxapp/stacks";
56
import { deployStack, DeployStackResult } from './deploy-stack';
67
import { ISDK } from './util/sdk';
78

@@ -26,6 +27,12 @@ export interface BootstrapEnvironmentProps {
2627
* @default - the default KMS key for S3 will be used.
2728
*/
2829
readonly kmsKeyId?: string;
30+
/**
31+
* Tags for cdktoolkit stack.
32+
*
33+
* @default - None.
34+
*/
35+
tags?: Tag[];
2936
}
3037

3138
/** @experimental */
@@ -83,5 +90,5 @@ export async function bootstrapEnvironment(environment: cxapi.Environment, aws:
8390
});
8491

8592
const assembly = builder.buildAssembly();
86-
return await deployStack({ stack: assembly.getStack(toolkitStackName), sdk: aws, roleArn });
93+
return await deployStack({ stack: assembly.getStack(toolkitStackName), sdk: aws, roleArn, tags: props.tags });
8794
}

packages/aws-cdk/test/api/test.bootstrap.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,56 @@ export = {
151151
test.equals(ret.noOp, false);
152152
test.equals(executed, true);
153153

154+
test.done();
155+
},
156+
async 'do bootstrap with custom tags for toolkit stack'(test: Test) {
157+
// GIVEN
158+
const sdk = new MockSDK();
159+
160+
let executed = false;
161+
162+
sdk.stubCloudFormation({
163+
describeStacks() {
164+
return {
165+
Stacks: []
166+
};
167+
},
168+
169+
createChangeSet(info: CreateChangeSetInput) {
170+
const template = fromYAML(info.TemplateBody as string);
171+
const bucketProperties = template.Resources.StagingBucket.Properties;
172+
test.equals(bucketProperties.BucketName, undefined, 'Expected BucketName to be undefined');
173+
test.equals(bucketProperties.BucketEncryption.ServerSideEncryptionConfiguration[0].ServerSideEncryptionByDefault.KMSMasterKeyID,
174+
undefined, 'Expected KMSMasterKeyID to be undefined');
175+
return {};
176+
},
177+
178+
describeChangeSet() {
179+
return {
180+
Status: 'CREATE_COMPLETE',
181+
Changes: [],
182+
};
183+
},
184+
185+
executeChangeSet() {
186+
executed = true;
187+
return {};
188+
}
189+
});
190+
191+
// WHEN
192+
const ret = await bootstrapEnvironment({
193+
account: '123456789012',
194+
region: 'us-east-1',
195+
name: 'mock',
196+
}, sdk, 'mockStack', undefined, {
197+
tags: [{ Key: 'Foo', Value: 'Bar' }]
198+
});
199+
200+
// THEN
201+
test.equals(ret.noOp, false);
202+
test.equals(executed, true);
203+
154204
test.done();
155205
},
156206
};

packages/aws-cdk/test/integ/cli/test-cdk-multiple-toolkit-stacks.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ toolkit_stack_name_1="toolkit-stack-1-${RANDOM}"
1010
toolkit_stack_name_2="toolkit-stack-2-${RANDOM}"
1111

1212
# deploy two toolkit stacks into the same environment (see #1416)
13-
cdk bootstrap --toolkit-stack-name ${toolkit_stack_name_1}
14-
cdk bootstrap --toolkit-stack-name ${toolkit_stack_name_2}
13+
cdk bootstrap --toolkit-stack-name ${toolkit_stack_name_1} --tags Foo=Bar
14+
cdk bootstrap --toolkit-stack-name ${toolkit_stack_name_2} --tags Foo=Bar
1515

1616
# just check that the new stack exists
1717
aws cloudformation describe-stack-resources --stack-name ${toolkit_stack_name_1}
1818
aws cloudformation describe-stack-resources --stack-name ${toolkit_stack_name_2}
1919

20+
# get tags from the new stack
21+
tag_stack_1=$(aws cloudformation describe-stacks --stack-name ${toolkit_stack_name_1} --query "Stacks[0].Tags[?Key=='Foo'].Value" --output text)
22+
tag_stack_2=$(aws cloudformation describe-stacks --stack-name ${toolkit_stack_name_2} --query "Stacks[0].Tags[?Key=='Foo'].Value" --output text)
23+
24+
# check if tag is not equal to bar
25+
if [ "${tag_stack_1}" != "Bar" ] || [ "${tag_stack_2}" != "Bar" ]; then
26+
fail "toolkit tags test expect Bar but got ${tag_stack_1} and ${tag_stack_2}"
27+
fi
28+
2029
# clean up
2130
aws cloudformation delete-stack --stack-name ${toolkit_stack_name_1}
2231
aws cloudformation delete-stack --stack-name ${toolkit_stack_name_2}

0 commit comments

Comments
 (0)