Costs meet code with programmatic budget notificationsCosts meet code with programmatic budget notificationsGoogle Cloud Developer Advocate

This is just a sample of the message with a subset of properties

Here’s the full notification format if you want to see more, but we’re mainly going to focus on a few key properties.

  • costAmount is the current cost against that budget, for whatever filters you chose (such as just Compute Engine products, or just your dev projects)
  • budgetAmount is the amount you’ve configured for the budget, and budgetAmountType will be SPECIFIED_AMOUNT or LAST_MONTH_COST depending on how you set the budget up
  • costIntervalStart is the start of the current time period where costs are being measured, which will be the start of the month
  • alertThresholdExceeded is the last threshold that has been passed based on the ones you’ve set up. If you want a refresher on thresholds, check out the first post
  • budgetDisplayName is the name of the budget, but you can actually get the unique ID of the budget through some extra metadata (that we’ll come back to later)

So with these basic properties, we get a lot of information about the budget! On top of that, we’ll get this notification multiple times a day (last time I checked I got it over 40 times scattered throughout a day) so we’ll always get pretty up-to-date information.

Note: Even though the notifications come in pretty consistently, cost data can still take some time to be reported from the resource level. The budget information will be up to date with the best information it has, but plan accordingly.

Another important note is that this notification doesn’t interfere with your threshold alerts. You can keep all of those the same and you’ll still get your alerts in the same way, plus these notifications will be sent to your Pub/Sub topic.

Well that’s fine and dandy, but now we need to actually do something with the notification. So, let’s use the lightweight Cloud Functions to be a subscriber of our topic.

Cloud Functions saves the day

Use the left-nav to head to find Cloud Functions and head there.