Well-Architected Framework
How to catch cloud spending anomalies before they spike
Monitoring for cloud spending anomalies help you identify cost issues that budgets miss. For example, if your monthly cloud 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.
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.
Set up anomaly detection in AWS
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.
resource "aws_ce_anomaly_monitor" "test" {
name = "AWSServiceMonitor"
monitor_type = "DIMENSIONAL"
monitor_dimension = "SERVICE"
}
resource "aws_ce_anomaly_subscription" "test" {
name = "DAILYSUBSCRIPTION"
frequency = "DAILY"
monitor_arn_list = [
aws_ce_anomaly_monitor.test.arn
]
subscriber {
type = "EMAIL"
address = "abc@example.com"
}
threshold_expression {
dimension {
key = "ANOMALY_TOTAL_IMPACT_ABSOLUTE"
match_options = ["GREATER_THAN_OR_EQUAL"]
values = ["100"]
}
}
}
Some of the key components in the previous example include:
- aws_ce_anomaly_monitor: Tracks spending patterns across all AWS services including EC2, S3, and Lambda.
- frequency = "DAILY": Sends a daily summary of detected anomalies.
- threshold_expression: Alerts when the anomaly's financial impact meets or exceeds $100.
HashiCorp resources
- Terraform resource: aws_ce_anomaly_subscription
- Terraform resource: azurerm_cost_anomaly_alert
- Start learning Terraform with the Get started tutorials.
External resources
- AWS getting started with AWS Cost Anomaly Detection
- Azure identify anomalies and unexpected changes in cost
- Google cloud anomaly detection overview
In this section of Manage cost, you learned about detecting cloud spending anomalies using Terraform. Create cloud budgets is part of the Optimize systems.
To learn more about managing resources with Terraform, view the following resources: