Well-Architected Framework
Create cloud budgets
Cloud spending can quickly get out of control without proper oversight and management. According to the 2023 HashiCorp State of Cloud Strategy Survey, 94% of respondents experienced avoidable cloud costs. Proactive budget creation, automated alerts, and anomaly detection give you the visibility and control you need to maintain predictable spending and prevent cost overruns before they occur.
Implementing a budget provides you with the following benefits:
- Visibility into cloud spending: Understand where your money is going.
- Proactive cost management: Take action before costs exceed budgets.
- Notification of spending anomalies: Get alerts when spending patterns change.
- Ability to improve financial planning and forecasting: Use historical data to make informed budget decisions.
Create spending limits and notifications
Most major cloud providers offer native tools to create budgets. These native tools allow you to set budget thresholds, monitor spending, and receive alerts when spending approaches or exceeds defined limits.
You can use Terraform to define and manage cloud budgets across your organization. You can create Terraform modules to create budgets for different teams, projects, or environments. These modules can automatically apply appropriate budget thresholds, alerting mechanisms, and spending limits to new or existing cloud resources.
If you're tracking resources by tags, it is important to have a well-defined tagging strategy to ensure you apply budgets correctly. Terraform enforces tagging policies and tags all resources consistently. Creating infrastructure manually can lead to incorrect or missing tags on resources and result in inaccurate budget tracking.
Cloud budget configuration with Terraform
The following is an example of a Terraform configuration that creates an AWS EC2 budget. This budget tracks EC2 instance costs and sends an alert to test@example.com when the forecasted cost exceeds 100% of the budget. You can set similar budgets and alerts for other cloud providers, such as Azure and GCP.
resource "aws_budgets_budget" "ec2" {
name = "budget-ec2-monthly"
budget_type = "COST"
limit_amount = "1200"
limit_unit = "USD"
time_period_end = "2087-06-15_00:00"
time_period_start = "2017-07-01_00:00"
time_unit = "MONTHLY"
cost_filter {
name = "Service"
values = [
"Amazon Elastic Compute Cloud - Compute",
]
}
notification {
comparison_operator = "GREATER_THAN"
threshold = 100
threshold_type = "PERCENTAGE"
notification_type = "FORECASTED"
subscriber_email_addresses = ["test@example.com"]
}
tags = {
Environment = "production"
Team = "engineering"
ManagedBy = "terraform"
}
}
Some of the key components in the previous example include:
- limit_amount: Defines the monthly spend limit.
- notification: Defines the notification criteria, including the recipient email.
- tags: Applies tags to the budget resource, not the EC2 instance. Tags allow you to filter and organize budgets in the billing console.
For AWS environments, you can use the aws_budgets_budget resource to create budgets that track spending by service, linked account, tag, or other dimensions. You can specify the budget amount, time period, and notification thresholds.
For Azure environments, the azurerm_consumption_budget_subscription resource lets you create subscription-level budgets with similar notification capabilities. You can define multiple notification rules that trigger at different spending thresholds.
For Google Cloud Platform, the google_billing_budget resource operates at the billing account level, and you can filter by project, service, or label. GCP budgets support both actual and forecasted spending alerts.
HashiCorp resources
- Learn how to Tag cloud resources
- Start learning Terraform with the Get started tutorials.
- Terraform resource: aws_budgets_budget
- Terraform resource: azurerm_consumption_budget_subscription
- Terraform resource: google_billing_budget
External resources
- AWS Budgets: Getting started with AWS Budgets
- Azure Cost Management and Billing: Create and manage budgets
- Google Cloud Budgets and alerts: Creating budgets
Next steps
In this section of Manage cost, you learned about creating budgets and alerts to manage and control cloud spending, including creating spending limits with cloud provider budgets and detecting spending anomalies automatically. Create cloud budgets is part of the Optimize systems.
To learn more about managing resources with Terraform, view the following resources: