Sub topic 12
Creating a monthly budget with alerts
This chapter shows how to create a monthly cost budget in AWS Budgets and attach an email alert to it. It assumes you have finished the earlier chapters. You have an AWS account with MFA on the root user, a separate administrative identity for daily work, and billing access for that identity. You should be comfortable running AWS CLI commands and reading JSON.
What a budget is
An AWS budget is a saved rule that compares your spending (or usage) against a number you choose. When the comparison crosses a threshold, AWS Budgets sends a notification. A budget can track several things:
| Budget type | What it tracks | Example |
|---|---|---|
| Cost | Dollar spend | "20 USD per month" |
| Usage | Quantity of a service used | "750 hours of EC2 per month" |
| Savings Plans and Reservation | Coverage or utilization of commitments | "Keep utilization above 80%" |
This chapter uses a cost budget, which is the right first budget for a new account. It answers the question you care about on day one: is my bill heading somewhere I did not expect?
How the pieces fit together
A budget has two parts that you define separately:
- The budget itself. A name, an amount, a time unit (monthly, quarterly, annual), and a type.
- Notifications with subscribers. Each notification names a trigger, such as "actual spend is greater than 80 percent of the budget". It also lists who is told, by email address or by an Amazon SNS topic.
The CLI mirrors this split. create-budget takes one --budget document and one --notifications-with-subscribers document.
Before you create the budget
Check these three things first. Most failures at this step come from one of them.
- You are not using the root user. Run
aws sts get-caller-identityand confirm the ARN is your admin identity, notarn:aws:iam::<account-id>:root. - Your identity can use Budgets. An administrator policy covers it. A narrower policy needs
budgets:ViewBudgetandbudgets:ModifyBudget. - IAM access to billing is turned on. By default, IAM users and roles cannot see billing information, even with admin policies. The root user must activate "IAM user and role access to Billing information" once, in the account settings. This is one of the few root-only tasks. If you skipped it, do it now, then sign out of root again.
Also have your 12-digit account ID ready. The examples below use the placeholder 111122223333. Replace it with your own.
Step 1: define the budget
Save the following as budget.json:
{
"BudgetName": "monthly-cost-budget",
"BudgetLimit": { "Amount": "20", "Unit": "USD" },
"TimeUnit": "MONTHLY",
"BudgetType": "COST"
}Each field has a purpose:
BudgetNamemust be unique within the account. Use a name that says what the budget covers.BudgetLimit.Amountis a string, not a number. Quote it.Unitis a currency code.USDis the usual choice, and your billing currency should match it.TimeUnitofMONTHLYresets the comparison at the start of each calendar month.BudgetTypeofCOSTtracks dollar spend.
Pick a limit just above what you expect to spend. Choose 20 USD for a learning account, not 2,000 USD. A budget set far above normal spend will not warn you until the damage is done.
Step 2: define the alert
Save this as notifications.json:
[
{
"Notification": {
"NotificationType": "ACTUAL",
"ComparisonOperator": "GREATER_THAN",
"Threshold": 80,
"ThresholdType": "PERCENTAGE"
},
"Subscribers": [
{ "SubscriptionType": "EMAIL", "Address": "[email protected]" }
]
}
]This is a list, so you can add more than one notification. The fields mean:
NotificationTypeisACTUALorFORECASTED.ACTUALfires when real charges cross the line.FORECASTEDfires when AWS predicts you will cross it by the end of the period.ComparisonOperatorisGREATER_THAN,LESS_THAN, orEQUAL_TO. For cost alerts you almost always wantGREATER_THAN.Thresholdis a number. WithThresholdTypeset toPERCENTAGE, 80 means 80 percent of the budget, which is 16 USD here. WithABSOLUTE_VALUE, it is a dollar amount.Subscriberslists up to 10 email addresses per notification. Use an address that more than one person can read, or one that reaches you quickly. Do not use an address that depends on the account you are protecting.
Add a forecast alert
An actual-spend alert tells you about money already spent. A forecast alert gives earlier warning. Add a second entry to the array in 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]" }
]
}
]Forecasts need usage history, so a brand-new account may not trigger one in its first weeks. The actual-spend alert works from day one. Keep both.
Step 3: create the budget
Run the command from the directory that holds both files:
aws budgets create-budget \
--account-id 111122223333 \
--budget file://budget.json \
--notifications-with-subscribers file://notifications.jsonOn success the command prints nothing and exits with status 0. The file:// prefix tells the CLI to read the JSON from disk. Without it, the CLI treats the argument as inline JSON and fails with a parse error.
Step 4: confirm it exists
List the budgets, then list the notifications for the one you made:
aws budgets describe-budgets --account-id 111122223333
aws budgets describe-notifications-for-budget \
--account-id 111122223333 \
--budget-name monthly-cost-budgetYou can also open Billing and Cost Management in the console and choose Budgets. The console shows the current spend as a bar against the limit.
Common errors
| Error | Likely cause | Fix |
|---|---|---|
| `AccessDeniedException` | Your identity lacks `budgets:ModifyBudget`, or IAM billing access is off | Attach the permission, or have root activate IAM billing access |
| `DuplicateRecordException` | A budget with that name already exists | Choose another name, or use `update-budget` |
| `InvalidParameterException` | Amount given as a number, not a string, or a malformed threshold | Quote `"20"`, and check the JSON syntax |
| `Error parsing parameter` | Missing `file://` prefix | Add `file://` before the path |
| No email arrives | Address typo, spam filter, or the threshold has not been crossed | Check `describe-notifications-for-budget` and your spam folder |
Timing and cost of the budget itself
Budgets reads billing data that AWS refreshes up to three times a day. An alert can therefore arrive several hours after the spend happened. A resource that costs 50 USD an hour can run for most of a day before your 80 percent alert reaches you.
Cost budgets are free to create. AWS charges only for budgets that run automated actions, and the first two action-enabled budgets each month are free. Check the current Budgets pricing page before you add actions.
Review
You defined a 20 USD monthly cost budget, attached an 80 percent actual-spend alert and a 100 percent forecast alert, created both with one CLI command, and verified the result. Next, you will look at what a budget cannot do and what to add to cover that gap.
Exercise
You create a monthly budget of 50 USD with an ACTUAL notification at 90 PERCENTAGE. At what dollar amount does the alert fire, and why might the email arrive after the spend has already passed that amount?
Show answer
It fires when actual spend exceeds 45 USD (90 percent of 50). The email can be late because AWS refreshes billing data only up to three times a day, so the budget compares your threshold against data that may be several hours old.
Exercise
Your create-budget command fails with an error saying the parameter cannot be parsed. The command includes --budget budget.json. What is wrong, and what is the fix?
Show answer
The argument is missing the file:// prefix, so the CLI tries to parse "budget.json" as inline JSON. Change it to --budget file://budget.json.