-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstack-set.ts
175 lines (164 loc) · 5.67 KB
/
stack-set.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
import { CfnStackSet } from "aws-cdk-lib";
import type { GuStack, GuStackForStackSetInstance } from "../core";
interface GuStackSetProps {
/**
* The contents for `TemplateBody` within a `AWS::CloudFormation::StackSet`.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudformation-stackset.html#cfn-cloudformation-stackset-templatebody
*/
stackSetInstance: GuStackForStackSetInstance;
/**
* The name of the stack set. This will appear in the target account.
*/
name: string;
/**
* A description of the stack set. This will appear in the target account.
*/
description: string;
/**
* AWS Organisation Unit IDs to deploy the stack set into.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudformation-stackset-deploymenttargets.html#cfn-cloudformation-stackset-deploymenttargets-organizationalunitids
*/
organisationUnitTargets: string[];
/**
* The contents for`Parameters` within a `AWS::CloudFormation::StackSet`.
*
* Typically, you'll only need parameters if the parent stack creates resources for use in the stack set instances.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudformation-stackset.html#cfn-cloudformation-stackset-parameters
*/
stackSetInstanceParameters?: Record<string, string>;
/**
* Which regions to run the stack set in.
*
* @default The region of the parent stack.
*/
regions?: string[];
}
/**
* A construct to create a `AWS::CloudFormation::StackSet`.
*
* The stack set will be configured to automatically deploy into accounts when they join an AWS Organisation Unit.
* It's assumed stack sets are only provisioned for infrastructure, therefore the `STAGE` value will always be `INFRA`.
*
* Usage:
* ```typescript
* // the infrastructure to create in target accounts
* class AccountAlertTopic extends GuStackForStackSetInstance {
* constructor(id: string, props: GuStackProps) {
* super(id, props);
*
* new GuSnsTopic(this, "topic-for-alerts");
* }
* }
*
* class ParentStack extends GuStackForInfrastructure {
* constructor(scope: App, id: string, props: GuStackProps) {
* super(scope, id, props);
*
* new GuStackSet(this, `${id}StackSet`, {
* stackSetInstance: new AccountAlertTopic("Alerts", { stack: props.stack }),
* name: "centralised-alarms",
* description: "Provisioning of standard account alerting resources",
* organisationUnitTargets: [ "o-abcde12345" ]
* });
* }
* }
*
* // contents of `bin/cdk.ts`
* new ParentStack(new App(), "AccountAlarmResources", { stack: "alarms" })
* ```
*
* This will produce a CloudFormation template like this:
* ```yaml
* Resources:
* AccountAlarmResourcesStackSet:
* Type: AWS::CloudFormation::StackSet
* Properties:
* PermissionModel: SERVICE_MANAGED
* StackSetName: centralised-alarms
* AutoDeployment:
* Enabled: true
* RetainStacksOnAccountRemoval: false
* Description: Provisioning of standard account alerting resources
* Parameters: []
* StackInstancesGroup:
* - DeploymentTargets:
* OrganizationalUnitIds:
* - o-abcde12345
* Regions:
* - Ref: AWS::Region
* Tags:
* - Key: gu:cdk:version
* Value: TEST
* - Key: gu:repo
* Value: guardian/cdk
* - Key: Stack
* Value: alarms
* - Key: Stage
* Value: INFRA
* TemplateBody: |-
* {
* "Resources": {
* "topicforalerts57330FBE": {
* "Type": "AWS::SNS::Topic",
* "Properties": {
* "Tags": [
* {
* "Key": "gu:cdk:version",
* "Value": "TEST"
* },
* {
* "Key": "gu:repo",
* "Value": "guardian/cdk"
* },
* {
* "Key": "Stack",
* "Value": "alarms"
* },
* {
* "Key": "Stage",
* "Value": "INFRA"
* }
* ]
* }
* }
* }
* }
* ```
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudformation-stackset.html
*/
export class GuStackSet extends CfnStackSet {
constructor(scope: GuStack, id: string, props: GuStackSetProps) {
const stackSetInstanceParameters = props.stackSetInstanceParameters ?? {};
const params = Object.keys(stackSetInstanceParameters);
const parameterKeys = Object.keys(props.stackSetInstance.parameters);
const undefinedStackSetParams = parameterKeys.filter((_) => !params.includes(_));
if (undefinedStackSetParams.length !== 0) {
throw new Error(`There are undefined stack set parameters: ${undefinedStackSetParams.join(", ")}`);
}
super(scope, id, {
stackSetName: props.name,
description: props.description,
permissionModel: "SERVICE_MANAGED",
autoDeployment: {
enabled: true,
retainStacksOnAccountRemoval: false,
},
stackInstancesGroup: [
{
regions: props.regions ?? [scope.region],
deploymentTargets: {
organizationalUnitIds: props.organisationUnitTargets,
},
},
],
templateBody: props.stackSetInstance.cfnJson,
parameters: Object.entries(stackSetInstanceParameters).map(([key, value]) => {
return { parameterKey: key, parameterValue: value };
}),
});
}
}