|
| 1 | +--- |
| 2 | +page_title: Create cloud budgets |
| 3 | +description: |
| 4 | +--- |
| 5 | + |
| 6 | +# Create cloud budgets |
| 7 | + |
| 8 | +Cloud spending can quickly get out of control without proper oversight and controls. 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 gives 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 has a `tags` block. Refer to the [Tag cloud resources](/well-architected-framework/docs/docs/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 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 missed tags on resources, resulting 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 | +In the previous example |
| 67 | +- **limit_amount:** Defines the monthly spend limit. |
| 68 | +- **notification:** Defines the notification criteria and sends an email to test@example.com. |
| 69 | +- **tags:** The tag is applied to the budget resource, not the EC2 instance. The tags allow you to filter and organize budgets in the billing console. |
| 70 | + |
| 71 | +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. |
| 72 | + |
| 73 | +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. |
| 74 | + |
| 75 | +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. |
| 76 | + |
| 77 | +HashiCorp resources: |
| 78 | + |
| 79 | +- Learn how to [Tag cloud resources](/well-architected-framework/docs/docs/optimize-systems/lifecycle-management/tag-cloud-resources) |
| 80 | +- Terraform resource: [aws_budgets_budget](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/budgets_budget) |
| 81 | +- Terraform resource: [azurerm_consumption_budget_subscription](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/consumption_budget_subscription) |
| 82 | +- Terraform resource: [google_billing_budget](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/billing_budget) |
| 83 | + |
| 84 | +External resources: |
| 85 | + |
| 86 | +- AWS Budgets: [Getting started with AWS Budgets](https://docs.aws.amazon.com/cost-management/latest/userguide/budgets-managing-costs.html) |
| 87 | +- Azure Cost Management and Billing: [Create and manage budgets](https://learn.microsoft.com/en-us/azure/cost-management-billing/costs/tutorial-acm-create-budgets) |
| 88 | +- Google Cloud Budgets and alerts: [Creating budgets](https://cloud.google.com/billing/docs/how-to/budgets) |
| 89 | + |
| 90 | +## Detect spending anomalies |
| 91 | + |
| 92 | +Anomaly detection identifies unusual 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. |
| 93 | + |
| 94 | +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 using Terraform for AWS Cost Anomaly Detection and Azure Cost Management. |
| 95 | + |
| 96 | +The following is an example Terraform code that sets up a cost anomaly detection with email alerts in AWS. This cost anomaly detection will detect the previous EC2 scenario. |
| 97 | + |
| 98 | +```hcl |
| 99 | +resource "aws_ce_anomaly_monitor" "test" { |
| 100 | + name = "AWSServiceMonitor" |
| 101 | + monitor_type = "DIMENSIONAL" |
| 102 | + monitor_dimension = "SERVICE" |
| 103 | +} |
| 104 | +
|
| 105 | +resource "aws_ce_anomaly_subscription" "test" { |
| 106 | + name = "DAILYSUBSCRIPTION" |
| 107 | + frequency = "DAILY" |
| 108 | +
|
| 109 | + monitor_arn_list = [ |
| 110 | + aws_ce_anomaly_monitor.test.arn |
| 111 | + ] |
| 112 | +
|
| 113 | + subscriber { |
| 114 | + type = "EMAIL" |
| 115 | + address = "abc@example.com" |
| 116 | + } |
| 117 | +
|
| 118 | + threshold_expression { |
| 119 | + dimension { |
| 120 | + key = "ANOMALY_TOTAL_IMPACT_ABSOLUTE" |
| 121 | + match_options = ["GREATER_THAN_OR_EQUAL"] |
| 122 | + values = ["100"] |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +In the previous example: |
| 129 | +- **aws_ce_anomaly_monitor:** Tracks spending patterns across all AWS services such as EC2, S3, and Lambda. |
| 130 | +- **frequency = "DAILY":** Sends a daily summary of detected anomalies. |
| 131 | +- **threshold_expression:** Only alerts when the anomaly's financial impact exceeds $100. |
| 132 | + |
| 133 | +HashiCorp resources: |
| 134 | + |
| 135 | +- Terraform resource: [aws_ce_anomaly_subscription](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ce_anomaly_subscription) |
| 136 | +- Terraform resource: [azurerm_cost_anomaly_alert](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/cost_anomaly_alert) |
| 137 | + |
| 138 | +External resources: |
| 139 | + |
| 140 | +- AWS Cost Anomaly Detection: [What is AWS Cost Anomaly Detection?](https://docs.aws.amazon.com/cost-anomaly/latest/userguide/what-is-cost-anomaly.html) |
| 141 | +- Azure Cost Management anomalies: [Detect anomalies in your cost data](https://learn.microsoft.com/en-us/azure/cost-management-billing/costs/analyze-cost-data#detect-anomalies-in-your-cost-data) |
| 142 | +- Google Cloud's cost anomaly detection: [Using anomaly detection](https://cloud.google.com/billing/docs/how-to/using-anomaly-detection) |
| 143 | + |
| 144 | +## Next steps |
| 145 | + |
| 146 | +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). |
| 147 | + |
| 148 | +To learn more about managing resources with Terraform, view the following resources: |
| 149 | +- [Create reusable infrastructure modules](/well-architected-framework/define-and-automate-processes/define/modules) |
| 150 | +- [Implement CI/CD](/well-architected-framework/define-and-automate-processes/automate/cicd) |
| 151 | +- [Reduce costs with Terraform Cloud ephemeral workspaces](https://www.youtube.com/watch?v=-woCmG8yGdA) |
| 152 | +- [Tag cloud resources](/well-architected-framework/docs/docs/optimize-systems/lifecycle-management/tag-cloud-resources) |
0 commit comments