Jump to Content
Developers & Practitioners

Using budgets to automate cost controls

December 13, 2022
Mark Mirchandani

Developer Advocate

Terrence Ryan

Google Cloud Developer Advocate

TL;DR - Budgets can do more than just track costs! You can set up automated cost controls using programmatic budget notifications, and we have an interactive walkthrough with sample architecture to help get you started.

Budgets can help you answer cost questions, and so much more!

There's a few blog posts on what Google Cloud Budgets are and how to use them for more than just sending emails by using programmatic budget notifications. These are important steps to take when using Google Cloud, so you can accurately ask and answer questions about your costs and get meaningful answers in the systems you already use. As your cloud usage grows and matures, you may also need to be more proactive in dealing with your costs.

More than just a budget

To recap: budgets let you create a dynamic way of being alerted about your costs, such as getting emails when you've spent or are forecasted to spend a certain amount. When creating a budget, you can provide a fixed amount or you can have the amount based on the previous period, so you could set up a budget that alerts you if your spending has changed significantly in a monthly cadence. In addition, you can have budgets send data to Pub/Sub on a regular basis (programmatic budget notifications) that can be used however you'd like, such as sending messages to Slack.

Budgets that send out notifications are flexible enough to do just about anything, but that's also where things can become a bit tricky to set up. If you're monitoring the costs for a large company with a lot of cloud usage, that could involve multiple environments with lots of products being used in different ways. Being informed about the costs is a good starting point, but you'll likely want to set up automated cost controls to protect yourself and your cloud spending.

In essence, setting up automated cost controls is the same as using programmatic budget notifications: the budget occasionally sends out a Pub/Sub message, and you create a Cloud Function (or similar) subscriber that receives that message and runs some code. Of course, the specifics of that code might be anything and will heavily depend on your business logic needs, ranging from sending a text message all the way to shutting down cloud resources. While the specifics are up to you, we made a few things to make getting started easier!

Show me the way

We've created an interactive walkthrough to help you with all of the steps needed in getting programmatic budget notifications up and running.


Following the walkthrough, you'll set up a budget, Pub/Sub topic, and Cloud Function that work together to respond to programmatic notifications. Not only will you get a sense of all the pieces involved, you can easily modify the code from the function for your specific purposes, so it serves as a great starting point. That also leads to a question I've heard often: "This is great, but what code am I supposed to use?" And that is why we've expanded our walkthrough to include a full, one-click architecture deployment!

It's like a sentry, but for your cloud costs

Cost Sentry, powered by DeployStack, takes the next step in programmatic budget notifications and sets up all the pieces needed to create basic automated cost-enforcement, as well as some example architecture to test it on! In fact, the overall architecture isn't much more than just setting up the programmatic budget notifications alone, but it gives a good example of how that could work in a full environment. 

This architecture will get deployed for you, along with the working code to handle a programmatic budget notification and interact with Compute Engine and Cloud Run.


Both the walkthrough and deploying the Cost Sentry stack can be used as the starting point for a full automated cost-enforcement solution. With these samples, you'll want to take a look at the Cloud Function code that receives data from your budget, and how it interacts with the Google Cloud APIs to shut down resources. In this example, any Compute Engine instances or Cloud Run deployments that have been labeled with 'costsentry' will be shut-down/disabled when your budget exceeds the configured amount.

While this is a great solution for getting an automated cost-enforcement solution started, the hard part is probably in the next questions you'll need to answer for your use case. Questions like "What do I actually want to have happen when I hit my budget?" and "Will stopping all of these instances automatically have ramifications?" (spoiler alert: probably) are important ones to figure out when looking at the full scope of a cost-enforcement solution.

Setting up a full automated cost enforcement solution gives you the flexibility to customize your response to budget updates, such as sending higher-priority messaging as you get closer to your budget total, and taking action by shutting down services when you greatly exceed your budget. Any way that you want to build a solution, this is a great starting point!

Go forth, and do

This may seem like a lot, and I'm a big fan of the "crawl, walk, run" philosophy. If you're new to Google Cloud, get started by just setting up a budget for all of your costs. From there, you can work with programmatic budget notifications to start expanding how you use budgets. As you get more familiar with Google Cloud, you'll likely need to customize your cost controls and start with Cost Sentry to set up your automated cost-enforcement solution.

Check out the interactive walkthrough and Cost Sentry architecture to get started!

Posted in