Skip to content
Latchkey

Terraform "Resource already exists" - Fix with terraform import

Terraform tried to create a resource that already exists in the provider, but it is not tracked in state. The two are out of sync, so apply collides with the real object.

What this error means

terraform apply fails creating a resource because one with the same name/identifier already exists in the cloud. State has no record of it, so Terraform tries to create rather than manage it.

terraform apply output
Error: creating IAM Role (app-role): EntityAlreadyExists: Role with name
app-role already exists.

  with aws_iam_role.app,
  on iam.tf line 1, in resource "aws_iam_role" "app":

Common causes

Resource created outside Terraform

The object was created manually or by another tool. Terraform has no state for it, so it attempts to create a duplicate and the provider rejects it.

State lost or not shared

If state was deleted, or a different pipeline used a separate state, Terraform does not know the resource already exists.

How to fix it

Import the existing resource into state

Bring the real object under management so Terraform manages it instead of recreating it.

Terminal
terraform import aws_iam_role.app app-role
terraform plan   # should now show no creation

Use import blocks for repeatable imports

For CI-friendly, reviewable imports, declare them in config (Terraform 1.5+).

import.tf
import {
  to = aws_iam_role.app
  id = "app-role"
}

How to prevent it

  • Use a shared, durable backend so state is never lost or forked.
  • Create resources through Terraform, not the console, to avoid duplicates.
  • Use import blocks to adopt pre-existing resources cleanly.

Related guides

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