AWS Budgets allows users to set custom budgets to track their costs and usage and trigger email or SNS notifications when the defined thresholds are exceeded. In this quick blog post, let's learn how to set up AWS budgets and notification triggers using a Terraform module.
Here's a breakdown of the components involved:
Cost Budget Module (./modules/cost_budgets/total_cost_budget.tf)
In total_cost_budget.tf
, let's define the aws_budgets_budget
resource. This resource will set a monthly budget and configure notifications based on actual and forecasted costs. In the following example, the actual cost notification gets triggered when actual costs exceed 80% of the budget and the forecasted cost notification gets triggered when forecasted costs exceed 100% of the budget. Both notifications are sent to a specified email address (e.g. sysadmin@yourcompany.com
). Also, let's include recurring reservation charges and exclude various other charges such as taxes, refunds, and credits. This helps in focusing on the recurring costs that are more predictable and manageable.
resource "aws_budgets_budget" "cost" {
name = "Monthly Recurring Cost Before Tax"
budget_type = "COST"
limit_amount = var.cost_budget_limit_amount
limit_unit = "USD"
time_unit = "MONTHLY"
# Actual cost > 80%
notification {
comparison_operator = "GREATER_THAN"
threshold = 80
threshold_type = "PERCENTAGE"
notification_type = "ACTUAL"
subscriber_email_addresses = ["sysadmin@yourcompany.com"]
}
# Forecasted cost > 100%
notification {
comparison_operator = "GREATER_THAN"
threshold = 100
threshold_type = "PERCENTAGE"
notification_type = "FORECASTED"
subscriber_email_addresses = ["sysadmin@yourcompany.com"]
}
cost_types {
# Include: Recurring reservation charges
include_recurring = true
# Exclude: Taxes, Refunds, Credits, Upfront reservation fees, Other subscription costs, Support charges, Discounts
include_tax = false
include_refund = false
include_credit = false
include_upfront = false
include_other_subscription = false
include_subscription = false
include_support = false
include_discount = false
# Cost aggregated by: Unblended costs
use_blended = false
}
}
To learn more about the available cost and usage budget options, please check the terraform documentation here: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/budgets_budget
Variables File (variables.tf
)
Let's also define the variables.tf
file which includes the input variable cost_budget_limit_amount
that determines the budget limit amount in USD.
variable "cost_budget_limit_amount" {
type = string
default = "1000"
description = "USD limit amount used for cost budget"
}
Main Configuration File (main.tf
)
Finally, let's include this module in the main.tf
file which is the entry point of our Terraform configuration. It will load our custom module for AWS cost budgets.
# Load aws cost budget
module "cost_budgets" {
source = "./modules/cost_budgets"
cost_budget_limit_amount = var.cost_budget_limit_amount
}
Using the Code in Your Project
- Save the above code snippets (
main.tf
,variables.tf
, andtotal_cost_budget.tf
) in their respective directories within your Terraform project. - Update the
cost_budget_limit_amount
variable invariables.tf
with your desired budget limit. - (Optional) Modify the notification email address in
total_cost_budget.tf
. - Run
terraform init
to initialize Terraform and download the required providers. - Run
terraform plan
to preview the changes Terraform will make. - If the plan looks good, run
terraform apply
to create the AWS budget resources.
Conclusion
This Terraform module provides a simple yet effective way to set up AWS cost budgets and notification triggers. By automating budget management and receiving cost alerts, you can gain better control over your AWS spending and optimize your cloud costs. Also, it is a safety net for detecting unintended usage patterns and if you manage multiple AWS accounts, this approach will save you a lot of time and reduce manual errors.
If you run into any issues or have any suggestions, please let me know in the comments below :)
Stay tuned for the next DevOps tip. Until then, happy coding!