Prompt
Why is my Terraform plan different in dev and prod?
Latest observation
Terraform plans differ between dev and prod for a few common reasons. The short version: Terraform plans are driven by the combination of configuration, input values, provider behavior, and the current real-world state. If any of those differ, the plan can differ.
Common reasons
1) Different variables or environment-specific values
Dev and prod often use different:
*.tfvarsfiles- workspace names
- environment variables
- module inputs
Examples:
- different instance sizes
- different counts
- different CIDRs/subnets
- different tags or naming prefixes
2) Different remote state or current resource state
If dev and prod point to different backends or state files, Terraform will compare against different existing infrastructure.
Also, if someone manually changed prod resources outside Terraform, the plan may show drift.
3) Different provider versions
If dev and prod use different Terraform provider versions, the plan can change because providers may:
- default fields differently
- normalize values differently
- introduce breaking changes
Check:
required_providers.terraform.lock.hcl- CI vs local installed versions
4) Different Terraform versions
Terraform CLI version differences can affect:
- expression evaluation
- provider interaction
- plan formatting
- handling of null/unknown values
5) Data sources returning different values
Data sources are often environment-dependent:
- latest AMI/image
- current account/region
- existing network resources
- secrets or parameters
If dev and prod are in different regions/accounts, the data source outputs can differ.
6) Conditional logic in code
Your config may include:
countorfor_eachconditionsifexpressionslocalsbased on environment- module branching
For example:
count = var.environment == "prod" ? 3 : 1
This guarantees different plans.
7) Default values and unset arguments
A value omitted in one environment might be explicitly set in another. Providers may treat:
null- empty string
- unset
- defaulted
differently, leading to drift or plan diffs.
8) Ordering/normalization differences
Some resources have fields that providers sort or normalize:
- security group rules
- IAM policies
- JSON structures
- sets vs lists
This can create plan differences even when the logical config is “the same.”
9) Manual changes or drift
If someone changed prod manually in the cloud console/CLI, Terraform will see a different actual state and plan accordingly.
10) Different modules or code paths
If dev and prod use:
- different branches
- different module versions
- different
terraform.tfvars - different override files
then the plans will naturally diverge.
How to debug it
Compare the exact inputs
Check:
terraform.tfvars- environment variables
- workspace
- module versions
backendconfig- provider versions
Compare the state
Run:
terraform state list
terraform show
and compare dev vs prod.
Inspect the plan JSON
Generate machine-readable plans:
terraform plan -out=tfplan
terraform show -json tfplan > plan.json
Then compare the JSON outputs.
Check provider and Terraform versions
Run:
terraform version
and verify .terraform.lock.hcl.
Look for drift
Run:
terraform plan -refresh-only
This helps identify changes made outside Terraform.
Best practices to keep dev/prod predictable
- Pin Terraform and provider versions
- Use the same module version across environments
- Keep environment-specific values isolated in tfvars
- Avoid relying on unstable data sources when possible
- Use remote state consistently
- Prevent manual changes or regularly reconcile drift
- Review plans in CI with the same versions used locally
If you want, I can help you debug your specific dev/prod diff if you paste:
- the relevant
terraform plandifferences, - your environment variable/tfvars setup, and
- your provider + Terraform versions.
Brands