Terraform "Module source has changed" in CI
Terraform records installed module sources in .terraform/modules. When a module block source or version changes but init was not re-run, plan/apply detects the mismatch and stops.
What this error means
plan/apply fails saying a module source has changed and init must run, or that the module is not yet installed. It happens when CI reuses a cached .terraform without re-initializing after a module edit.
Error: Module source has changed
on main.tf line 1, in module "network":
1: module "network" {
The source address was updated, but the module installed in .terraform/modules
does not match. Run "terraform init" to install the updated module.Common causes
Source/version changed without re-init
The module source or version constraint was edited, but the cached .terraform/modules still holds the old install.
Stale cached .terraform in CI
A pipeline that caches .terraform across runs can serve a module install that no longer matches the config.
How to fix it
Re-initialize after module changes
Run init (with -upgrade when versions changed) so the installed modules match the config.
terraform init -input=false -upgrade
terraform plan -input=falseManage the .terraform cache carefully
- Key any .terraform cache on the module/lock files so it invalidates on change.
- Always run init before plan/apply in CI.
- Avoid sharing a stale .terraform across incompatible configs.
How to prevent it
- Run terraform init before plan/apply in every CI job.
- Invalidate .terraform caches when module sources/versions change.
- Use -upgrade after changing version constraints.