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

You imported an IAM role and the plan wants to remove two managed policy attachments that prod depends on.

IntermediateTroubleshootingImport, Already Exists and State Surgery
Answer

Likely causes → The code uses managed_policy_arns (exclusive management) or aws_iam_role_policy_attachments_exclusive, so Terraform removes attachments not listed in code.

Diagnose → aws iam list-attached-role-policies --role-name app-role

Fix → Add all real attachments to code (or import the existing aws_iam_role_policy_attachment resources) before applying. Note managed_policy_arns is deprecated in AWS provider v5; prefer separate attachment resources.

Prevent → After every import, require a plan with zero changes before merging.

All Terraform questions Roadmap: Terraform in production

More on Import, Already Exists and State Surgery