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

After importing an EC2 instance, every plan shows `user_data` changes or a replacement.

IntermediateTroubleshootingImport, Already Exists and State Surgery
Answer

Likely causes → The provider stores a hash/base64 of user data; your code's template renders differently (trailing newline, different variables). With user_data_replace_on_change = true this forces replacement.

Diagnose →

aws ec2 describe-instance-attribute --instance-id i-0abc123 --attribute userData \
  --query 'UserData.Value' --output text | base64 -d > real.sh
diff real.sh <(terraform console <<< 'local.user_data')

Fix → Make the template output byte-identical, or ignore_changes = [user_data] for instances where user data only matters at first boot.

Prevent → For long-lived instances, keep user data minimal and push config via SSM/Ansible.

All Terraform questions Roadmap: Terraform in production

More on Import, Already Exists and State Surgery