Skip to content

Commit 54083de

Browse files
authored
feat(core): add setter for termination protection on stack (#26992)
Closes #14463. Closes #21304. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d3789ab commit 54083de

File tree

12 files changed

+386
-2
lines changed

12 files changed

+386
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "34.0.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "Stack.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"34.0.0"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "34.0.0",
3+
"testCases": {
4+
"stack/DefaultTest": {
5+
"stacks": [
6+
"Stack"
7+
],
8+
"assertionStack": "stack/DefaultTest/DeployAssert",
9+
"assertionStackName": "stackDefaultTestDeployAssert0386B0AD"
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"version": "34.0.0",
3+
"artifacts": {
4+
"Stack.assets": {
5+
"type": "cdk:asset-manifest",
6+
"properties": {
7+
"file": "Stack.assets.json",
8+
"requiresBootstrapStackVersion": 6,
9+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version"
10+
}
11+
},
12+
"Stack": {
13+
"type": "aws:cloudformation:stack",
14+
"environment": "aws://unknown-account/unknown-region",
15+
"properties": {
16+
"templateFile": "Stack.template.json",
17+
"terminationProtection": true,
18+
"validateOnSynth": false,
19+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
20+
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
21+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
22+
"requiresBootstrapStackVersion": 6,
23+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
24+
"additionalDependencies": [
25+
"Stack.assets"
26+
],
27+
"lookupRole": {
28+
"arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}",
29+
"requiresBootstrapStackVersion": 8,
30+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version"
31+
}
32+
},
33+
"dependencies": [
34+
"Stack.assets"
35+
],
36+
"metadata": {
37+
"/Stack/BootstrapVersion": [
38+
{
39+
"type": "aws:cdk:logicalId",
40+
"data": "BootstrapVersion"
41+
}
42+
],
43+
"/Stack/CheckBootstrapVersion": [
44+
{
45+
"type": "aws:cdk:logicalId",
46+
"data": "CheckBootstrapVersion"
47+
}
48+
]
49+
},
50+
"displayName": "Stack"
51+
},
52+
"stackDefaultTestDeployAssert0386B0AD.assets": {
53+
"type": "cdk:asset-manifest",
54+
"properties": {
55+
"file": "stackDefaultTestDeployAssert0386B0AD.assets.json",
56+
"requiresBootstrapStackVersion": 6,
57+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version"
58+
}
59+
},
60+
"stackDefaultTestDeployAssert0386B0AD": {
61+
"type": "aws:cloudformation:stack",
62+
"environment": "aws://unknown-account/unknown-region",
63+
"properties": {
64+
"templateFile": "stackDefaultTestDeployAssert0386B0AD.template.json",
65+
"terminationProtection": false,
66+
"validateOnSynth": false,
67+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
68+
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
69+
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
70+
"requiresBootstrapStackVersion": 6,
71+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
72+
"additionalDependencies": [
73+
"stackDefaultTestDeployAssert0386B0AD.assets"
74+
],
75+
"lookupRole": {
76+
"arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}",
77+
"requiresBootstrapStackVersion": 8,
78+
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version"
79+
}
80+
},
81+
"dependencies": [
82+
"stackDefaultTestDeployAssert0386B0AD.assets"
83+
],
84+
"metadata": {
85+
"/stack/DefaultTest/DeployAssert/BootstrapVersion": [
86+
{
87+
"type": "aws:cdk:logicalId",
88+
"data": "BootstrapVersion"
89+
}
90+
],
91+
"/stack/DefaultTest/DeployAssert/CheckBootstrapVersion": [
92+
{
93+
"type": "aws:cdk:logicalId",
94+
"data": "CheckBootstrapVersion"
95+
}
96+
]
97+
},
98+
"displayName": "stack/DefaultTest/DeployAssert"
99+
},
100+
"Tree": {
101+
"type": "cdk:tree",
102+
"properties": {
103+
"file": "tree.json"
104+
}
105+
}
106+
}
107+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "34.0.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "stackDefaultTestDeployAssert0386B0AD.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"version": "tree-0.1",
3+
"tree": {
4+
"id": "App",
5+
"path": "",
6+
"children": {
7+
"Stack": {
8+
"id": "Stack",
9+
"path": "Stack",
10+
"children": {
11+
"BootstrapVersion": {
12+
"id": "BootstrapVersion",
13+
"path": "Stack/BootstrapVersion",
14+
"constructInfo": {
15+
"fqn": "aws-cdk-lib.CfnParameter",
16+
"version": "0.0.0"
17+
}
18+
},
19+
"CheckBootstrapVersion": {
20+
"id": "CheckBootstrapVersion",
21+
"path": "Stack/CheckBootstrapVersion",
22+
"constructInfo": {
23+
"fqn": "aws-cdk-lib.CfnRule",
24+
"version": "0.0.0"
25+
}
26+
}
27+
},
28+
"constructInfo": {
29+
"fqn": "aws-cdk-lib.Stack",
30+
"version": "0.0.0"
31+
}
32+
},
33+
"stack": {
34+
"id": "stack",
35+
"path": "stack",
36+
"children": {
37+
"DefaultTest": {
38+
"id": "DefaultTest",
39+
"path": "stack/DefaultTest",
40+
"children": {
41+
"Default": {
42+
"id": "Default",
43+
"path": "stack/DefaultTest/Default",
44+
"constructInfo": {
45+
"fqn": "constructs.Construct",
46+
"version": "10.2.70"
47+
}
48+
},
49+
"DeployAssert": {
50+
"id": "DeployAssert",
51+
"path": "stack/DefaultTest/DeployAssert",
52+
"children": {
53+
"BootstrapVersion": {
54+
"id": "BootstrapVersion",
55+
"path": "stack/DefaultTest/DeployAssert/BootstrapVersion",
56+
"constructInfo": {
57+
"fqn": "aws-cdk-lib.CfnParameter",
58+
"version": "0.0.0"
59+
}
60+
},
61+
"CheckBootstrapVersion": {
62+
"id": "CheckBootstrapVersion",
63+
"path": "stack/DefaultTest/DeployAssert/CheckBootstrapVersion",
64+
"constructInfo": {
65+
"fqn": "aws-cdk-lib.CfnRule",
66+
"version": "0.0.0"
67+
}
68+
}
69+
},
70+
"constructInfo": {
71+
"fqn": "aws-cdk-lib.Stack",
72+
"version": "0.0.0"
73+
}
74+
}
75+
},
76+
"constructInfo": {
77+
"fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase",
78+
"version": "0.0.0"
79+
}
80+
}
81+
},
82+
"constructInfo": {
83+
"fqn": "@aws-cdk/integ-tests-alpha.IntegTest",
84+
"version": "0.0.0"
85+
}
86+
},
87+
"Tree": {
88+
"id": "Tree",
89+
"path": "Tree",
90+
"constructInfo": {
91+
"fqn": "constructs.Construct",
92+
"version": "10.2.70"
93+
}
94+
}
95+
},
96+
"constructInfo": {
97+
"fqn": "aws-cdk-lib.App",
98+
"version": "0.0.0"
99+
}
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as cdk from 'aws-cdk-lib';
2+
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
3+
4+
/**
5+
* This test creates a stack and changes termination protection with the setter.
6+
*/
7+
8+
const app = new cdk.App();
9+
const stack = new cdk.Stack(app, 'Stack', { terminationProtection: false });
10+
stack.terminationProtection = true;
11+
12+
new IntegTest(app, 'stack', { testCases: [stack] });

packages/aws-cdk-lib/core/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,13 @@ const stack = new Stack(app, 'StackName', {
11741174
});
11751175
```
11761176

1177+
You can also set termination protection with the setter after you've instantiated the stack.
1178+
1179+
```ts
1180+
const stack = new Stack(app, 'StackName', {});
1181+
stack.terminationProtection = true;
1182+
```
1183+
11771184
By default, termination protection is disabled.
11781185

11791186
### Description

0 commit comments

Comments
 (0)