Skip to content
This repository was archived by the owner on Mar 30, 2026. It is now read-only.

Commit bb50ffd

Browse files
authored
Merge pull request #49 from meshcloud/feature/azure-budget-alert-polishing
feat: Azure budget alert for subscriptions
2 parents 40880c0 + 1d4111f commit bb50ffd

3 files changed

Lines changed: 67 additions & 56 deletions

File tree

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
1+
locals {
2+
start_date = formatdate("YYYY-MM-01'T'hh:mm:ssZ", timestamp())
3+
end_date = formatdate("2026-MM-01'T'hh:mm:ssZ", local.start_date)
4+
location = "germanywestcentral"
5+
action_group_name = "budget_action_group"
6+
action_group_short_name = "bdgt_ag"
7+
resource_group_for_action_group_name = "rg-${local.action_group_name}"
8+
}
29
//-----------------------------------------------
310
// Budget on subscription level
411
//-----------------------------------------------
@@ -10,43 +17,29 @@ resource "azurerm_consumption_budget_subscription" "subscription_budget" {
1017
name = var.budget_name
1118
subscription_id = data.azurerm_subscription.subscription.id
1219

13-
amount = 1000
20+
amount = var.monthly_budget_amount
1421
time_grain = "Monthly"
1522

1623
time_period {
17-
start_date = var.start_date #"2022-06-01T00:00:00Z"
18-
end_date = var.end_date #"2022-07-01T00:00:00Z"
24+
start_date = local.start_date #"2022-06-01T00:00:00Z"
25+
end_date = local.end_date #"2022-07-01T00:00:00Z"
1926
}
2027

21-
# filter {
22-
# dimension {
23-
# name = "ResourceGroupName"
24-
# values = [
25-
# azurerm_resource_group.example.name,
26-
# ]
27-
# }
28-
29-
# tag {
30-
# name = "foo"
31-
# values = [
32-
# "bar",
33-
# "baz",
34-
# ]
35-
# }
36-
# }
37-
3828
notification {
3929
enabled = true
40-
threshold = 90.0
30+
threshold = var.actual_threshold
4131
operator = "EqualTo"
4232

4333
contact_emails = var.contact_emails
4434

35+
contact_groups = [
36+
azurerm_monitor_action_group.action_group.id,
37+
]
4538
}
4639

4740
notification {
48-
enabled = false
49-
threshold = 100.0
41+
enabled = true
42+
threshold = var.forcasted_threshold
5043
operator = "GreaterThan"
5144
threshold_type = "Forecasted"
5245

@@ -63,11 +56,18 @@ resource "azurerm_consumption_budget_subscription" "subscription_budget" {
6356
// Action Group
6457
//-----------------------------------------------
6558
resource "azurerm_resource_group" "rg_action_group" {
66-
name = var.resource_group_for_action_group_name
67-
location = var.location
59+
name = local.resource_group_for_action_group_name
60+
location = local.location
6861
}
6962
resource "azurerm_monitor_action_group" "action_group" {
70-
name = "budget_action_group"
63+
name = local.action_group_name
7164
resource_group_name = azurerm_resource_group.rg_action_group.name
72-
short_name = "bdgt_ag"
65+
short_name = local.action_group_short_name
66+
67+
68+
webhook_receiver {
69+
name = "callmyapiaswell"
70+
service_uri = "http://example.com/alert"
71+
use_common_alert_schema = true
72+
}
7373
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
output "Budget_notification" {
22
value = azurerm_consumption_budget_subscription.subscription_budget.notification
33
}
4+
5+
output "start_date" {
6+
value = azurerm_consumption_budget_subscription.subscription_budget.time_period[0].start_date
7+
}
Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,52 @@
1-
2-
variable "location" {
3-
type = string
4-
default = "westeurope"
5-
}
6-
71
variable "budget_name" {
8-
type = string
9-
default = "budget_alert"
2+
type = string
3+
description = "Name of the budget alert rule"
4+
default = "budget_alert"
105
}
116

127
variable "subscription_id" {
138
type = string
149
description = "The ID of the subscription at which you want to assign the budget"
1510
}
1611

17-
variable "resource_group_for_action_group_name" {
18-
type = string
19-
description = "The name of the resource group to deploy the action group inside"
20-
default = "rg_action_group"
12+
variable "contact_emails" {
13+
type = list(string)
14+
description = "List of emails of the users who should receive the Budget alert. e.g. ['foo@example.com', 'bar@example.com']"
2115
}
2216

23-
variable "end_date" {
24-
type = string
25-
description = "End date of the budget rule, Timestamp in the format YYYY-MM-DDTHH:MM:SSZ"
26-
validation {
27-
condition = can(regex("^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T\\d{2}:\\d{2}:\\d{2}Z$", var.end_date))
28-
error_message = "Timestamp must be in the format YYYY-MM-DDTHH:MM:SSZ"
29-
}
17+
variable "monthly_budget_amount" {
18+
type = number
19+
description = "Set the monthly budget goal for this subscription"
20+
default = 100
3021
}
3122

32-
variable "start_date" {
23+
variable "actual_threshold" {
3324
type = string
34-
description = "Start date of the budget rule, should be first day of the month"
35-
validation {
36-
condition = can(regex("^\\d{4}-(0[1-9]|1[0-2])-(01)T\\d{2}:\\d{2}:\\d{2}Z$", var.start_date))
37-
error_message = "The 'start_date' must be in the format 'YYYY-MM-01THH:MM:SSZ', e.g., '2023-11-01T00:00:00Z'."
38-
}
25+
description = "The precise percentage of the monthly budget at which you wish to activate the alert upon reaching. E.g. '15' for 15% or '120' for 120%"
3926
}
4027

41-
variable "contact_emails" {
42-
type = list(string)
43-
description = "List of emails of the users who should receive the Budget alert. e.g. ['foo@example.com', 'bar@example.com']"
28+
variable "forcasted_threshold" {
29+
type = string
30+
description = "The forcasted percentage of the monthly budget at which you wish to activate the alert upon reaching. E.g. '15' for 15% or '120' for 120%"
4431
}
4532

33+
34+
// Uncomment next 2 variables if you want to change the start and end date via inputs --------------------------------
35+
# variable "end_date" {
36+
# type = string
37+
# description = "End date of the budget rule, Timestamp in the format YYYY-MM-DDTHH:MM:SSZ"
38+
# validation {
39+
# condition = can(regex("^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T\\d{2}:\\d{2}:\\d{2}Z$", var.end_date))
40+
# error_message = "Timestamp must be in the format YYYY-MM-DDTHH:MM:SSZ"
41+
# }
42+
# }
43+
44+
# variable "start_date" {
45+
# type = string
46+
# description = "Start date of the budget rule, should be first day of the month. Timestamp in the format YYYY-MM-DDTHH:MM:SSZ"
47+
# validation {
48+
# condition = can(regex("^\\d{4}-(0[1-9]|1[0-2])-(01)T\\d{2}:\\d{2}:\\d{2}Z$", var.start_date))
49+
# error_message = "The 'start_date' must be in the format 'YYYY-MM-01THH:MM:SSZ', e.g., '2023-11-01T00:00:00Z'."
50+
# }
51+
# }
52+
//------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)