AWS Budgets in CDK
Creating a basic budget using csharp CDK for AWS
Problem
I wanted to create a budget for my AWS account using a CDK Construct.
Prerequisites
- IAM Access to Billing Information
- A CDK Stack to work with
Solution
Use the CfnBudget low level apis
// generate a topic to use
var topic = new Topic(...);
topic.AddSubscription(new EmailSubscription("me@nowhere.com"));
// now use the low level apis to create a budget with alerts
var budget = new CfnBudget(scope, "MyBudget", new CfnBudgetProps
{
// define a monthly budget of $10.00
Budget = new CfnBudget.BudgetDataProperty
{
BudgetLimit = new CfnBudget.SpendProperty {Unit = "USD", Amount = 10.00},
BudgetName = $"Account Monthly Budget",
BudgetType = "COST",
TimeUnit = "MONTHLY",
},
// define notification parameters
NotificationsWithSubscribers = new[]{
new CfnBudget.NotificationWithSubscribersProperty
{
// notifiy when the forecast hits 80% of the budget
Notification = new CfnBudget.NotificationProperty
{
ComparisonOperator = "GREATER_THAN",
NotificationType = "FORECASTED",
Threshold = 80.00,
ThresholdType = "PERCENTAGE"
},
// define subscribers
Subscribers = new[]
{
// add the topic from above as a subscriber
new CfnBudget.SubscriberProperty
{
SubscriptionType = "SNS",
Address = props.BillingTopic.TopicArn
}
}
}
}
});
Things to know
It's worth pointing out that while the budget notification subscribers can be email addresses directly, the best practice seems to be to hook to an SNS topic with emails added to the topic.
Like this article?
0
Software Development Nerd