Terraform GCP fails to apply - failed to check permissions on billing account oauth2 invalid_grant

Terraform GCP fails to apply - failed to check permissions on billing account oauth2 invalid_grant

When running terraform apply

When deploying a fresh GCP project using the project factory module with the following command:

tf apply -var-file=staging.tfvars

I encountered this terraform error:

Error: failed pre-requisites: failed to check permissions on billing account "billingAccounts/<redacted>": Post "https://cloudbilling.googleapis.com/v1/billingAccounts/<redacted>:testIamPermissions?alt=json&prettyPrint=false": oauth2: "invalid_grant" "Bad Request"
with module.cloud-run-project.module.project-factory.google_project.main,
on .terraform/modules/cloud-run-project/modules/core_project_factory/main.tf line 73, in resource "google_project" "main":
73: resource "google_project" "main" {

My terraform project is very basic to get this new project started:

module "cloud-run-project" {
  source  = "terraform-google-modules/project-factory/google"
  version = "14.4.0"
  name              = "gcp-cloudrun-${var.namespace}"
  random_project_id = false
  org_id            = var.organization_id
  billing_account   = var.billing_account
  activate_apis = [
    "appengine.googleapis.com",
    "run.googleapis.com",
    "artifactregistry.googleapis.com",
  ]
}

Along with my variables file:

namespace="staging"
billing_account = "<my billing account number>"
organization_id = "<my org id>"

region = "us-central1"
zone = "us-central1-f" # same as the existing database

project_admins = [
  "user:<my admin email>"
]

To resolve this error, I made sure to log in to gcp with both gcloud commands:

gcloud auth login

gcloud auth application-default login

For me, the second command was what actually fixed the problem.

Have a great day.!

- Stewart