Setting up budgets for cloud usage with Terraform

From time to time your team may want to use a new service from your cloud provider. That request may come with an estimated usage cost for the service and if it fits in the budget and seems good ROI it will be approved.  For most startup projects, that’s where the cloud cost control ends. With just a bit of extra effort, especially if resources are already being provisioned with Terraform, you can use budgeting tools offered by Amazon, Google etc. to ensure the actual cost aligns with expectations.

For the purposes of this example, I’ll use Google Cloud Budgets, but the analogous resources and APIs exist in AWS and Azure.

Goal: Add a budget to monitor the cost of a new Google Cloud Run service your team wants to deploy. 

Prerequisites: An operational knowledge of Terraform and editor access to a Google Cloud Project & Google Cloud Billing Account

Part 0 - Become familiar with your cloud provider's budgeting tool

If you haven't spent a few minutes creating a budget using the cloud console itself. The various parameters and options in Terraform will make a lot more sense if you've already got the context and perspective of how the budgeting process as a whole works. In Google Cloud budgets are under "Budgets & alerts" in the billing section.

Part 1 - Setup the cloud run project

This is just a sample directly from the terraform resource documentation

resource "google_cloud_run_service" "default" {name     = "cloudrun-srv"location = "us-central1"template {spec {containers {image = "us-docker.pkg.dev/cloudrun/container/hello"}}}traffic {percent         = 100latest_revision = true}}

Part 2 - Find the service ID

When setting up a budget with Google Cloud you have the option to have the budget monitor cost of a specific service via a filter. The terraform resource for creating budgets with these filters requires you specify the service by the service's ID. You can find the ID of various services in the cloud console UI as per the screenshot below.

Part 3 - Setup the budget

The below terraform code is lightly modified from the sample code in the google cloud budget terraform resource documentation

data "google_billing_account" "account" {billing_account = "000000-0000000-0000000-000000"}data "google_project" "project" {}resource "google_billing_budget" "budget" {billing_account = data.google_billing_account.account.iddisplay_name = "Project X Cloud Run Billing Budget"budget_filter {projects = ["projects/${data.google_project.project.number}"]credit_types_treatment = "EXCLUDE_ALL_CREDITS"services = ["services/152E-C115-5142"] # Cloud Run}amount {specified_amount {currency_code = "USD"units = "100" # $100 per month}}threshold_rules {threshold_percent = 0.5 # Alert at $50}threshold_rules {threshold_percent = 0.9 # Alert when forecast to hit $90spend_basis = "FORECASTED_SPEND"}}

You can even set up custom alerting rules so the teams that create new infrastructure at the ones notified if/when spend exceeds the amount forecast during planning and development.

Previous
Previous

SF Engineering Leadership Annual 2022

Next
Next

Production SQL Server Checklist & Best Practices