
Set a monthly AWS budget in CDK, and know what its alerts can and can't do
Define an AWS Budget in CDK with actual and forecasted alerts to email and SNS, plus the limits that matter: refresh timing, forecast history and one-time alerts.
A budget is one of the cheapest guardrails you can add to an AWS account, and it takes about thirty lines of CDK. The part that deserves attention is not the code but what the alerts do once they exist, because a few documented behaviors change how much you can rely on them.
The stack
The L1 construct
CfnBudget maps directly to AWS::Budgets::Budget . The budget below is a 50 USD monthly cost budget with two notifications: one when actual spend passes 80% of the limit, and one when forecasted spend passes 100%. The 80% alert goes to an email address and to an SNS topic; the forecast alert goes to email only.import * as cdk from 'aws-cdk-lib';import * as budgets from 'aws-cdk-lib/aws-budgets';import * as sns from 'aws-cdk-lib/aws-sns';import * as iam from 'aws-cdk-lib/aws-iam';
const topic = new sns.Topic(this, 'BudgetAlerts');topic.addToResourcePolicy(new iam.PolicyStatement({ principals: [new iam.ServicePrincipal('budgets.amazonaws.com')], actions: ['sns:Publish'], resources: [topic.topicArn],}));
new budgets.CfnBudget(this, 'MonthlyCost', { budget: { budgetName: 'monthly-cost', budgetType: 'COST', timeUnit: 'MONTHLY', budgetLimit: { amount: 50, unit: 'USD' }, }, notificationsWithSubscribers: [ { notification: { notificationType: 'ACTUAL', comparisonOperator: 'GREATER_THAN', threshold: 80, thresholdType: 'PERCENTAGE', }, subscribers: [ { subscriptionType: 'EMAIL', address: 'you@example.com' }, { subscriptionType: 'SNS', address: topic.topicArn }, ], }, { notification: { notificationType: 'FORECASTED', comparisonOperator: 'GREATER_THAN', threshold: 100, thresholdType: 'PERCENTAGE', }, subscribers: [{ subscriptionType: 'EMAIL', address: 'you@example.com' }], }, ],});The snippet is the body of a stack constructor, so
this is the stack. I ran cdk synth on it and it produced the SNS topic, the topic policy, and an AWS::Budgets::Budget resource with both notifications. I did not deploy it, so the email and SNS delivery described below comes from the documentation, not from a test run.The SNS topic needs a policy
If you send alerts to SNS, the budget needs permission to publish to the topic. The AWS best practices page for Budgets says so explicitly, which is why the example adds a resource policy for the
budgets.amazonaws.com service principal. Without it the SNS subscriber is configured but has nothing to deliver through. In a real account you would narrow that statement with a condition on the account, but the minimal form above is enough to show the shape.Each notification can have up to ten email subscribers and one SNS topic. Every notification needs at least one subscriber.
Actual and forecasted alerts behave differently
An actual alert fires once per budget period, the first time spend crosses the threshold. If you set 80% and spend reaches 80% on day 12, you get one message. Nothing arrives at 90% unless you defined a second notification for it. Forecasted alerts can fire more than once in a period.
Forecasted alerts also have a precondition. According to the best practices page, AWS needs roughly five weeks of usage data before a forecast-based alert can trigger. In a new account, or for a brand new workload, the forecasted notification in the example will stay quiet for a while. Treat it as an addition to the actual alerts, not as a replacement for them.
Alerts lag behind spend
Budget data is not live. The Budgets documentation says it is updated up to three times a day, typically eight to twelve hours apart, and it also notes that there is a delay between a charge and the notification about it. A threshold can be exceeded well before anyone is told. Setting the first alert at 80% instead of 100% is a reasonable way to leave room for that gap.
It also means a budget is a reporting tool, not a limit. Nothing in the template above stops a resource from running or blocks a deployment. Budgets do have a separate actions feature (
AWS::Budgets::BudgetsAction ), but it is a different resource with its own IAM setup and is outside the scope of this example.Changing notifications replaces them
In the CloudFormation reference, every property of a notification and every subscriber property is marked as requiring replacement on update. The
Budget property itself, which holds the limit and the time unit, updates with no interruption. So raising the limit from 50 to 80 USD is a quiet in-place change, while editing a threshold or swapping an email address replaces that notification. Plan for that if you manage the stack from a pipeline.Things worth checking before you deploy
Put the real address in, and keep it out of a public repository if it is a personal one. Pick a limit that matches what the account normally costs, otherwise the first alert will either never fire or fire on day two and get ignored. The documentation also recommends recurring budgets over fixed-period ones, which is what the
MONTHLY time unit gives you here.One small inconsistency in the docs: the
AWS::Budgets::Budget property text mentions five notifications while its array limit and the notification page say ten. The example uses two, so it makes no difference here, but check the current reference before building around the upper limit.What was verified: the template synthesizes with
aws-cdk-lib and the property names and allowed values match the CloudFormation reference. What was not: a deployment, email delivery, or SNS delivery.Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article