7
7
# https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html#API_Subscribe_RequestParameters
8
8
9
9
locals {
10
+ # Prefix for single budget
11
+ notification_cmd_prefix_single = << EOF
12
+ aws budgets create-notification \
13
+ --account-id ${ data . aws_caller_identity . current . account_id } \
14
+ --budget-name ${ var . budget_name_prefix } ${ var . budget_name } -${ title (lower (var. time_unit ))} \
15
+ --subscribers ${ join (" " , formatlist (" SubscriptionType=EMAIL,Address=%s" , var. emails ))} \
16
+ --notification \
17
+ EOF
18
+
10
19
# Prefix for multiple budgets. So only contain stuff that is common to all budgets in var.budgets
11
20
# Could move subscribers here as long as only supporting one
12
21
notification_cmd_prefix = << EOF
@@ -15,8 +24,62 @@ aws budgets create-notification \
15
24
EOF
16
25
}
17
26
27
+ //
28
+ // Notifications for single budget
29
+ //
30
+ # When forecasted bill exceeds budget by 10%
31
+ resource "null_resource" "budget_forecast_110" {
32
+ count = " ${ length (var. budgets ) == 0 ? 1 : 0 } "
33
+
34
+ triggers {
35
+ budget_id = " ${ aws_budgets_budget . budget . id } "
36
+ prefix = " ${ local . notification_cmd_prefix_single } "
37
+ }
38
+
39
+ provisioner "local-exec" {
40
+ command = << CMD
41
+ ${ local . notification_cmd_prefix_single } NotificationType=FORECASTED,ComparisonOperator=GREATER_THAN,Threshold=110,ThresholdType=PERCENTAGE
42
+ CMD
43
+ }
44
+ }
45
+
46
+ # When actual bill exceeds 90% budget
47
+ resource "null_resource" "budget_actual_90" {
48
+ count = " ${ length (var. budgets ) == 0 ? 1 : 0 } "
49
+
50
+ triggers {
51
+ budget_id = " ${ aws_budgets_budget . budget . id } "
52
+ prefix = " ${ local . notification_cmd_prefix_single } "
53
+ }
54
+
55
+ provisioner "local-exec" {
56
+ command = << CMD
57
+ ${ local . notification_cmd_prefix_single } NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=90,ThresholdType=PERCENTAGE
58
+ CMD
59
+ }
60
+ }
61
+
62
+ # When actual bill exceeds 80% budget
63
+ resource "null_resource" "budget_actual_80" {
64
+ count = " ${ length (var. budgets ) == 0 ? 1 : 0 } "
65
+
66
+ triggers {
67
+ budget_id = " ${ aws_budgets_budget . budget . id } "
68
+ prefix = " ${ local . notification_cmd_prefix_single } "
69
+ }
70
+
71
+ provisioner "local-exec" {
72
+ command = << CMD
73
+ ${ local . notification_cmd_prefix_single } NotificationType=ACTUAL,ComparisonOperator=GREATER_THAN,Threshold=80,ThresholdType=PERCENTAGE
74
+ CMD
75
+ }
76
+ }
77
+
78
+ //
79
+ // Notifications for multiple budgets
80
+ //
18
81
# When forecasted bill exceeds budget by 10%
19
- resource "null_resource" "budget_forecast_notification " {
82
+ resource "null_resource" "budgets_forecast_110 " {
20
83
count = " ${ length (var. budgets )} "
21
84
22
85
triggers {
34
97
}
35
98
36
99
# When actual bill exceeds 90% budget
37
- resource "null_resource" "budget_actual_90 " {
100
+ resource "null_resource" "budgets_actual_90 " {
38
101
count = " ${ length (var. budgets )} "
39
102
40
103
triggers {
52
115
}
53
116
54
117
# When actual bill exceeds 80% budget
55
- resource "null_resource" "budget_actual_80 " {
118
+ resource "null_resource" "budgets_actual_80 " {
56
119
count = " ${ length (var. budgets )} "
57
120
58
121
triggers {
0 commit comments