-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
159 lines (132 loc) · 4.39 KB
/
main.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
158
159
locals {
sns_topics = { for key, config in var.alert_config :
key => config.sns_topic_name }
subscribers = merge([for key, config in var.alert_config : {
for subKey, subConfig in config.subscriptions :
"${key}-${subConfig.name}" => merge(subConfig, { defaultMonitor = key }) }]...)
slack = merge([for key, config in var.alert_config : {
for subKey, subConfig in config.subscriptions :
"${key}-${subConfig.name}" => merge(subConfig, { defaultMonitor = key })
if subConfig.frequency == "IMMEDIATE"
}
]...)
}
resource "aws_chatbot_slack_channel_configuration" "chatbot_slack_channel" {
for_each = local.slack
configuration_name = replace(each.value.name, " ", "-")
slack_channel_id = each.value.slack_channel_id
slack_team_id = data.aws_chatbot_slack_workspace.flaconi.slack_team_id
sns_topic_arns = [module.sns_topic.wrapper[each.value.defaultMonitor].topic_arn]
iam_role_arn = module.chatbot_role.iam_role_arn
guardrail_policy_arns = ["arn:aws:iam::aws:policy/ReadOnlyAccess"]
tags = var.tags
}
data "aws_iam_policy_document" "chatbot_notifications_only" {
statement {
actions = [
"cloudwatch:Describe*",
"cloudwatch:Get*",
"cloudwatch:List*"
]
resources = ["*"]
}
}
module "chatbot_role_policy" {
source = "github.com/terraform-aws-modules/terraform-aws-iam//modules/iam-policy?ref=v5.52.2"
name = "aws-chatbot-notifications-only-policy"
path = "/service-role/"
description = "NotificationsOnly policy for AWS-Chatbot"
policy = data.aws_iam_policy_document.chatbot_notifications_only.json
tags = var.tags
}
module "chatbot_role" {
source = "github.com/terraform-aws-modules/terraform-aws-iam//modules/iam-assumable-role?ref=v5.52.2"
create_role = true
role_requires_mfa = false
allow_self_assume_role = false
role_path = "/service-role/"
role_name = "aws-chatbot-role"
trusted_role_actions = ["sts:AssumeRole"]
trusted_role_services = [
"chatbot.amazonaws.com"
]
custom_role_policy_arns = [
module.chatbot_role_policy.arn
]
tags = var.tags
}
resource "aws_ce_anomaly_subscription" "subscriptions" {
for_each = local.subscribers
name = each.value.name
frequency = each.value.frequency
monitor_arn_list = [aws_ce_anomaly_monitor.monitors[each.value.defaultMonitor].arn]
dynamic "subscriber" {
for_each = each.value.frequency == "IMMEDIATE" ? [true] : []
content {
type = "SNS"
address = module.sns_topic.wrapper[each.value.defaultMonitor].topic_arn
}
}
dynamic "subscriber" {
for_each = each.value.subscriber
content {
type = strcontains(subscriber.value, "@") ? "EMAIL" : "SNS"
address = subscriber.value
}
}
threshold_expression {
dimension {
key = each.value.threshold_expresion.key
match_options = [each.value.threshold_expresion.match_option]
values = [each.value.threshold_expresion.value]
}
}
tags = var.tags
}
resource "aws_ce_anomaly_monitor" "monitors" {
for_each = var.alert_config
name = try(each.value.cost_monitor.name, each.key)
monitor_type = each.value.cost_monitor.monitor_type
monitor_dimension = each.value.cost_monitor.monitor_type == "DIMENSIONAL" ? "SERVICE" : null
tags = var.tags
monitor_specification = each.value.cost_monitor.monitor_type == "CUSTOM" ? jsonencode({
And = null
CostCategories = null
Dimensions = null
Not = null
Or = null
Tags = {
Key = "CostCenter"
MatchOptions = null
Values = [
"10000"
]
}
}) : null
}
module "sns_topic" {
source = "github.com/terraform-aws-modules/terraform-aws-sns//wrappers?ref=v6.1.2"
defaults = {
tags = var.tags
enable_default_topic_policy = false
}
items = { for key, sns_topic in local.sns_topics :
key => {
name = sns_topic
topic_policy_statements = {
cost_publish = {
actions = ["sns:Publish"]
principals = [{
type = "Service"
identifiers = ["costalerts.amazonaws.com"]
}]
conditions = [{
test = "StringEquals"
variable = "aws:SourceAccount"
values = [var.source_account]
}]
}
}
}
}
}