Skip to content
Latchkey

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, alreadyExists

Common 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

  1. Confirm the resource should be managed by this config.
  2. Import it using the provider's expected import id.
  3. Re-run plan to confirm no recreation.
Terminal
terraform import google_compute_network.main \
  projects/my-proj/global/networks/vpc-main

Use 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →