Terraform repeatedly wants to recreate AWS security groups due to non-deterministic 'tags' ordering in provider responses
Environment
Terraform 1.2.9 with hashicorp/aws provider 3.63.0 managed via CI (GitHub Actions). Resources include aws_security_group and aws_instance with 'tags' block. Team runs 'terraform apply' in CI after every merge; PRs show terraform plan proposing resource recreation for security groups when only tag ordering has changed.
Symptoms
A no-op code change (formatting) triggers 'terraform plan' to show replacement for existing AWS resources (not just in-place tag updates). The diff shows tags reordered in the provider's plan output (map vs list ordering issue). Recreating security groups is disruptive to production workloads.
Observations
'terraform plan -out=tfplan' then 'terraform show -json tfplan' shows 'tags' attribute differs: provider returns tags in different key order than what's stored in state. The resources' IDs remain the same in AWS Console. Manually running 'aws ec2 describe-tags' shows the tags exist but order is arbitrary. Using lifecycle ignore_changes on tags suppresses the plan change. Upgrading/downgrading provider versions temporarily changes behavior.
Already attempted
Tried pinning provider version in required_providers (no stable resolution). Converted tag blocks to use locals with 'toset' and 'tomap' transformations in Terraform — partially helped but plan still shows diffs for some resource types. Enabled provider debug logs: provider returns tags sometimes as list, sometimes as map. CI Terraform runs under a different Terraform binary on self-hosted runners with cached provider plugins.
Question
Why does Terraform propose to recreate AWS resources when only tags order differs, and what are safe, maintainable approaches to prevent unnecessary resource replacements without masking real changes?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags