Terraform "creating IAM Role: EntityAlreadyExists" in CI
IAM entity names are unique per account. Terraform tried to create a role/policy/user whose name is already in use, usually because the entity exists out-of-band and is not tracked in state.
What this error means
apply fails creating an aws_iam_role (or policy/user) with EntityAlreadyExists. It appears when a role was created manually or by another stack, or when state was lost and the role still exists.
Error: creating IAM Role (ci-deploy-role): operation error IAM: CreateRole,
https response error StatusCode: 409, EntityAlreadyExists: Role with name
ci-deploy-role already exists.
with aws_iam_role.deploy,
on iam.tf line 1, in resource "aws_iam_role" "deploy":Common causes
Entity exists but is not in state
A role/policy/user with the same name was created manually or by another config, so Terraform creating it collides.
State lost or pointed at the wrong backend
After a lost or mis-targeted state, Terraform no longer knows it manages the entity and tries to create it again.
How to fix it
Import the existing entity
Bring the pre-existing role under Terraform management with import (or an import block), then plan.
terraform import aws_iam_role.deploy ci-deploy-role
terraform plan # confirm no recreateOr make the name unique
- If a duplicate is genuinely unwanted, give the resource a unique name or name_prefix.
- Confirm no other stack manages the same entity name.
- Verify the backend points at the correct state for this account.
How to prevent it
- Import out-of-band IAM entities rather than recreating them.
- Use name_prefix to avoid cross-stack name collisions.
- Protect and back up state so Terraform keeps tracking what it owns.