Sub topic 12
Creating a monthly budget with alerts
This chapter assumes you have a new AWS account with MFA on the root user, a separate admin identity for daily work, and access to billing data. You also need the AWS CLI configured with that admin identity, and you should be comfortable editing JSON files.
Why set a budget first
A new account can run up charges before you notice. A misconfigured resource, a forgotten test server, or leaked credentials can all do it. AWS Budgets lets you set a spending target and get an email when your spend approaches or passes it. The alert only tells you something is wrong. Setting it up on day one means you hear about a problem in hours or days, not when the monthly bill arrives.
How a budget works
A budget has two parts:
- The budget definition. A name, an amount, a time period, and a type such as
COST. - Notifications. Each one has a trigger (actual or forecasted spend, compared with a threshold) and one or more subscribers, such as email addresses.
Step 1: Define the budget
Save this as budget.json. It sets a 20 USD monthly cost budget.
{
"BudgetName": "monthly-cost-budget",
"BudgetLimit": { "Amount": "20", "Unit": "USD" },
"TimeUnit": "MONTHLY",
"BudgetType": "COST"
}Pick an amount slightly above what you expect to spend. A budget that is far too high will not warn you in time.
Step 2: Define the alert
Save this as notifications.json. It sends an email when actual spend passes 80 percent of the budget. Replace the address with one you read regularly.
[
{
"Notification": {
"NotificationType": "ACTUAL",
"ComparisonOperator": "GREATER_THAN",
"Threshold": 80,
"ThresholdType": "PERCENTAGE"
},
"Subscribers": [
{ "SubscriptionType": "EMAIL", "Address": "[email protected]" }
]
}
]ACTUAL means the alert fires on money already charged. You can add a second entry with NotificationType set to FORECASTED to be warned when AWS predicts you will exceed the budget. Forecasts need some usage history, so on a brand-new account they may not appear at first.
Step 3: Create the budget
Run the command with your own 12-digit account ID in place of the placeholder.
aws budgets create-budget \
--account-id 111122223333 \
--budget file://budget.json \
--notifications-with-subscribers file://notifications.jsonIf you do not know your account ID, aws sts get-caller-identity prints it. A successful call prints nothing. You can confirm the budget exists with aws budgets describe-budgets --account-id 111122223333.
Common errors
| Symptom | Likely cause |
|---|---|
| `AccessDeniedException` | The identity lacks Budgets permissions, or IAM access to billing is not activated for the account. |
| `DuplicateRecordException` | A budget with that name already exists. Change `BudgetName`. |
| Parameter validation error | The JSON is malformed or the amount is not a string. |
| No email arrives | The address is wrong, the message is in spam, or spend has not reached the threshold. |
Keeping the alert useful
- Send alerts to a mailbox more than one person can read, or one you check daily.
- Add a second notification at 100 percent so you get a clear "over budget" signal.
- Review the budget when your usage grows, so alerts stay meaningful.
Exercise
You created a 20 USD budget with an ACTUAL alert at 80 percent. On the 10th of the month, a forgotten resource has already cost 25 USD. What happens to the resource, and what should you do?
Show answer
Nothing happens to the resource. A budget only sends notifications, so it keeps running and accruing charges. You should find the resource (for example in the Billing console's cost breakdown by service), stop or delete it, and consider adding a FORECASTED alert so you are warned earlier next time.
Exercise
Which two files do you pass to aws budgets create-budget, and which option takes each?
Show answer
budget.json is passed with --budget file://budget.json, and notifications.json is passed with --notifications-with-subscribers file://notifications.json.