-
Notifications
You must be signed in to change notification settings - Fork 2
/
eventbridge.tf
157 lines (135 loc) · 5.49 KB
/
eventbridge.tf
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
# Cron is mandatory rule regardless of the type of environment
resource "aws_cloudwatch_event_rule" "cron_every_minute" {
name = "${var.name}-cron"
description = "Trigger ${var.name} by cron"
schedule_expression = var.schedule_expression
tags = var.tags
}
resource "aws_cloudwatch_event_target" "cron" {
arn = module.reporter_lambda.lambda_function_arn
rule = aws_cloudwatch_event_rule.cron_every_minute.name
target_id = "${module.reporter_lambda.lambda_function_name}-cron"
}
# Default eventbridge rule for ECS environment
resource "aws_cloudwatch_event_rule" "ecs_task_updated" {
count = local.to_be_reported_ecs && var.create_default_eventbridge_rules ? 1 : 0
name = "${var.name}-ecs-task-updated"
description = "ECS task has been updated"
event_pattern = jsonencode({
source = ["aws.ecs"]
detail-type = ["ECS Task State Change"]
detail = {
clusterArn = ["arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:cluster/*"]
desiredStatus = ["RUNNING"]
lastStatus = ["RUNNING"]
}
})
tags = var.tags
}
resource "aws_cloudwatch_event_target" "ecs_task_updated" {
count = local.to_be_reported_ecs && var.create_default_eventbridge_rules ? 1 : 0
arn = module.reporter_lambda.lambda_function_arn
rule = aws_cloudwatch_event_rule.ecs_task_updated[0].name
target_id = "${var.name}-ecs-task-updated"
}
# Default eventbridge rule for Lambda environment
locals {
lambda_event_pattern = jsonencode({
source = ["aws.lambda"]
detail-type = ["AWS API Call via CloudTrail"]
detail = {
requestParameters = {
functionName = [{
prefix = ""
}]
}
responseElements = {
functionName = [{
prefix = ""
}]
}
}
})
}
resource "aws_cloudwatch_event_rule" "lambda_function_version_published" {
count = local.to_be_reported_lambda && var.create_default_eventbridge_rules ? 1 : 0
name = "${var.name}-lambda-function-version-published"
description = "Lambda function version has been published"
event_pattern = local.lambda_event_pattern
tags = var.tags
}
resource "aws_cloudwatch_event_target" "lambda_function_version_published" {
count = local.to_be_reported_lambda && var.create_default_eventbridge_rules ? 1 : 0
arn = module.reporter_lambda.lambda_function_arn
rule = aws_cloudwatch_event_rule.lambda_function_version_published[0].name
target_id = "${var.name}-lambda-function-version-published"
}
# Default eventbridge rule for S3 environment
resource "aws_cloudwatch_event_rule" "s3_configuration_updated" {
count = local.to_be_reported_s3 && var.create_default_eventbridge_rules ? 1 : 0
name = "${var.name}-s3-configuration-updated"
description = "S3 configuragtion has been updated"
event_pattern = jsonencode({
source = ["aws.s3"]
resources = ["arn:aws:s3:::*"]
})
tags = var.tags
}
resource "aws_cloudwatch_event_target" "s3_configuration_updated" {
count = local.to_be_reported_s3 && var.create_default_eventbridge_rules ? 1 : 0
arn = module.reporter_lambda.lambda_function_arn
rule = aws_cloudwatch_event_rule.s3_configuration_updated[0].name
target_id = "${var.name}-s3-configuration-updated"
}
# Custom EventBridge rules and targets for multiple patterns
resource "aws_cloudwatch_event_rule" "custom" {
count = var.use_custom_eventbridge_patterns ? length(var.custom_eventbridge_patterns) : 0
name = "${var.name}-custom-${count.index}"
description = "${var.name} EventBridge rule provided by user - ${count.index}"
event_pattern = var.custom_eventbridge_patterns[count.index]
tags = var.tags
}
resource "aws_cloudwatch_event_target" "custom" {
count = var.use_custom_eventbridge_patterns ? length(var.custom_eventbridge_patterns) : 0
arn = module.reporter_lambda.lambda_function_arn
rule = aws_cloudwatch_event_rule.custom[count.index].name
target_id = "${var.name}-custom-${count.index}"
}
# Prepare triggers list
locals {
trigger_cron = {
AllowExecutionFromCloudWatchCron = {
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.cron_every_minute.arn
}
}
trigger_ecs_task_changed = local.to_be_reported_ecs && var.create_default_eventbridge_rules ? { AllowExecutionFromCloudWatchECS = {
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.ecs_task_updated[0].arn
}
} : {}
trigger_lambda_new_version_published = local.to_be_reported_lambda && var.create_default_eventbridge_rules ? { AllowExecutionFromCloudWatchLambda = {
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.lambda_function_version_published[0].arn
}
} : {}
trigger_s3_configuration_changed = local.to_be_reported_s3 && var.create_default_eventbridge_rules ? { AllowExecutionFromCloudWatchS3 = {
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.s3_configuration_updated[0].arn
}
} : {}
trigger_custom = var.use_custom_eventbridge_patterns ? merge(
{ for i, rule in aws_cloudwatch_event_rule.custom : "AllowExecutionCustom-${i}" => {
principal = "events.amazonaws.com"
source_arn = rule.arn
}
}
) : {}
allowed_triggers_combined = merge(
local.trigger_cron,
local.trigger_ecs_task_changed,
local.trigger_lambda_new_version_published,
local.trigger_s3_configuration_changed,
local.trigger_custom
)
}