Skip to content
Latchkey

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.

terraform
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.

Terminal
terraform init -input=false -upgrade
terraform plan -input=false

Manage the .terraform cache carefully

  1. Key any .terraform cache on the module/lock files so it invalidates on change.
  2. Always run init before plan/apply in CI.
  3. 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.

Frequently asked questions

What causes ""Module source has changed""?
The module source or version constraint was edited, but the cached .terraform/modules still holds the old install.
How do I fix "Module source has changed"?
Run init (with -upgrade when versions changed) so the installed modules match the config.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card