Interview Prep/Terraform/Lost, Corrupted and Drifted State

Every plan shows an update for `aws_security_group` and `aws_security_group_rule` alternately adding and removing the same rule.

IntermediateTroubleshootingLost, Corrupted and Drifted State
Answer

Likely causes → Inline ingress {} blocks in aws_security_group and separate rule resources manage the same group. Each one thinks the other's rules are drift.

Diagnose → grep -rn 'security_group_id\s*=\s*aws_security_group.web' . alongside ingress { in the group.

Fix → Choose one style. Current best practice: no inline rules, use aws_vpc_security_group_ingress_rule / aws_vpc_security_group_egress_rule. Move rules with removed/import to avoid gaps.

Prevent → Same rule for route tables (route {} vs aws_route) and IAM (inline policy vs attachment resources).

All Terraform questions Roadmap: Terraform in production

More on Lost, Corrupted and Drifted State