-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCrossAccountHelperMacros.yaml
336 lines (291 loc) · 13 KB
/
CrossAccountHelperMacros.yaml
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
AWSTemplateFormatVersion: '2010-09-09'
Description: Cross account helper macros
Resources:
# Role for the pipeline helper macro function.
PipelineHelperMacroFunctionRole:
Type: AWS::IAM::Role
Properties:
RoleName: CrossAccount-PipelineHelperMacroFunction
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Ref PipelineHelperMacroFunctionPolicy
PipelineHelperMacroFunctionPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: CrossAccount-PipelineHelperMacroFunction
Description: Policy for cross account pipeline helper macro function
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ssm:GetParameter
Resource:
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/cross-account-pipeline-helper/*
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/CrossAccount-PipelineHelperMacroFunction
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/CrossAccount-PipelineHelperMacroFunction:*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/CrossAccount-PipelineHelper2MacroFunction
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/CrossAccount-PipelineHelper2MacroFunction:*
PipelineHelperMacroFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /aws/lambda/CrossAccount-PipelineHelperMacroFunction
RetentionInDays: 3
PipelineHelperMacroFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
const AWS = require('aws-sdk')
exports.handler = async function main(event, context) {
console.info(JSON.stringify(event));
const accounts = event.params.Accounts || event.templateParameterValues.Accounts;
const regions = event.params.Regions || event.templateParameterValues.Regions;
const names = event.params.Names || event.templateParameterValues.Names;
const manualApprovalNames = event.params.ManualApprovalNames;
const approvalNotificationArn = event.params.ApprovalNotificationArn;
const artifactBuckets = event.params.ArtifactBuckets || event.templateParameterValues.ArtifactBuckets;
const artifactKMSKeys = event.params.ArtifactKMSKeys || event.templateParameterValues.ArtifactKMSKeys;
const artifactRegions = event.params.ArtifactRegions || event.templateParameterValues.ArtifactRegions;
const fragment = event.fragment;
const stages = fragment.Properties.Stages;
if (artifactRegions) {
const artifactStores = [];
for (var i = 0; i < artifactRegions.length; i++) {
artifactStores.push({
ArtifactStore: {
Location: artifactBuckets[i],
Type: 'S3',
EncryptionKey: {
Id: artifactKMSKeys[i],
Type: 'KMS'
}
},
Region: artifactRegions[i]
});
}
fragment.Properties.ArtifactStores = artifactStores;
}
for (const duplicate of event.params.DuplicateStages || []) {
const stageIndex = stages.findIndex(s => s.Name === duplicate);
const stageString = JSON.stringify(stages[stageIndex]);
stages.splice(stageIndex, 1)
for (var i = 0; i < accounts.length; i++) {
const stage = JSON.parse(stageString.replace(/{ACCOUNT_ID}/g, accounts[i]).replace(/{REGION}/g, regions[i]).replace(/{NAME}/g, names[i]));
stage.Name = `${stage.Name}-${names[i]}`;
// if the manual intervention names list contains the name of the current item then we will inject an approval step
if (manualApprovalNames && manualApprovalNames.findIndex(n => n === names[i]) !== -1) {
const approvalAction = {
Name: `${names[i]}-Approval`,
ActionTypeId: {
Category: 'Approval',
Owner: 'AWS',
Provider: 'Manual',
Version: '1'
},
Configuration: {
CustomData: `${fragment.Properties.Name} ${names[i]} Deployment`,
NotificationArn: approvalNotificationArn
},
RunOrder: 1
};
stage.Actions.splice(0, 0, approvalAction);
}
stages.splice(stageIndex + i, 0, stage);
}
}
fragment.Properties.Stages = stages;
console.info(JSON.stringify(fragment));
return {
requestId: event.requestId,
status: 'success',
fragment: fragment
}
}
Description: Replicates a stage of a pipeline for each environment in the list
FunctionName: CrossAccount-PipelineHelperMacroFunction
Handler: index.handler
MemorySize: 128
Role: !GetAtt PipelineHelperMacroFunctionRole.Arn
Runtime: nodejs16.x
Timeout: 30
PipelineHelper2MacroFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /aws/lambda/CrossAccount-PipelineHelper2MacroFunction
RetentionInDays: 3
PipelineHelper2MacroFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
const AWS = require('aws-sdk')
exports.handler = async function main(event, context) {
console.info(JSON.stringify(event));
const configParameterName = event.params.ConfigParameterName || event.templateParameterValues.ConfigParameterName;
const approvalNotificationArn = event.params.ApprovalNotificationArn;
const artifactBuckets = event.params.ArtifactBuckets || event.templateParameterValues.ArtifactBuckets;
const artifactKMSKeys = event.params.ArtifactKMSKeys || event.templateParameterValues.ArtifactKMSKeys;
const artifactRegions = event.params.ArtifactRegions || event.templateParameterValues.ArtifactRegions;
const fragment = event.fragment;
const stages = fragment.Properties.Stages;
if (artifactRegions) {
const artifactStores = [];
for (var i = 0; i < artifactRegions.length; i++) {
artifactStores.push({
ArtifactStore: {
Location: artifactBuckets[i],
Type: 'S3',
EncryptionKey: {
Id: artifactKMSKeys[i],
Type: 'KMS'
}
},
Region: artifactRegions[i]
});
}
fragment.Properties.ArtifactStores = artifactStores;
}
const ssm = new AWS.SSM();
const param = await ssm.getParameter({ Name: configParameterName }).promise();
const stageDuplications = JSON.parse(param.Parameter.Value);
for (const stageDuplication of stageDuplications || []) {
const stageIndex = stages.findIndex(s => s.Name === stageDuplication.stage);
let count = 1;
for (const wave of stageDuplication.waves) {
const stage = {
Name: `${stageDuplication.stage}-${wave.name}`,
Actions: []
}
const actionsString = JSON.stringify(stages[stageIndex].Actions);
for (const environment of wave.environments) {
const actions = JSON.parse(actionsString.replace(/{ACCOUNT_ID}/g, environment.account).replace(/{REGION}/g, environment.region).replace(/{NAME}/g, environment.name));
for (const action of actions) {
action.Name = `${action.Name}-${environment.name}`
}
stage.Actions.push(...actions);
}
if (wave.manualApproval) {
const approvalAction = {
Name: `${wave.name}-Approval`,
ActionTypeId: {
Category: 'Approval',
Owner: 'AWS',
Provider: 'Manual',
Version: '1'
},
Configuration: {
CustomData: `${fragment.Properties.Name} ${wave.name} Deployment`,
NotificationArn: approvalNotificationArn
},
RunOrder: 1
};
stage.Actions.splice(0, 0, approvalAction);
}
stages.splice(stageIndex + count, 0, stage);
count++;
}
// remove the stage template
stages.splice(stageIndex, 1)
}
fragment.Properties.Stages = stages;
console.info(JSON.stringify(fragment));
return {
requestId: event.requestId,
status: 'success',
fragment: fragment
}
}
Description: Replicates a stage of a pipeline for based on the stage duplication config
FunctionName: CrossAccount-PipelineHelper2MacroFunction
Handler: index.handler
MemorySize: 128
Role: !GetAtt PipelineHelperMacroFunctionRole.Arn
Runtime: nodejs16.x
Timeout: 30
PipelineHelperMacroLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /jasonwadsworth/CrossAccount-PipelineHelperMacro
RetentionInDays: 3
PipelineHelper2MacroLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /jasonwadsworth/CrossAccount-PipelineHelper2Macro
RetentionInDays: 3
PipelineHelperMacroRole:
Type: AWS::IAM::Role
Properties:
RoleName: CrossAccount-PipelineHelperMacro
Path: /cross-account-service-role/
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: cloudformation.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Ref PipelineHelperMacroPolicy
PipelineHelperMacroPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: CrossAccount-PipelineHelperMacro
Path: /cross-account-service-role/
Description: Policy for cross account pipeline helper macro
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/jasonwadsworth/CrossAccount-PipelineHelperMacro
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/jasonwadsworth/CrossAccount-PipelineHelperMacro:*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/jasonwadsworth/CrossAccount-PipelineHelper2Macro
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/jasonwadsworth/CrossAccount-PipelineHelper2Macro:*
PipelineHelperMacro:
Type: AWS::CloudFormation::Macro
Properties:
Name: CrossAccount-PipelineHelperMacro
Description: Replicates a stage of a pipeline for each environment in the list
FunctionName: !GetAtt PipelineHelperMacroFunction.Arn
LogGroupName: /jasonwadsworth/CrossAccount-PipelineHelperMacro
LogRoleARN: !GetAtt PipelineHelperMacroRole.Arn
PipelineHelper2Macro:
Type: AWS::CloudFormation::Macro
Properties:
Name: CrossAccount-PipelineHelper2Macro
Description: Replicates a stage of a pipeline based on the stage duplication config
FunctionName: !GetAtt PipelineHelper2MacroFunction.Arn
LogGroupName: /jasonwadsworth/CrossAccount-PipelineHelper2Macro
LogRoleARN: !GetAtt PipelineHelperMacroRole.Arn
CrossAccountPipelineHelperMacroInvokePolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
Description: Policy to allow the Cross Account Pipeline Helper Macro to be invoked
ManagedPolicyName: CrossAccount-PipelineHelperMacroInvoke
Path: /cross-account-service-role/
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- lambda:InvokeFunction
Effect: Allow
Resource:
- !GetAtt PipelineHelperMacroFunction.Arn
- !GetAtt PipelineHelper2MacroFunction.Arn
Roles:
- CrossAccount-UpdatePipeline