How to fix Error: Module version requirements have changed in Terraform
This error occurs when Terraform detects a mismatch between your module version constraints and the currently locked versions. It happens when you update version requirements in your module declarations without updating the .terraform.lock.hcl file.
Quick fix
Fixes Error: Module version requirements have changed
terraform init -upgrade- Run terraform init with the upgrade flag
- Verify your module version constraints
- Delete and reinitialize if needed
- Review and commit the updated lock file
terraform init -upgradeError: Module version requirements have changed
What this error means
Terraform maintains a lock file (.terraform.lock.hcl) that tracks the exact versions of modules and providers used in your configuration. When you modify your Terraform configuration to change module version constraints (e.g., updating from `version = "~> 1.0"` to `version = "~> 2.0"`), Terraform detects that the previously locked versions no longer satisfy the new constraints. This mismatch causes Terraform to refuse initialization until the lock file is reconciled with your updated configuration.
Typical symptoms
- Terraform init fails with 'Module version requirements have changed' error
- Changes to version constraints in module source declarations trigger the error
- The .terraform.lock.hcl file contains versions incompatible with updated constraints
- Error appears when switching between different branches with different module versions
Common causes
- You updated module version constraints in your configuration without reinitializing
- Version constraints were changed in a shared module or root configuration
- The .terraform.lock.hcl file is out of sync with the current configuration
- Multiple team members updated version requirements in different ways
- Upgrading to a newer version of Terraform with stricter version matching
How to fix "Error: Module version requirements have changed"
Execute terraform init -upgrade to tell Terraform to update module versions to satisfy your new constraints and update the lock file:
terraform init -upgradeThis command will redownload modules that match your updated constraints and regenerate the .terraform.lock.hcl file with compatible versions.
Review the version constraints in your Terraform configuration to ensure they are correct. Check all module blocks for version attributes:
module "example" {
source = "./modules/example"
version = "~> 2.0"
}Ensure the version constraints use valid operators like >=, ~>, <=, and !=.
If terraform init -upgrade doesn't resolve the issue, delete the .terraform directory and reinitialize:
rm -rf .terraform
terraform initThis performs a clean initialization and downloads modules matching your current constraints from scratch.
After running terraform init -upgrade, review the changes to .terraform.lock.hcl:
git diff .terraform.lock.hclCommit the updated lock file to version control to ensure your team uses the same module versions:
git add .terraform.lock.hcl
git commit -m "Update Terraform module lock file"Advanced notes
For reusable modules (those intended for use across multiple projects), follow HashiCorp best practices: specify only minimum version constraints (e.g., >= 1.0.0) rather than maximum constraints (e.g., ~> 1.0). This prevents version conflicts in downstream configurations. Root configurations can then manage maximum versions. Additionally, if working in Terraform Cloud, ensure all team members have the same version of Terraform installed, as version mismatches across the team can cause lock file conflicts. When working with CI/CD pipelines, lock the Terraform version in your pipeline configuration (e.g., using terraform version in GitHub Actions) to prevent version drift.
Frequently asked questions
- What does "Error: Module version requirements have changed" mean?
- hcl) that tracks the exact versions of modules and providers used in your configuration. 0"), Terraform detects that the previously locked versions no longer satisfy the new constraints. This mismatch causes Terraform to refuse initialization until the lock file is reconciled with your updated configuration.
- How do I fix "Error: Module version requirements have changed"?
- This error occurs when Terraform detects a mismatch between your module version constraints and the currently locked versions. It happens when you update version requirements in your module declarations without updating the .terraform.lock.hcl file.
- What causes "Error: Module version requirements have changed"?
- You updated module version constraints in your configuration without reinitializing Version constraints were changed in a shared module or root configuration The .terraform.lock.hcl file is out of sync with the current configuration
- Is the "Error: Module version requirements have changed" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.