{"id":16164010,"url":"https://github.com/cyclenerd/poweroff-google-cloud-cap-billing","last_synced_at":"2025-03-16T10:30:37.913Z","repository":{"id":72096421,"uuid":"502345307","full_name":"Cyclenerd/poweroff-google-cloud-cap-billing","owner":"Cyclenerd","description":"📴 Automating cost control by capping Google Cloud billing","archived":false,"fork":false,"pushed_at":"2025-02-24T12:49:22.000Z","size":286,"stargazers_count":46,"open_issues_count":0,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-16T01:11:26.710Z","etag":null,"topics":["billing","cloud-billing-api","cloud-computing","cost-management","finops","gcp","google","google-cloud","google-cloud-platform","infrastructure-as-code","terraform"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Cyclenerd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"Cyclenerd"}},"created_at":"2022-06-11T12:41:40.000Z","updated_at":"2025-02-25T12:03:19.000Z","dependencies_parsed_at":"2023-12-11T13:10:16.621Z","dependency_job_id":"89adc959-4aad-4eb1-b875-6ced600896df","html_url":"https://github.com/Cyclenerd/poweroff-google-cloud-cap-billing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cyclenerd%2Fpoweroff-google-cloud-cap-billing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cyclenerd%2Fpoweroff-google-cloud-cap-billing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cyclenerd%2Fpoweroff-google-cloud-cap-billing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cyclenerd%2Fpoweroff-google-cloud-cap-billing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cyclenerd","download_url":"https://codeload.github.com/Cyclenerd/poweroff-google-cloud-cap-billing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243858268,"owners_count":20359253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["billing","cloud-billing-api","cloud-computing","cost-management","finops","gcp","google","google-cloud","google-cloud-platform","infrastructure-as-code","terraform"],"created_at":"2024-10-10T02:44:59.027Z","updated_at":"2025-03-16T10:30:37.551Z","avatar_url":"https://github.com/Cyclenerd.png","language":"HCL","funding_links":["https://github.com/sponsors/Cyclenerd"],"categories":[],"sub_categories":[],"readme":"# Automatic cost control by capping Google Cloud billing\n\n[![Badge: Google Cloud](https://img.shields.io/badge/Google%20Cloud-%234285F4.svg?logo=google-cloud\u0026logoColor=white)](#readme)\n[![Badge: Terraform](https://img.shields.io/badge/Terraform-%235835CC.svg?logo=terraform\u0026logoColor=white)](#readme)\n[![Badge: CI](https://github.com/Cyclenerd/poweroff-google-cloud-cap-billing/actions/workflows/ci.yml/badge.svg)](https://github.com/Cyclenerd/poweroff-google-cloud-cap-billing/actions/workflows/ci.yml)\n[![Badge: GitHub](https://img.shields.io/github/license/cyclenerd/poweroff-google-cloud-cap-billing)](https://github.com/Cyclenerd/poweroff-google-cloud-cap-billing/blob/master/LICENSE)\n\nWith this repo you can cap costs and stop usage for a Google Cloud project by disabling Cloud Billing automatically.\nRemoving the billing account from a project will cause all Google Cloud services in the project to terminate, including free-tier services.\n\n![Image: Architecture](https://raw.githubusercontent.com/Cyclenerd/poweroff-google-cloud-cap-billing/master/img/stop-billing.jpg)\n\nYou might want cap costs because you have a hard limit on how much money you can spend on Google Cloud. This is typical for students, researchers, or developers working in sandbox environments. In these cases you want to stop the spending and might be willing to shutdown all your Google Cloud services and usage when your budget limit is reached.\n\n\u003e **Warning**\n\u003e When you remove Cloud Billing from your project, all resources are shut down.\n\u003e The resources may not shut down gracefully and be irretrievably deleted.\n\u003e There is no gracefully recovery if you disable Cloud Billing.\n\u003e You can re-enable Cloud Billing,\n\u003e but there is no guarantee that the service will be restored and manual configuration is required.\n\nEverything is based on the original [Google Cloud documentation](https://cloud.google.com/billing/docs/how-to/notify#cap_disable_billing_to_stop_usage).\n\nThis repo has the advantage that everything is deployed automatically thanks to Terraform.\nYou don't have to set up all the steps each time for additional projects.\n\nIt also creates a separate custom role that can only unlink the billing account from the project, but not enable it.\nThis has the advantage that only a billing administrator can enable the billing back and not the project itself.\n\n**Recommendation:** If you have a hard funds limit, set your maximum budget below your available funds to account for billing delays.\n\n## 🏃 Deploying\n\nRun all tasks in the free Google Cloud Shell.\nAll necessary tools (`gcloud` and `terraform`) are already installed.\n\n[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.png)](https://shell.cloud.google.com/cloudshell/open?shellonly=true\u0026ephemeral=false\u0026cloudshell_git_repo=https://github.com/Cyclenerd/poweroff-google-cloud-cap-billing\u0026cloudshell_git_branch=master\u0026cloudshell_tutorial=cloud-shell-tutorial.md)\n\nTrust repo:\n\n![Screenshot: Cloud Shell trust repo](https://raw.githubusercontent.com/Cyclenerd/poweroff-google-cloud-cap-billing/master/img/trust-repo.jpg)\n\nYou need to be the Owner and Billing Account Administrator of the project.\n\n### 1️⃣ Clone\n\nClone this repo and initial setup:\n```bash\ngit clone https://github.com/Cyclenerd/poweroff-google-cloud-cap-billing.git\ncd poweroff-google-cloud-cap-billing\nterraform init\n```\n\n### 2️⃣ Set Project\n\nSet the project that should be stopped when a certain amount is exceeded:\n```bash\ngcloud auth login\ngcloud config set project YOUR-GOOGLE-CLOUD-PROJECT\n```\n\n### 3️⃣ Enable APIs\n\nEnable required APIs and services:\n```bash\nbash enable-services.sh\n```\n\n### 4️⃣ Deploy\n\nNow you can create a budget alert and Cloud Function for this project:\n```bash\n# Stop billing if 1000 USD are exceeded\nterraform apply \\\n  -var=\"project_id=$GOOGLE_CLOUD_PROJECT\" \\\n  -var=\"target_amount=1000\"\n```\n\nIn detail the following is added to the project:\n\n1. Service account : `sa-cap-billing@...`\n1. Custom role : `myCapBilling`\n1. Pub/Sub topic : `cap-billing-alert`\n1. Pub/Sub subscription : `cap-billing-alert-pull`\n1. Budget alert : `Unlink YOUR-GOOGLE-CLOUD-PROJECT from billing account`\n1. Storage bucket for Cloud Function source code : `RANDOM-UUID`\n1. Cloud Function with Pub/Sub event trigger : `cap-billing-RANDOM-HEX`\n\n#### Variables\n\nYou can customize the setup with the following Terraform input variables:\n\n| Variable        | Description                                                        | Default           |\n|-----------------|--------------------------------------------------------------------|-------------------|\n| `project_id`    | The project ID for the resources and budget alert                  |                   |\n| `pubsub_topic`  | Name of the Pub/Sub topic                                          | cap-billing-alert |\n| `target_amount` | Set maximum monthly budget amount (currency as in billing account) | 1000              |\n| `region`        | Region for the resources                                           | us-central1       |\n\n**Examples**\n\nVia command:\n```bash\n# Stop billing if 5 USD are exceeded and deploy everything in europe-west4\nterraform apply \\\n  -var=\"project_id=$GOOGLE_CLOUD_PROJECT\" \\\n  -var=\"target_amount=5\" \\\n  -var=\"region=europe-west4\"\n```\n\nOr via config file `terraform.tfvars`:\n```text\n# Project ID\nproject_id=bla-fa-123\n# Target amount\ntarget_amount=10\n# Pub/Sub topic\npubsub_topic=stop-billing-alert\n```\nApply with config file:\n```bash\nterraform apply\n```\n\n» [Terraform Help](https://www.terraform.io/language/values/variables)\n\n\n### 💥 Test\n\nYou can perform a test.\nThe billing account will be removed.\nDo it only if you are sure and the project is not important.\n\nCheck active billing account:\n```bash\ngcloud beta billing projects describe \"$GOOGLE_CLOUD_PROJECT\" | grep billingAccountName\n```\n\nSend a message that triggers the Cloud Function and disables billing:\n```bash\ngcloud pubsub topics publish \"cap-billing-alert\" --message='{ \"costAmount\" : 2, \"budgetAmount\": 1 }'\n```\n\nWait a while... Billing should then be disabled.\n```bash\ngcloud beta billing projects describe \"$GOOGLE_CLOUD_PROJECT\" | grep billingAccountName\n```\n\n## 💸 Enable Billing\n\nEnable billing for an existing project:\n\n![Screenshot: Enable billing](https://raw.githubusercontent.com/Cyclenerd/poweroff-google-cloud-cap-billing/master/img/enable-billing.jpg?v1)\n\n» [Google documentation](https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_an_existing_project)\n\n## 📎 Prerequisites\n\nTo run the commands described in this repo, you need the following:\n\n1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/install) version 319.0.0 or later\n1. Install [Terraform](https://www.terraform.io/downloads.html) version 1.1.9 or later.\n1. Set up a Google Cloud\n   [billing account](https://cloud.google.com/billing/docs/how-to/manage-billing-account) and project.\n\n## ❤️ Contributing\n\nHave a patch that will benefit this project?\nAwesome! Follow these steps to have it accepted.\n\n1. Please read [how to contribute](CONTRIBUTING.md).\n1. Fork this Git repository and make your changes.\n1. Create a Pull Request.\n1. Incorporate review feedback to your changes.\n1. Accepted!\n\n\n## 📜 License\n\nAll files in this repository are under the [Apache License, Version 2.0](LICENSE) unless noted otherwise.\n\nPlease note:\n\n* No warranty\n* No official Google product","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyclenerd%2Fpoweroff-google-cloud-cap-billing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcyclenerd%2Fpoweroff-google-cloud-cap-billing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyclenerd%2Fpoweroff-google-cloud-cap-billing/lists"}