Search code examples
google-cloud-platformterraform-provider-gcp

terraform create billing budget on GCP


When I try to create a billing budget for a project using terraform, I get the following error message:

Error: Error creating Budget: googleapi: Error 403: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the billingbudgets.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/. If you are getting this error with curl or similar tools, you may need to specify 'X-Goog-User-Project' HTTP header for quota and billing purposes. For more information regarding 'X-Goog-User-Project' header, please check https://cloud.google.com/apis/docs/system-parameters.
Details:
[
  {
    "@type": "type.googleapis.com/google.rpc.ErrorInfo",
    "domain": "googleapis.com",
    "metadata": {
      "consumer": "projects/543254326546",
      "service": "billingbudgets.googleapis.com"
    },
    "reason": "SERVICE_DISABLED"
  }
]

The project that I logged in to has the API service billingbudgets.googleapis.com enabled.

The following roles are assigned to my service account:

Actions Admin
Billing Account Usage Commitment Recommender Admin
Billing Account Usage Commitment Recommender Viewer
Project Billing Manager
Service Usage Admin
Service Usage Consumer
Billing Account Administrator

My terraform code:

resource "google_billing_budget" "budget" {
  billing_account = var.billing_account
  display_name    = var.name

  budget_filter {
    projects               = [var.projects]
  }

  amount {
    specified_amount {
      currency_code = "USD"
      units = var.budget_amount
    }
  }

  threshold_rules {
    threshold_percent = var.budget_threshold
  }
}

With the gcloud SDK, I'm able to create the budget and I don't get any errors.
Here is the command I run to create the budget:

gcloud billing budgets create --billing-account=0FFF0-FFFFF-CBFFFF --display-name=test-daniel --budget-amount=100USD --project=testproject

Solution

  • As per this git issue, Adding billing.admin role to the Terraform Service Account solves your issue.

    And check this issue if the issue still continues after adding the role.