Sub topic 14
Budgets, anomaly detection, and tagging
Earlier chapters showed how to find and reduce transfer costs. This chapter covers how to keep them down. It assumes you know the transfer categories from the first chapters, can use the AWS CLI, and have permission to use Billing and Cost Management in a payer or standalone account.
The three controls
Each control answers a different question.
| Control | Question it answers | Typical delay |
|---|---|---|
| AWS Budgets | Did spend cross a limit I set? | Billing data updates several times a day |
| Cost Anomaly Detection | Did spend change in a way that is unusual for this account? | Detection can take up to about a day |
| Cost allocation tags | Which team or workload caused the spend? | Up to 24 hours after activation |
Budget on a transfer usage type
A budget on total account spend hides transfer problems, because transfer is a small slice of a large bill. Filter the budget to a usage type group instead. This budget tracks inter-AZ transfer only.
Save this as budget.json:
{
"BudgetName": "inter-az-transfer-monthly",
"BudgetType": "COST",
"TimeUnit": "MONTHLY",
"BudgetLimit": { "Amount": "500", "Unit": "USD" },
"CostFilters": { "UsageTypeGroup": ["EC2: Data Transfer - Inter AZ"] }
}Save this as notifications.json:
[
{
"Notification": {
"NotificationType": "FORECASTED",
"ComparisonOperator": "GREATER_THAN",
"Threshold": 80,
"ThresholdType": "PERCENTAGE"
},
"Subscribers": [
{ "SubscriptionType": "EMAIL", "Address": "[email protected]" }
]
}
]Then create the budget:
aws budgets create-budget \
--account-id 123456789012 \
--budget file://budget.json \
--notifications-with-subscribers file://notifications.jsonA FORECASTED notification fires when projected month-end spend passes 80 percent of the limit. That gives you time to act before the bill arrives.
Anomaly detection
A fixed limit cannot catch a sudden jump that stays below the limit. Cost Anomaly Detection learns your usual pattern for each monitored segment and flags deviations. A service-level monitor is a good starting point because NAT gateway, EC2, and CloudFront charges appear as separate services.
aws ce create-anomaly-monitor \
--anomaly-monitor '{"MonitorName":"by-service","MonitorType":"DIMENSIONAL","MonitorDimension":"SERVICE"}'
aws ce create-anomaly-subscription \
--anomaly-subscription '{
"SubscriptionName": "transfer-alerts",
"MonitorArnList": ["arn:aws:ce::123456789012:anomalymonitor/EXAMPLE-ID"],
"Subscribers": [{"Type": "EMAIL", "Address": "[email protected]"}],
"Frequency": "DAILY",
"ThresholdExpression": {
"Dimensions": {
"Key": "ANOMALY_TOTAL_IMPACT_ABSOLUTE",
"MatchOptions": ["GREATER_THAN_OR_EQUAL"],
"Values": ["100"]
}
}
}'Replace the monitor ARN with the one returned by the first command. The threshold means you are only alerted when the estimated impact is at least 100 USD.
Tagging for attribution
An alert says that spend rose. A tag says whose workload caused it. Tags do not appear in billing data until you activate them as cost allocation tags.
aws ce update-cost-allocation-tags-status \
--cost-allocation-tags-status TagKey=team,Status=ActiveKeep these limits in mind:
- Activation is not retroactive. Costs from before activation carry no tag.
- Tag the resources that generate transfer, such as NAT gateways, load balancers, and instances. Untagged resources land in an "untagged" group in Cost Explorer.
- Use a small, fixed set of keys, such as
teamandenvironment. Inconsistent capitalization (Teamagainstteam) creates separate keys. - Some transfer charges cannot be tied to one workload, such as traffic through a shared NAT gateway. For those, use Flow Logs to split the cost by source address.
Response routine
When an alert arrives, group last month's cost by usage type in Cost Explorer, filter by the team tag, and run the Flow Logs top-talkers query on the affected VPC. Then apply the matching fix from earlier chapters.
Exercise
Your total monthly budget of 20,000 USD never alerts, yet NAT gateway data processing grew from 300 USD to 2,500 USD. Name two controls that would have caught this, and say why each works.
Show answer
A budget filtered to the NAT gateway usage type (or its service) with a limit near 400 USD would alert, because the filter removes the noise of other spend. A Cost Anomaly Detection monitor would also flag it, because it compares the jump against the learned baseline instead of a fixed limit. Tags then identify which team's workload to investigate.