-
Notifications
You must be signed in to change notification settings - Fork 2
/
macro.yaml
54 lines (51 loc) · 1.76 KB
/
macro.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
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation Transformation Macros
Resources:
LambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
EventRuleTargetsFunction:
Type: AWS::Lambda::Function
Properties:
Handler: index.handler
Role: !GetAtt LambdaRole.Arn
Runtime: nodejs12.x
Code:
ZipFile: !Sub |
'use strict';
exports.handler = async (event) => {
console.log(JSON.stringify(event));
const resources = event.fragment.Resources;
const resources_to_transform = Object.keys(resources).filter(r => resources[r].Type === 'AWS::Events::Rule' && !resources[r].Properties.Targets);
resources_to_transform.forEach((r) => {
resources[r].Properties.Targets = event.templateParameterValues.arns.map((a, index) => {
return {
Arn: { 'Fn::Select': [index , { Ref: 'arns' } ] },
Id: a.split(':')[4] + '-' + a.split(':')[5].split('/')[1]
};
});
});
console.log(JSON.stringify(event.fragment));
return {
requestId: event.requestId,
status: 'success',
fragment: event.fragment
};
};
EventRuleTargetsMacro:
Type: AWS::CloudFormation::Macro
Properties:
Name: EventsRuleTargets
FunctionName: !GetAtt EventRuleTargetsFunction.Arn