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.

Diagnose it: init, state, or credentials?

Terraform failures in CI are dominated by backend and credential problems rather than configuration errors. Confirm the runner can initialise, authenticate, and lock state before reading the plan.

Terminal
terraform init -backend=true -input=false
terraform validate
terraform providers
terraform plan -input=false -no-color -detailed-exitcode
#   0 = no changes, 2 = changes, 1 = error

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 Terraform "Module source has changed" in CI?
There are 2 common causes: source/version changed without re-init and stale cached .terraform in ci. The module source or version constraint was edited, but the cached .terraform/modules still holds the old install.
How do I fix Terraform "Module source has changed" in CI?
There are 2 fixes depending on which cause you have: re-initialize after module changes and manage the .terraform cache carefully. Work through them in order, since the first is the most common.
What does Terraform "Module source has changed" in CI actually mean?
plan/apply fails saying a module source has changed and init must run, or that the module is not yet installed.
How do I stop Terraform "Module source has changed" in CI happening again?
Run terraform init before plan/apply in every CI job. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card