Terraform Google provider "Error 409: already exists" in CI
Google returned Error 409 ... alreadyExists: the resource name is already taken in the project, but Terraform tried to create it because state has no record of it. The names collide.
What this error means
terraform apply fails with "googleapi: Error 409: The resource 'projects/.../X' already exists, alreadyExists" for a bucket, instance, or dataset.
Terraform
Error: Error creating Network: googleapi: Error 409: The resource
'projects/my-proj/global/networks/vpc-main' already exists, alreadyExistsCommon causes
The resource exists but is missing from state
It was created manually or by another state/workspace, so Terraform sees a name conflict instead of its own resource.
A prior run created it then failed before saving state
The create succeeded, but the run errored before writing state, leaving the resource orphaned from Terraform.
How to fix it
Import the existing resource
- Confirm the resource should be managed by this config.
- Import it using the provider's expected import id.
- Re-run plan to confirm no recreation.
Terminal
terraform import google_compute_network.main \
projects/my-proj/global/networks/vpc-mainUse a unique name if it should be new
If the existing resource is unrelated, derive a unique name so there is no collision.
main.tf
name = "vpc-main-${var.environment}"How to prevent it
- Import pre-existing GCP resources before the first apply.
- Keep one backend/workspace so state matches the project.
- Derive unique names per environment.
Related guides
Terraform Google provider "Error 403 ... does not have permission" in CIFix Terraform Google provider "googleapi: Error 403: ... does not have <permission>" in CI - the service acco…
Terraform AWS provider "EntityAlreadyExists" for an IAM resource in CIFix Terraform AWS provider "EntityAlreadyExists: Role/Policy with name X already exists" in CI - the IAM enti…
Terraform AzureRM provider "A resource with the ID already exists" (needs import) in CIFix Terraform AzureRM "A resource with the ID ... already exists - to be managed via Terraform this resource…