-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventbridge-rule.cfndsl.rb
60 lines (51 loc) · 1.8 KB
/
eventbridge-rule.cfndsl.rb
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
CloudFormation do
events = external_parameters.fetch(:events, {})
events.each do |name, properties|
event_pattern = {}
schedule_pattern = ""
# rule with event pattern or schedule
case properties["type"]
when "event_pattern"
event_pattern = {
source: properties["source"],
"detail-type": properties["detail_type"],
"detail": properties["detail"]
}
when "schedule"
schedule_pattern = properties["schedule_pattern"]
end
# event targets
targets = []
properties['targets'].each do |target|
event_target = {}
event_target["Arn"] = FnSub(target["arn"])
event_target["Id"] = FnSub("${EnvironmentName}-#{target["id"]}")
event_target["Input"] = FnSub(target["input"].to_json) if target.has_key?('input')
if target.has_key?('dlq_arn')
event_target["DeadLetterConfig"] = {
"Arn": FnSub(target["dlq_arn"])
}
end
targets.append(event_target)
end
Events_Rule(name) do
Description FnSub("${EnvironmentName}-#{name}")
State Ref(:EventsRuleState)
EventPattern FnSub(event_pattern.to_json) if properties["type"] == "event_pattern"
ScheduleExpression schedule_pattern if properties["type"] == "schedule"
Targets targets
end
# eventbridge permission resource depending on the target type
properties['targets'].each do |target|
case target["type"]
when "lambda"
Lambda_Permission("#{name}#{target["id"]}Permission") do
FunctionName FnSub(target["arn"])
Action 'lambda:InvokeFunction'
Principal 'events.amazonaws.com'
SourceArn FnGetAtt(name, "Arn")
end
end
end
end
end