I am about to start a run of hands-on posts that use AWS AI services such as Amazon Bedrock. Before I build any of it, I want an alarm that tells me when the bill starts to climb. Forgetting to clean something up, or leaving a loop running against a paid API, is the easiest way to get a nasty surprise. This post sets that alarm up in about ten minutes.
What We Are Building
An AWS Budget with a monthly limit that emails me when:
- I have spent 50% of the limit
- I have spent 80% of the limit
- AWS forecasts that I will go over 100% of the limit by the end of the month
The third one is the useful one. It warns you part way through the month rather than after the damage is done.
Before We Start
I am assuming the awscli is installed and you have credentials configured (see How to use a Password Manager to Store AWS Credentials if you want to avoid keeping keys on disk). Your user or role needs permission to create budgets and CloudFormation stacks.
Billing data for a brand new account can take up to 24 hours to appear, so don’t worry if the numbers look empty at first.
Option 1: The Console
- Sign in and open Billing and Cost Management, then Budgets
- Choose Create budget and pick the Use a template (simplified) option
- Choose Monthly cost budget, set a budget name, an amount (I use 10 USD for a sandbox) and add your email address
- Choose Create budget
This template gives you an alert at 85% actual, 100% actual and 100% forecasted. It is a perfectly good start, and the custom option lets you pick your own thresholds.
Option 2: CloudFormation
I prefer to keep this in code so I can recreate it in every new account. Save the following as budget.yaml:
AWSTemplateFormatVersion: "2010-09-09"
Description: "Monthly cost budget with email alerts"
Parameters:
BudgetAmount:
Type: Number
Default: 10
Description: Monthly limit in USD
AlertEmail:
Type: String
Description: Email address that receives the alerts
Resources:
MonthlyBudget:
Type: AWS::Budgets::Budget
Properties:
Budget:
BudgetName: monthly-cost-budget
BudgetType: COST
TimeUnit: MONTHLY
BudgetLimit:
Amount: !Ref BudgetAmount
Unit: USD
NotificationsWithSubscribers:
- Notification:
NotificationType: ACTUAL
ComparisonOperator: GREATER_THAN
Threshold: 50
ThresholdType: PERCENTAGE
Subscribers:
- SubscriptionType: EMAIL
Address: !Ref AlertEmail
- Notification:
NotificationType: ACTUAL
ComparisonOperator: GREATER_THAN
Threshold: 80
ThresholdType: PERCENTAGE
Subscribers:
- SubscriptionType: EMAIL
Address: !Ref AlertEmail
- Notification:
NotificationType: FORECASTED
ComparisonOperator: GREATER_THAN
Threshold: 100
ThresholdType: PERCENTAGE
Subscribers:
- SubscriptionType: EMAIL
Address: !Ref AlertEmailDeploy it:
aws cloudformation deploy \
--template-file budget.yaml \
--stack-name monthly-budget \
--parameter-overrides BudgetAmount=10 AlertEmail=you@example.comThen confirm it exists:
aws budgets describe-budgets --account-id $(aws sts get-caller-identity --query Account --output text)Turn On Cost Anomaly Detection Too
A budget compares you to a number you chose. AWS Cost Anomaly Detection compares you to your own normal pattern and flags unusual spikes, even if you are still under budget. In the console, open Cost Anomaly Detection under Billing and Cost Management, create a monitor for AWS services and subscribe your email. I did this one in the console because it only takes a minute.
What It Costs
At this scale, effectively nothing. Budgets and Cost Anomaly Detection have free usage that covers a setup like this, but pricing changes, so check the AWS Budgets pricing page before relying on it.
Cleaning Up
I leave this running permanently. If you want to remove it:
aws cloudformation delete-stack --stack-name monthly-budgetWhat I’d Do Next
Create a separate sandbox account for these experiments and sign in with IAM Identity Center rather than long-lived keys. That is the next post.