@@ -5,34 +5,70 @@ data "aws_caller_identity" "current" {}
55
66# https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_budgets_CostTypes.html
77#
8- /*
8+ /**/
99# This is future work for single budget that can set most things
1010locals {
1111 cost_filters = {
1212 AZ = {
1313 AZ = " "
1414 }
15+
1516 LinkedAccount = {
16- LinkedAccount = "${join(",", var.account_ids)}"
17+ # LinkedAccount = "${join(",", var.account_ids)}"
1718 }
19+
1820 Operation = {
1921 Operation = " "
2022 }
23+
2124 PurchaseType = {
2225 PurchaseType = " "
2326 }
27+
2428 Service = {
2529 Service = " "
2630 }
31+
2732 TagKeyValue = {
28- TagKeyValue = "Stack$$$ {var.service }"
33+ TagKeyValue = " user: ${ var . tag_key } $$$ {var.tag_value }"
2934 }
35+
3036 UsageType = {
3137 UsageType = " "
3238 }
3339 }
3440}
41+
3542/**/
43+ resource "aws_budgets_budget" "budget" {
44+ count = " ${ length (var. budgets ) == 0 ? 1 : 0 } "
45+ account_id = " ${ data . aws_caller_identity . current . account_id } "
46+ name = " ${ var . budget_name_prefix } ${ var . budget_name } -${ title (lower (var. time_unit ))} "
47+ budget_type = " ${ var . budget_type } "
48+ limit_unit = " ${ var . limit_unit } "
49+ time_unit = " ${ var . time_unit } "
50+ cost_filters = " ${ local . cost_filters [var . cost_filter_type ]} "
51+
52+ limit_amount = " ${ var . time_unit == " ANNUALLY" ?
53+ var . limit_amount * 12 :
54+ var . time_unit == " QUARTERLY" ?
55+ var . limit_amount * 3 :
56+ var . limit_amount
57+ } "
58+
59+ time_period_start = " ${ var . time_unit == " ANNUALLY" ?
60+ " ${ substr (timestamp (), 0 , 5 )} 01-01_00:00" :
61+ var . time_unit == " QUARTERLY" ?
62+ " ${ substr (timestamp (), 0 , 5 )} ${ format (" %02d" , (substr (timestamp (), 5 , 2 ) - 1 ) / 3 * 3 + 1 )} -01_00:00" :
63+ " ${ substr (timestamp (), 0 , 8 )} 01_00:00"
64+ } "
65+
66+ lifecycle {
67+ ignore_changes = [" time_period_start" ]
68+ }
69+ }
70+
71+ # Multiple linked account budgets
3672resource "aws_budgets_budget" "budgets" {
3773 count = " ${ length (var. budgets )} "
3874 account_id = " ${ data . aws_caller_identity . current . account_id } "
0 commit comments