Closed clive-h-townsend closed 4 months ago
I have the same problem when using google_spanner_instance: The Project ID is not respected and when the service account is from another project this project is incorrectly used by the resource.
I have the same problem. Did anyone figure out the solution?
I also encountered this challenge with Cloud Build triggers using Cloud Functions Version 2/Gen 2 and was pointed by a peer to the reason. user_project_override
defaults to false
which allows the service (i.e. API) to decides which project to use for provisioning and managing the resource. In the case of Cloud Build the API defaults to the project associated with the Terraform user versus the project set at the provider or resource level. For many of us we use an account/service account from a different GCP project with Terraform than the one we're deploying resources which causes this problem. Instead set user_project_override
on the provider and specify the project in either the provider or resource which will ensure Cloud Build provisions in the correct project where Cloud Build is enabled.
Also encountered this challenge with Cloud Build triggers and was pointed by another to the reason.
user_project_override
defaults tofalse
which allows the service to decide if the resource project or project associated with the credential will be used. In the case of Cloud Build triggers seems its preference is the credential's project even if you set the project at either the provider or resource level. Setting theuser_project_override
in the provider resolved this for me.
I had the same problem. Thanks to @whmackay's suggestion I solved it by adding the google-beta provider and set the user_project_override
attribute like this:
provider "google-beta" {
user_project_override = true
# ...
}
resource "google_service_account" "cloudbuild_service_account" {
project = data.google_project.project.project_id
account_id = "cloud-build-service-account"
}
resource "google_cloudbuild_trigger" "cloud-build-trigger" {
provider = google-beta
project = data.google_project.project.project_id
service_account = google_service_account.cloudbuild_service_account.id
# ...
}
I just experienced this, and it was very unintuitive. I even set project = ...
on the resource, and that was ignored in favour of the default project of the credentials it uses.
Is this possible to fix in the Go code? Can we set one of these options to false to allow it to use the defined project from the resource config? https://github.com/hashicorp/terraform-provider-google/blob/1e379a92d1ca7233dac691a2f171df9f71b141c8/google/services/cloudbuild/resource_cloudbuild_trigger.go#L6038-L6043
Here's a version of my config that had this issue. I had to add user_project_override = true
to the provider to get it working.
resource "google_cloudbuild_trigger" "my-trigger" {
project = "my-cloudbuild-project"
location = "global"
# ...
}
import {
id = "projects/my-cloudbuild-project/locations/global/triggers/abc123-redacted"
to = google_cloudbuild_trigger.my-trigger
}
Output:
$ terraform plan
... <trimmed>
│ Error: Error when reading or editing CloudBuildTrigger "projects/my-cloudbuild-project/triggers/abc123-redacted": googleapi: Error 403: Cloud Build API has not been used in project my-terraform-service-project before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudbuild.googleapis.com/overview?project=my-terraform-service-project then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
│ Details:
│ [
│ {
│ "@type": "type.googleapis.com/google.rpc.Help",
│ "links": [
│ {
│ "description": "Google developers console API activation",
│ "url": "https://console.developers.google.com/apis/api/cloudbuild.googleapis.com/overview?project=my-terraform-service-project"
│ }
│ ]
│ },
│ {
│ "@type": "type.googleapis.com/google.rpc.ErrorInfo",
│ "domain": "googleapis.com",
│ "metadata": {
│ "consumer": "projects/my-terraform-service-project",
│ "service": "cloudbuild.googleapis.com"
│ },
│ "reason": "SERVICE_DISABLED"
│ }
│ ]
The Cloud Build API, along with several other Google Cloud Platform APIs, must be enabled in both the project where the service account (used to initiate API calls) resides, and the project where Cloud Build resources (e.g., triggers, builds) are created. This is expected behavior and there is noting we can/should do on the terraform provider side. The recommended way to handle it would be enabling CloudBuild API on the terraform SA project. @hao-nan-li we may close the ticket.
Thanks @averbuks!
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Community Note
modular-magician
user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned tohashibot
, a community member has claimed the issue already.Terraform Version
Affected Resource(s)
google-beta
provider and specificallygoogle_cloudbuild_trigger
Terraform Configuration Files
Debug Output
https://gist.github.com/clive-h-townsend/76517f93e88750058f3deda1bb6950e9
Expected Behavior
Create the Cloud Build Trigger.
Actual Behavior
Failed because it says the API is not enabled. The API is indeed enabled (confirmed by checking on the console). It appears that the
project=
field is not working because the error goes away when I add Cloud Build API on the service account project. But, we are looking to setup the trigger on theproject=
project.Steps to Reproduce
terraform apply
References
https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloudbuild_trigger#project