Sub topic 15
Acting on the numbers: Budgets and anomaly alerts
This chapter assumes you can read a Cost Explorer report, you have activated at least one cost allocation tag (see the chapters on tags and activation), and you can run aws commands with a profile that has billing permissions. Cost Explorer shows what happened. AWS Budgets and Cost Anomaly Detection tell you when something needs attention, without anyone opening a report.
Two tools, two questions
The two services answer different questions.
| Question | Service | How it decides to alert |
|---|---|---|
| Are we on track to exceed a number we chose? | AWS Budgets | Compares actual or forecasted spend with a threshold you set |
| Did spend change in a way we did not expect? | Cost Anomaly Detection | Compares spend with a pattern learned from your past usage |
A budget needs you to know the expected number in advance. An anomaly monitor does not. Most teams use both. Budgets catch slow drift and planned limits. Anomaly detection catches sudden jumps, such as a misconfigured autoscaling group, that stay below any budget threshold for days.
AWS Budgets
Budget types
A budget tracks one kind of measurement over a period (monthly, quarterly, annual, or daily).
- Cost budgets track spend in dollars. This is the type you will use most.
- Usage budgets track a quantity, such as GB-months of S3 storage or EC2 hours.
- Reservation and Savings Plans budgets track utilization or coverage. They alert when commitments go unused or when too much usage runs at on-demand rates.
Scoping a budget with tags
A budget covers the whole account by default. You narrow it with filters: service, linked account, region, usage type, and tag. The tag filter is what connects this chapter to the earlier ones. A budget for "the checkout project in prod" is only possible if Project and Environment are active cost allocation tags.
In the API, a tag filter is written as user:<Key>$<Value>. User-defined tags carry the user: prefix. AWS-generated tags carry aws:.
Creating a budget from the CLI
The following command creates a 2,000 USD monthly budget for spend tagged Project=checkout. It takes the budget definition and the notifications from two JSON files. Replace 111122223333 with your account ID and run it from the management account or the account that owns the budget.
aws budgets create-budget \
--account-id 111122223333 \
--budget file://budget.json \
--notifications-with-subscribers file://notifications.jsonbudget.json:
{
"BudgetName": "checkout-monthly",
"BudgetType": "COST",
"TimeUnit": "MONTHLY",
"BudgetLimit": { "Amount": "2000", "Unit": "USD" },
"CostFilters": { "TagKeyValue": ["user:Project$checkout"] }
}notifications.json:
[
{
"Notification": {
"NotificationType": "ACTUAL",
"ComparisonOperator": "GREATER_THAN",
"Threshold": 80,
"ThresholdType": "PERCENTAGE"
},
"Subscribers": [
{ "SubscriptionType": "EMAIL", "Address": "[email protected]" }
]
},
{
"Notification": {
"NotificationType": "FORECASTED",
"ComparisonOperator": "GREATER_THAN",
"Threshold": 100,
"ThresholdType": "PERCENTAGE"
},
"Subscribers": [
{ "SubscriptionType": "EMAIL", "Address": "[email protected]" }
]
}
]Actual versus forecasted alerts
The two notifications above do different jobs.
- An actual alert at 80 percent fires after you have spent 1,600 USD. It reports something that has already happened.
- A forecasted alert at 100 percent fires when AWS predicts that month-end spend will pass 2,000 USD. It can arrive in the middle of the month, while you can still act. The forecast needs some usage history, so it may not work for a brand-new account or a new tag value.
A common pattern is an actual alert at 50 and 80 percent, plus a forecasted alert at 100 percent.
Budget actions
A budget can also do something when a threshold is crossed. You can attach an IAM policy to a user, group, or role, attach a service control policy to an organizational unit or account, or stop specific EC2 or RDS instances. You can set these to run automatically or only after someone approves them.
Limits to remember
- Budget data is not real time. It refreshes several times a day, so an alert can lag the spending that caused it.
- A budget alerts. It does not cap spending, except through an action you configure.
- A tag filter only matches spend from the date the tag was activated. Spend before that date is not retroactively tagged, so a budget on a new tag starts low.
- Budgets beyond the free allowance, and budgets with actions, may have a charge. Check current pricing before you create many.
Cost Anomaly Detection
How it works
Cost Anomaly Detection uses a machine learning model trained on your own spend history. It is not tied to a threshold. A daily spend of 500 USD is normal for one service and a serious jump for another, and the model learns the difference. It is also built on billing data, so an anomaly usually appears a day or so after the usage that caused it.
When it finds one, it reports the anomaly with:
- The total cost impact, in dollars.
- The suspected root causes, listing the service, account, region, and usage type that account for the increase.
Monitors
A monitor defines which spend to watch. There are four kinds.
| Monitor type | Watches | Good for |
|---|---|---|
| AWS services | Each AWS service separately | A general safety net. This is the usual first monitor. |
| Linked account | Each member account | Organizations that give each team its own account |
| Cost category | Each value of a Cost Category | Teams or business units already modelled with rules |
| Cost allocation tag | Each value of a chosen tag key | Projects or environments tracked by tag |
A tag monitor is the natural follow-up to a tagging strategy. With a monitor on Project, a spike in checkout is judged against checkout's own history and not diluted in an account-wide total. The tag key must be active as a cost allocation tag first.
Subscriptions and thresholds
A subscription connects one or more monitors to the people who should hear about their anomalies. It has three settings.
- Recipients: email addresses or an Amazon SNS topic.
- Frequency:
IMMEDIATEsends an alert to an SNS topic as soon as an anomaly is found.DAILYandWEEKLYsend summaries by email. - Threshold: the minimum impact worth reporting, as an absolute dollar amount or a percentage.
The threshold is how you control noise. If every 5 USD blip sends an email, people will filter the alerts out.
Creating a monitor and subscription from the CLI
The following commands create a service-level monitor, then a daily email subscription that reports anomalies with an impact of at least 100 USD. The first command returns a monitor ARN. Put that ARN into the subscription file.
aws ce create-anomaly-monitor \
--anomaly-monitor MonitorName=all-services,MonitorType=DIMENSIONAL,MonitorDimension=SERVICE
aws ce create-anomaly-subscription \
--anomaly-subscription file://subscription.jsonsubscription.json:
{
"SubscriptionName": "daily-over-100-usd",
"MonitorArnList": [
"arn:aws:ce::111122223333:anomalymonitor/EXAMPLE-MONITOR-ID"
],
"Subscribers": [
{ "Type": "EMAIL", "Address": "[email protected]" }
],
"Frequency": "DAILY",
"ThresholdExpression": {
"Dimensions": {
"Key": "ANOMALY_TOTAL_IMPACT_ABSOLUTE",
"MatchOptions": ["GREATER_THAN_OR_EQUAL"],
"Values": ["100"]
}
}
}Choosing between them
Use these rules to decide where each control belongs.
- Put a budget on every spend boundary that someone owns: each project, each environment, and the account as a whole.
- Put an anomaly monitor on the same boundaries, starting with one for AWS services.
- Use forecasted budget alerts for early warning and actual alerts for confirmation.
- Send alerts to a shared mailbox or SNS topic, not to one person.
- Review the thresholds after a month. If alerts are ignored, raise the threshold. If a real overspend went unreported, lower it.
Common mistakes
- Budget filtered on an inactive tag. The budget stays near zero because no tagged spend reaches it. Check activation first with
aws ce list-cost-allocation-tags --status Active. - Untagged spend escapes every tag budget. A project budget covers only tagged resources. Keep an account-level budget as well, so untagged spend still triggers an alert.
- Alerts go to one inbox. If that person leaves, nobody hears about the next overspend.
- Thresholds set once and never revisited. Spend grows. A limit that fit in January produces constant alerts by June.
- Treating an anomaly alert as proof of a problem. It means the spend differs from the pattern. A planned launch will trigger one, and that is expected.
Exercise
Your team tags every resource with Project and Environment, and both keys are active as cost allocation tags. Spend for the checkout project in prod normally stays around 1,500 USD a month. Last month a runaway job added 900 USD of spend over two days, and nobody noticed until the invoice. Which two controls would you set up to catch this sooner, and how would you configure each?
Show answer
Create a cost budget filtered to user:Project$checkout (and Environment prod if you want it narrower) with a limit near 1,500 to 2,000 USD. Add a forecasted alert at 100 percent so you hear about the overrun mid-month, plus an actual alert at 80 percent. Also create a cost allocation tag anomaly monitor on the Project key, with a subscription that has a threshold (for example 100 USD absolute impact) and sends to a shared mailbox or SNS topic. The budget catches the month heading over its limit. The monitor catches the two-day spike against checkout's own history, usually within a day or so of the billing data arriving.