|
| 1 | +--- |
| 2 | +page_title: Create cloud budgets |
| 3 | +description: Create cloud budgets and spending alerts using Terraform for AWS, Azure, and GCP. Implement cost monitoring, anomaly detection, and automated notifications with infrastructure as code. |
| 4 | +--- |
| 5 | + |
| 6 | +# Create cloud budgets |
| 7 | + |
| 8 | +Cloud spending can quickly get out of control without proper oversight and management. According to the [2023 HashiCorp State of Cloud Strategy Survey](https://www.hashicorp.com/en/blog/hashicorp-state-of-cloud-strategy-survey-2023-maturity-drives-operational-efficiency), 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. |
| 9 | + |
| 10 | +Implementing a budget provides you with the following benefits: |
| 11 | + |
| 12 | +- **Visibility into cloud spending:** Understand where your money is going. |
| 13 | +- **Proactive cost management:** Take action before costs exceed budgets. |
| 14 | +- **Notification of spending anomalies:** Get alerts when spending patterns change. |
| 15 | +- **Ability to improve financial planning and forecasting:** Use historical data to make informed budget decisions. |
| 16 | + |
| 17 | +<Note> |
| 18 | + |
| 19 | +The Terraform example in this document uses the `tags` block. Refer to the [Tag cloud resources](/well-architected-framework/optimize-systems/lifecycle-management/tag-cloud-resources) document to learn about implementing a tagging strategy. |
| 20 | + |
| 21 | +</Note> |
| 22 | + |
| 23 | +## Create spending limits and notifications |
| 24 | + |
| 25 | +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. |
| 26 | + |
| 27 | +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. |
| 28 | + |
| 29 | +If you're tracking resources by tags, it is important to have a well-defined tagging strategy to ensure budgets are applied correctly. Terraform can help you enforce tagging policies and ensure that all resources are tagged consistently. Creating infrastructure manually can lead to incorrect or missing tags on resources and result in inaccurate budget tracking. |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +```hcl |
| 34 | +resource "aws_budgets_budget" "ec2" { |
| 35 | + name = "budget-ec2-monthly" |
| 36 | + budget_type = "COST" |
| 37 | + limit_amount = "1200" |
| 38 | + limit_unit = "USD" |
| 39 | + time_period_end = "2087-06-15_00:00" |
| 40 | + time_period_start = "2017-07-01_00:00" |
| 41 | + time_unit = "MONTHLY" |
| 42 | +
|
| 43 | + cost_filter { |
| 44 | + name = "Service" |
| 45 | + values = [ |
| 46 | + "Amazon Elastic Compute Cloud - Compute", |
| 47 | + ] |
| 48 | + } |
| 49 | +
|
| 50 | + notification { |
| 51 | + comparison_operator = "GREATER_THAN" |
| 52 | + threshold = 100 |
| 53 | + threshold_type = "PERCENTAGE" |
| 54 | + notification_type = "FORECASTED" |
| 55 | + subscriber_email_addresses = ["test@example.com"] |
| 56 | + } |
| 57 | +
|
| 58 | + tags = { |
| 59 | + Environment = "production" |
| 60 | + Team = "engineering" |
| 61 | + ManagedBy = "terraform" |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +Some of the key components in the previous example include: |
| 67 | + |
| 68 | +- **limit_amount:** Defines the monthly spend limit. |
| 69 | +- **notification:** Defines the notification criteria, including the recipient email. |
| 70 | +- **tags:** Applies tags to the budget resource, not the EC2 instance. Tags allow you to filter and organize budgets in the billing console. |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +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. |
| 75 | + |
| 76 | +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. |
| 77 | + |
| 78 | +HashiCorp resources: |
| 79 | + |
| 80 | +- Learn how to [Tag cloud resources](/well-architected-framework/optimize-systems/lifecycle-management/tag-cloud-resources) |
| 81 | +- Terraform resource: [aws_budgets_budget](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/budgets_budget) |
| 82 | +- Terraform resource: [azurerm_consumption_budget_subscription](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/consumption_budget_subscription) |
| 83 | +- Terraform resource: [google_billing_budget](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/billing_budget) |
| 84 | + |
| 85 | +External resources: |
| 86 | + |
| 87 | +- AWS Budgets: [Getting started with AWS Budgets](https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-managing-costs.html) |
| 88 | +- Azure Cost Management and Billing: [Create and manage budgets](https://learn.microsoft.com/en-us/azure/cost-management-billing/costs/tutorial-acm-create-budgets) |
| 89 | +- Google Cloud Budgets and alerts: [Creating budgets](https://cloud.google.com/billing/docs/how-to/budgets) |
| 90 | + |
| 91 | +## Detect spending anomalies |
| 92 | + |
| 93 | +Anomaly detection identifies unusual spending patterns rather than absolute thresholds. For example, if your monthly EC2 spending suddenly doubles from $2,000 to $4,000 but remains under your $5,000 budget, a budget alert would not trigger. However, anomaly detection would flag this unusual increase for investigation. Anomaly detection helps you catch issues like misconfigured autoscaling, forgotten resources, or unauthorized usage before they significantly impact costs. |
| 94 | + |
| 95 | +Most cloud providers offer machine learning-based anomaly detection that learns your normal usage patterns and alerts you when spending deviates from the baseline. You can configure anomaly detection with AWS Cost Anomaly Detection and Azure Cost Management using Terraform. |
| 96 | + |
| 97 | +The following is an example Terraform configuration that sets up cost anomaly detection with email alerts in AWS. This cost anomaly detection will detect the previous EC2 scenario. |
| 98 | + |
| 99 | +```hcl |
| 100 | +resource "aws_ce_anomaly_monitor" "test" { |
| 101 | + name = "AWSServiceMonitor" |
| 102 | + monitor_type = "DIMENSIONAL" |
| 103 | + monitor_dimension = "SERVICE" |
| 104 | +} |
| 105 | +
|
| 106 | +resource "aws_ce_anomaly_subscription" "test" { |
| 107 | + name = "DAILYSUBSCRIPTION" |
| 108 | + frequency = "DAILY" |
| 109 | +
|
| 110 | + monitor_arn_list = [ |
| 111 | + aws_ce_anomaly_monitor.test.arn |
| 112 | + ] |
| 113 | +
|
| 114 | + subscriber { |
| 115 | + type = "EMAIL" |
| 116 | + address = "abc@example.com" |
| 117 | + } |
| 118 | +
|
| 119 | + threshold_expression { |
| 120 | + dimension { |
| 121 | + key = "ANOMALY_TOTAL_IMPACT_ABSOLUTE" |
| 122 | + match_options = ["GREATER_THAN_OR_EQUAL"] |
| 123 | + values = ["100"] |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +Some of the key components in the previous example include: |
| 130 | + |
| 131 | +- **aws_ce_anomaly_monitor:** Tracks spending patterns across all AWS services including EC2, S3, and Lambda. |
| 132 | +- **frequency = "DAILY":** Sends a daily summary of detected anomalies. |
| 133 | +- **threshold_expression:** Alerts when the anomaly's financial impact meets or exceeds $100. |
| 134 | + |
| 135 | +HashiCorp resources: |
| 136 | + |
| 137 | +- Terraform resource: [aws_ce_anomaly_subscription](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ce_anomaly_subscription) |
| 138 | +- Terraform resource: [azurerm_cost_anomaly_alert](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cost_anomaly_alert) |
| 139 | + |
| 140 | +External resources: |
| 141 | + |
| 142 | +- [AWS getting started with AWS Cost Anomaly Detection](https://docs.aws.amazon.com/cost-anomaly/latest/userguide/what-is-cost-anomaly.html) |
| 143 | +- [Azure identify anomalies and unexpected changes in cost](https://learn.microsoft.com/en-us/azure/cost-management-billing/understand/analyze-unexpected-charges) |
| 144 | +- [Google cloud anomaly detection overview](https://cloud.google.com/bigquery/docs/anomaly-detection-overview) |
| 145 | + |
| 146 | +## Next steps |
| 147 | + |
| 148 | +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](/well-architected-framework/optimize-systems). |
| 149 | + |
| 150 | +To learn more about managing resources with Terraform, view the following resources: |
| 151 | + |
| 152 | +- [Create reusable infrastructure modules](/well-architected-framework/define-and-automate-processes/define/modules) |
| 153 | +- [Implement CI/CD](/well-architected-framework/define-and-automate-processes/automate/cicd) |
| 154 | +- [Reduce costs with Terraform Cloud ephemeral workspaces](https://www.youtube.com/watch?v=-woCmG8yGdA) |
| 155 | +- [Tag cloud resources](/well-architected-framework/optimize-systems/lifecycle-management/tag-cloud-resources) |
0 commit comments