-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
107 lines (87 loc) · 2.88 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
data "aws_caller_identity" "current" {}
# Activate tags for cost allocation tracking
# Must be done by master account
# https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_budgets_CostTypes.html
#
/**/
# This is future work for single budget that can set most things
locals {
cost_filters = {
AZ = {
AZ = ""
}
LinkedAccount = {
#LinkedAccount = "${join(",", var.account_ids)}"
}
Operation = {
Operation = ""
}
PurchaseType = {
PurchaseType = ""
}
Service = {
Service = ""
}
TagKeyValue = {
TagKeyValue = "user:${var.tag_key}$$${var.tag_value}"
}
UsageType = {
UsageType = ""
}
}
month = "${substr(timestamp(), 5, 2)}"
month_num = "${substr(local.month,0, 1) == "0" ? substr(local.month,1, 1) : local.month}"
}
/**/
resource "aws_budgets_budget" "budget" {
count = "${length(var.budgets) == 0 ? 1 : 0}"
account_id = "${data.aws_caller_identity.current.account_id}"
name = "${var.budget_name_prefix}${var.budget_name}-${title(lower(var.time_unit))}"
budget_type = "${var.budget_type}"
limit_unit = "${var.limit_unit}"
time_unit = "${var.time_unit}"
cost_filters = "${local.cost_filters[var.cost_filter_type]}"
limit_amount = "${var.time_unit == "ANNUALLY" ?
var.limit_amount * 12 :
var.time_unit == "QUARTERLY" ?
var.limit_amount * 3 :
var.limit_amount
}"
time_period_start = "${var.time_unit == "ANNUALLY" ?
"${substr(timestamp(), 0, 5)}01-01_00:00" :
var.time_unit == "QUARTERLY" ?
"${substr(timestamp(), 0, 5)}${format("%02d", (local.month_num - 1) / 3 * 3 + 1)}-01_00:00" :
"${substr(timestamp(), 0, 8)}01_00:00"
}"
lifecycle {
ignore_changes = ["time_period_start"]
}
}
# Multiple linked account budgets
resource "aws_budgets_budget" "budgets" {
count = "${length(var.budgets)}"
account_id = "${data.aws_caller_identity.current.account_id}"
name = "${var.budget_name_prefix}${lookup(var.budgets[count.index], "name")}-${title(lower(var.time_unit))}"
budget_type = "${var.budget_type}"
limit_unit = "${var.limit_unit}"
time_unit = "${var.time_unit}"
limit_amount = "${var.time_unit == "ANNUALLY" ?
lookup(var.budgets[count.index], "limit", "100") * 12 :
var.time_unit == "QUARTERLY" ?
lookup(var.budgets[count.index], "limit", "100") * 3 :
lookup(var.budgets[count.index], "limit", "100")
}"
time_period_start = "${var.time_unit == "ANNUALLY" ?
"${substr(timestamp(), 0, 5)}01-01_00:00" :
var.time_unit == "QUARTERLY" ?
"${substr(timestamp(), 0, 5)}${format("%02d", (local.month_num - 1) / 3 * 3 + 1)}-01_00:00" :
"${substr(timestamp(), 0, 8)}01_00:00"
}"
cost_filters = {
LinkedAccount = "${lookup(var.budgets[count.index], "account")}"
}
lifecycle {
ignore_changes = ["time_period_start"]
}
}
# Attributes: id