Interview Prep/Terraform/Import, Already Exists and State Surgery

Apply fails with `EntityAlreadyExists: Role with name app-role already exists.`

BeginnerTroubleshootingImport, Already Exists and State Surgery
Answer

Likely causes → The role was created manually, by another stack, or by a previous run whose state was lost. IAM names are unique per account.

Diagnose → aws iam get-role --role-name app-role --query 'Role.[Arn,CreateDate,Tags]' and check tags to find the owner.

Fix → If this stack should own it, import:

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

If another stack owns it, rename yours or reference it with a data "aws_iam_role".

Prevent → Prefix names with stack/env (${var.env}-app-role) or use name_prefix so stacks can't collide.

All Terraform questions Roadmap: Terraform in production

More on Import, Already Exists and State Surgery