hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.33k stars 1.73k forks source link

Error creating google_billing_budget due to "Error 400: Request contains an invalid argument" #19796

Open RaginiDhatric opened 1 week ago

RaginiDhatric commented 1 week ago

Community Note

Terraform Version & Provider Version(s)

Terraform version: 1.4.6 on

Affected Resource(s)

google_billing_budget

Terraform Configuration

resource "google_billing_budget" "budget" {
  for_each        = var.project_billing_map
  billing_account = each.value

  display_name    = "FDPlan GCP Budget Alert for ${each.key}"

  budget_filter {
    projects = ["projects/${each.key}"]
  }

  amount {
    specified_amount {
      currency_code = "USD"
      units         = "2000"
    }
  }

  threshold_rules {
    threshold_percent = 0.5
  }

  threshold_rules {
    threshold_percent = 0.9
  }

  threshold_rules {
    threshold_percent = 1.0
  }

  all_updates_rule {
    monitoring_notification_channels = [
      google_monitoring_notification_channel.email_notification[each.key].id
    ]
    disable_default_iam_recipients = true
  }
}

Debug Output

2024-10-09T12:49:23.918Z [WARN] Provider "registry.terraform.io/hashicorp/google" produced an invalid plan for google_billing_budget.budget["evd"], but we are tolerating it because it is using the legacy plugin SDK. The following problems may be the cause of any confusing errors from downstream operations:

2024-10-09T12:49:23.926Z [INFO] provider.terraform-provider-google_v4.55.0_x5: 2024/10/09 12:49:23 [DEBUG] Google API Request Details:

---[ REQUEST ]--------------------------------------- POST /v1/billingAccounts/00xxxx-xxxxxx-xxxxxx/budgets?alt=json HTTP/1.1 Host: billingbudgets.googleapis.com User-Agent: Terraform/1.4.6 (+https://www.terraform.io/) Terraform-Plugin-SDK/2.10.1 terraform-provider-google/4.55.0 Content-Length: 841 Content-Type: application/json Accept-Encoding: gzip

{ "amount": { "specifiedAmount": { "currencyCode": "USD", "units": "2000" } }, "budgetFilter": { "creditTypesTreatment": "INCLUDE_ALL_CREDITS", "projects": [ "projects/evd" ] }, "displayName": "GCP Budget Alert for evd", "notificationsRule": { "disableDefaultIamRecipients": true, "monitoringNotificationChannels": [ "projects/evd/notificationChannels/xxxxxxxxxxxxxxx687" ], "schemaVersion": "1.0" }, "thresholdRules": [ { "spendBasis": "CURRENT_SPEND", "thresholdPercent": 0.5 }, { "spendBasis": "CURRENT_SPEND", "thresholdPercent": 0.9 }, { "spendBasis": "CURRENT_SPEND", "thresholdPercent": 1 } ] }

2024-10-09T12:49:25.004Z [INFO] provider.terraform-provider-google_v4.55.0_x5: 2024/10/09 12:49:25 [DEBUG] Google API Response Details: ---[ RESPONSE ]-------------------------------------- HTTP/2.0 400 Bad Request Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 Cache-Control: private Content-Type: application/json; charset=UTF-8 Date: Wed, 09 Oct 2024 12:49:24 GMT Server: ESF Vary: Origin Vary: X-Origin Vary: Referer X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0

{ "error": { "code": 400, "message": "Request contains an invalid argument.", "status": "INVALID_ARGUMENT" } }

Error: Error creating Budget: googleapi: Error 400: Request contains an invalid argument.

with google_billing_budget.budget["evd"], on main.tf line 62, in resource "google_billing_budget" "budget": 62: resource "google_billing_budget" "budget" {

Expected Behavior

Create the billing budget for the specified google project which is linked to a billing account

Actual Behavior

Error: Error creating Budget: googleapi: Error 400: Request contains an invalid argument.

with google_billing_budget.budget["evd"], on main.tf line 62, in resource "google_billing_budget" "budget": 62: resource "google_billing_budget" "budget" {

Steps to reproduce

  1. terraform apply

Important Factoids

  1. terraform plan generated the following:

    • google_billing_budget.budget["evd"] will be created
    • resource "google_billing_budget" "budget" {

      • billing_account = "00xxxx-xxxxxx-xxxxxx"

      • display_name = "FDPlan GCP Budget Alert for evd"

      • id = (known after apply)

      • name = (known after apply)

      • all_updates_rule {

        • disable_default_iam_recipients = true
        • monitoring_notification_channels = [
          • "projects/evd/notificationChannels/xxxxxxxxxxxxxxx687", ]
        • schema_version = "1.0" }
      • amount {

        • specified_amount {
          • currency_code = "USD"
          • units = "2000" } }
      • budget_filter {

        • credit_types = (known after apply)
        • credit_types_treatment = "INCLUDE_ALL_CREDITS"
        • labels = (known after apply)
        • projects = [
          • "projects/evd", ]
        • services = (known after apply)
        • subaccounts = (known after apply) }
      • threshold_rules {

        • spend_basis = "CURRENT_SPEND"
        • threshold_percent = 0.5 }
      • threshold_rules {

        • spend_basis = "CURRENT_SPEND"
        • threshold_percent = 0.9 }
      • threshold_rules {

        • spend_basis = "CURRENT_SPEND"
        • threshold_percent = 1 } }
  2. I've tried running the same config with latest version of the provider (registry.terraform.io/hashicorp/google v6.6.0), still faced the same error.

  3. Also, I've successfully created billing budget for the same project using my user account with the Owner role via the Budgets & Alerts section in the Google Cloud Platform (GCP) UI.

  4. Additionally, the following APIs are enabled for the project: stackdriver.googleapis.com, billingbudgets.googleapis.com, cloudbilling.googleapis.com, servicemanagement.googleapis.com, monitoring.googleapis.com, serviceusage.googleapis.com.

  5. Despite following Google's documentation which states that users can create budgets for projects they own even with limited or no access to the Cloud Billing account https://cloud.google.com/billing/docs/how-to/budgets#permissions-project. In my case, the service account utilized by Terraform has substantial project-level permissions, including the roles of Owner, Monitoring Editor, Service Management Administrator, and Service Usage Admin. However, I encounter issues when trying to manage billing budgets via Terraform. Could there be a missing configuration, permission, or is this a potential bug in Terraform?

References

No response

b/374104774

CWXrlizardo commented 4 days ago

@RaginiDhatric I had the same issue. The fix was changing the currency code to match my Country/Region in Billing--> Payment Settings --> Country/Region. Hopefully, this helps!