-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
112 lines (99 loc) · 3.7 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
## Provision the SNS topic for the cost anomaly detection, if required
module "notifications" {
source = "appvia/notifications/aws"
version = "0.1.3"
allowed_aws_services = ["budgets.amazonaws.com", "lambda.amazonaws.com"]
create_sns_topic = var.create_sns_topic
email = local.email
slack = local.slack
sns_topic_name = var.sns_topic_name
tags = var.tags
}
## Provision the cost anomaly detection for services
resource "aws_ce_anomaly_monitor" "this" {
for_each = { for x in var.monitors : x.name => x }
name = each.value.name
monitor_type = each.value.monitor_type
monitor_dimension = each.value.monitor_dimension
tags = var.tags
}
## Provision the subscriptions to the anomaly detection monitors
resource "aws_ce_anomaly_subscription" "this" {
for_each = { for x in var.monitors : x.name => x }
name = each.value.name
frequency = each.value.notify.frequency
monitor_arn_list = [aws_ce_anomaly_monitor.this[each.key].arn]
tags = var.tags
dynamic "threshold_expression" {
for_each = [each.value.notify.threshold_expression != null ? [1] : []]
content {
dynamic "dimension" {
for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "dimension", null) != null]
content {
key = dimension.value.key
match_options = dimension.value.match_options
values = dimension.value.values
}
}
dynamic "cost_category" {
for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "cost_category", null) != null]
content {
key = cost_category.value.key
match_options = cost_category.value.match_options
values = cost_category.value.values
}
}
dynamic "tags" {
for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "tags", null) != null]
content {
key = tags.value.key
match_options = tags.value.match_options
values = tags.value.values
}
}
dynamic "and" {
for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "and", null) != null]
content {
dynamic "dimension" {
for_each = and.value.and.dimension != null ? [and.value.and.dimension] : []
content {
key = dimension.value.key
match_options = dimension.value.match_options
values = dimension.value.values
}
}
}
}
dynamic "or" {
for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "or", null) != null]
content {
dynamic "dimension" {
for_each = or.value.or.dimension != null ? [or.value.or.dimension] : []
content {
key = dimension.value.key
match_options = dimension.value.match_options
values = dimension.value.values
}
}
}
}
dynamic "not" {
for_each = [for x in each.value.notify.threshold_expression : x if lookup(x, "not", null) != null]
content {
dynamic "dimension" {
for_each = not.value.not.dimension != null ? [not.value.not.dimension] : []
content {
key = dimension.value.key
match_options = dimension.value.match_options
values = dimension.value.values
}
}
}
}
}
}
subscriber {
address = module.notifications.sns_topic_arn
type = "SNS"
}
}