Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): bootstrap stacks are protected from termination by default #9002

Merged
merged 3 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/aws-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ $ # Deploys only to environments foo and bar
$ cdk bootstrap --app='node bin/main.js' foo bar
```

By default, bootstrap stack will be protected from stack termination. This can be disabled using
`--termination-protection` argument.
Comment on lines +253 to +254
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe yargs also supports -no--termination-protection as well (convention for boolean flags)


#### `cdk doctor`
Inspect the current command-line environment and configurations, and collect information that can be useful for
troubleshooting problems. It is usually a good idea to include the information provided by this command when submitting
Expand Down
4 changes: 3 additions & 1 deletion packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ async function parseCommandLineArguments() {
.option('execute', {type: 'boolean', desc: 'Whether to execute ChangeSet (--no-execute will NOT execute the ChangeSet)', default: true})
.option('trust', { type: 'array', desc: 'The AWS account IDs that should be trusted to perform deployments into this environment (may be repeated)', default: [], nargs: 1, requiresArg: true, hidden: true })
.option('cloudformation-execution-policies', { type: 'array', desc: 'The Managed Policy ARNs that should be attached to the role performing deployments into this environment. Required if --trust was passed (may be repeated)', default: [], nargs: 1, requiresArg: true, hidden: true })
.option('force', { alias: 'f', type: 'boolean', desc: 'Always bootstrap even if it would downgrade template version', default: false }),
.option('force', { alias: 'f', type: 'boolean', desc: 'Always bootstrap even if it would downgrade template version', default: false })
.option('termination-protection', { type: 'boolean', default: true, desc: 'Toggle CloudFormation termination protection on the bootstrap stacks' }),
)
.command('deploy [STACKS..]', 'Deploys the stack(s) named STACKS into your AWS account', yargs => yargs
.option('build-exclude', { type: 'array', alias: 'E', nargs: 1, desc: 'Do not rebuild asset with the given ID. Can be specified multiple times.', default: [] })
Expand Down Expand Up @@ -253,6 +254,7 @@ async function initCommandLine() {
execute: args.execute,
trustedAccounts: args.trust,
cloudFormationExecutionPolicies: args.cloudformationExecutionPolicies,
terminationProtection: args.terminationProtection,
});

case 'deploy':
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/api/bootstrap/bootstrap-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { legacyBootstrapTemplate } from './legacy-template';
*
* @experimental
*/
export async function bootstrapEnvironment(environment: cxapi.Environment, sdkProvider: SdkProvider, options: BootstrapEnvironmentOptions): Promise<DeployStackResult> {
export async function bootstrapEnvironment(environment: cxapi.Environment, sdkProvider: SdkProvider, options: BootstrapEnvironmentOptions = {}): Promise<DeployStackResult> {
const params = options.parameters ?? {};

if (params.trustedAccounts?.length) {
Expand Down Expand Up @@ -43,7 +43,7 @@ export async function bootstrapEnvironment(environment: cxapi.Environment, sdkPr
export async function bootstrapEnvironment2(
environment: cxapi.Environment,
sdkProvider: SdkProvider,
options: BootstrapEnvironmentOptions): Promise<DeployStackResult> {
options: BootstrapEnvironmentOptions = {}): Promise<DeployStackResult> {

const params = options.parameters ?? {};

Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk/lib/api/bootstrap/bootstrap-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ export interface BootstrappingParameters {
* @default true
*/
readonly publicAccessBlockConfiguration?: boolean;

/**
* Whether the stacks created by the bootstrap process should be protected from termination.
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-protect-stacks.html
* @default true
*/
readonly terminationProtection?: boolean;
}
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/api/bootstrap/deploy-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function deployBootstrapStack(
environment: cxapi.EnvironmentUtils.format(environment.account, environment.region),
properties: {
templateFile,
terminationProtection: options.parameters?.terminationProtection ?? true,
},
});

Expand Down
28 changes: 28 additions & 0 deletions packages/aws-cdk/test/api/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ const env = {

let sdk: MockSdkProvider;
let executed: boolean;
let protectedTermination: boolean;
let cfnMocks: jest.Mocked<SyncHandlerSubsetOf<AWS.CloudFormation>>;
let changeSetTemplate: any | undefined;
beforeEach(() => {
sdk = new MockSdkProvider();
executed = false;
protectedTermination = false;

cfnMocks = {
describeStacks: jest.fn()
Expand Down Expand Up @@ -47,6 +49,10 @@ beforeEach(() => {
return {};
}),
deleteStack: jest.fn(),
updateTerminationProtection: jest.fn(() => {
protectedTermination = true;
return {};
}),
};
sdk.stubCloudFormation(cfnMocks);
});
Expand Down Expand Up @@ -254,4 +260,26 @@ test('even if the bootstrap stack failed to create, can still retry bootstrappin
expect(ret.noOp).toBeFalsy();
expect(executed).toBeTruthy();
expect(cfnMocks.deleteStack).toHaveBeenCalled();
});

test('stacks are termination protected by default', async () => {
// WHEN
await bootstrapEnvironment(env, sdk);

// THEN
expect(executed).toBeTruthy();
expect(protectedTermination).toBeTruthy();
});

test('termination protected is turned off when unset', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'termination protected is turned off when set to false'?

// WHEN
await bootstrapEnvironment(env, sdk, {
parameters: {
terminationProtection: false,
},
});

// THEN
expect(executed).toBeTruthy();
expect(protectedTermination).toBeFalsy();
});
24 changes: 24 additions & 0 deletions packages/aws-cdk/test/api/bootstrap2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ describe('Bootstrapping v2', () => {
]);
});

test('stacks are termination protected by default', async () => {
await bootstrapEnvironment2(env, sdk);

expect(mockDeployStack).toHaveBeenCalledWith(expect.objectContaining({
stack: expect.objectContaining({
terminationProtection: true,
}),
}));
});

test('termination protected is turned off when unset', async () => {
await bootstrapEnvironment2(env, sdk, {
parameters: {
terminationProtection: false,
},
});

expect(mockDeployStack).toHaveBeenCalledWith(expect.objectContaining({
stack: expect.objectContaining({
terminationProtection: false,
}),
}));
});

afterEach(() => {
mockDeployStack.mockClear();
});
Expand Down