Go to content
Infrastructure

Should You Use Local or Remote Terraform Modules?

Terraform modules are reusable packages of Terraform configuration. A module groups resources, such as an AWS VPC or an EC2 instance with its security group, and exposes them through input variables, which let you pass values in, and outputs, which return values out, so you can deploy the same infrastructure pattern in multiple places without copying code. You can load a module from a local path on disk or from a remote source such as a Git repository, the public Terraform Registry, or a private registry.

The short answer: use local modules to organize code inside a single configuration, and use remote modules when you need to share the same code across multiple configurations or teams and control when each consumer picks up changes. For AWS users, this is the choice between keeping a module like a VPC or ECS service definition in the same repo as your environments, or publishing it as a versioned module that multiple accounts and teams consume. The deciding factor is versioning. A remote source lets you pin a module to a specific release, while a local path applies changes to every caller immediately.

By the end, you will know how each source behaves during terraform init, why the dependency lock file does not protect you from module drift (the module version changing without your explicit choice), and a practical rule for choosing based on team size and reuse.

How Terraform loads modules from each source

When you declare a module block with source = "./modules/vpc", Terraform reads the code directly from that directory on every run. When you use a remote source such as git::https://github.com/example/terraform-aws-vpc.git?ref=v1.2.0, terraform init downloads the code into the local .terraform directory before you run plan or apply.

Re-running terraform init installs sources for modules added since the last run, but it does not change already-installed modules unless you pass -upgrade. The -upgrade flag updates all modules to the latest available source code, which is useful when you want to pull in new releases deliberately. The terraform init command reference documents this behavior.

The important detail is what Terraform does not remember. The dependency lock file (.terraform.lock.hcl) records the versions and checksums of the provider plugins Terraform uses to talk to cloud APIs, but it does not track module versions. The dependency lock file documentation states that Terraform will always select the newest available module version that meets the version constraints you specify. If you write version = "~> 2.0" and a new minor release appears, a fresh init picks it up.

Local path modules sit outside this system entirely. They have no version argument, and changes to the module affect the caller immediately, as this overview of module versioning gotchas explains.

When local modules are the right call

HashiCorp recommends using local modules to organize and encapsulate code even if you never publish a remote module. The modules tutorial makes this the default starting point: split a configuration into modules to keep it readable, then decide later whether any of those modules deserve their own release cycle.

Local modules fit a single repository that contains several environments, such as dev, staging, and production, or a small team that wants to avoid the overhead of a separate module repository and release process. Because every caller runs the same code, you can change a module and test all of its consumers in one pull request.

Keep the module tree flat. AWS Prescriptive Guidance recommends avoiding nesting modules more than one or two levels deep, because deeply nested inheritance complicates configuration and troubleshooting. It also advises against wrapping a single resource in a module; a module should encapsulate a logical relationship and be sized around one capability. If you find yourself creating a module for one EC2 instance, you are probably adding indirection without reuse.

With local modules, you cannot roll out a change gradually. When you update a local module, every environment that references it picks up the change on the next plan and apply. That is fine when you control all callers and can test them together, and it is a problem when you want to update staging first and production a week later. If that gradual rollout matters, you need a versioned remote source. For more on when modules earn their place, see when to use Terraform modules.

When remote modules earn their cost

Remote modules give you versioning and an independent release process. When you source a module directly from Git, you can pin to a tag, branch, or commit hash using the ref= query parameter, as this guide to advanced module usage shows. That lets different environments use different versions of the same module, such as v0.0.1 in production and v0.0.2 in staging.

Versioning works best with Semantic Versioning: MAJOR for incompatible changes, MINOR for backward-compatible new features, and PATCH for backward-compatible bug fixes. Terraform module versioning best practices recommend following that scheme, maintaining backward compatibility, documenting changes, and using version constraints such as version = "~> 2.0" in module blocks to avoid breaking changes from automatic upgrades.

If a module will be reused by many configurations, AWS Prescriptive Guidance recommends placing it in its own version control repository. That gives the module its own history, its own tests, and its own release tags. If you want to publish it to the public Terraform Registry, the registry requires a public GitHub repository named terraform-aws-<NAME>, a standard module structure, and at least one semantic version tag. HashiCorp’s naming convention for provider modules is terraform-<PROVIDER>-<NAME>, and the modules tutorial notes that this convention is required to publish to HCP Terraform or Terraform Enterprise module registries.

You can move a module from a local path to a Git source cheaply. Jonathan Bergknoff’s Terraform practices point out that Terraform resource addresses do not depend on the module’s source, so you can change the source argument without rewriting the resources inside. The same module can start as a local directory and later become a versioned Git reference without touching the rest of the configuration.

Before you release a module to other teams, test it in isolation. Testing Terraform modules before release covers how to validate a module’s behavior before consumers start depending on it.

The versioning trap: what the lock file does not do

The most common mistake is assuming that committing .terraform.lock.hcl protects your module versions. It does not. The lock file records provider packages and their checksums. Terraform trusts those checksums the first time it sees a provider and verifies them on later runs, but module sources are resolved separately during init. As noted earlier, Terraform does not remember version selections for remote modules.

That means a constraint like version = "~> 2.0" is a moving target. A fresh init in a clean environment, or a CI runner without a populated module cache, will fetch the newest 2.x release. If that release contains an unintended change, your infrastructure can drift from what you tested.

To make a remote module reproducible, use an exact version constraint, or pin the Git ref to a specific tag or commit. The dependency lock file documentation confirms that an exact version constraint ensures Terraform always selects the same module version.

Local paths have the opposite problem: there is no version to pin, so you cannot reproduce a past state. If you need to know exactly which module code produced the current state, you need a remote source with a tag or commit hash. This is the core tradeoff between the two approaches. When you design the module interface, keep it stable so consumers do not need to chase changes; designing stable Terraform module interfaces across environments explains how to structure inputs and outputs for that.

Repo structure: monorepo, multi-repo, or hybrid

The local-versus-remote decision usually follows your repository structure. A monorepo keeps all modules and configurations in one repository, which centralizes collaboration and makes dependency management easier. The tradeoff, as Thoughtworks’ comparison of monorepo and multi-repo notes, is that pulling code can slow down and security becomes harder because every contributor gets access to the whole codebase.

A multi-repo setup, with each module in its own repository, offers better security, scalability, and independent versioning, but it makes end-to-end testing and cross-repo refactoring harder. A change that touches two modules requires two pull requests and a coordinated release.

Team size and module count give a practical starting point. This guide to Terraform module organization suggests a monorepo pattern for teams under 20 engineers or organizations with fewer than 30 modules, and a multi-repo pattern for organizations with many modules or multiple platform teams. In between, a hybrid approach works well: keep foundational modules in a monorepo maintained by a platform team, and let product teams own domain-specific modules in separate repositories.

Bergknoff’s effective Terraform practices describe a similar split: a config repository for environment tfvars files, which set variable values per environment, a separate modules repository for reusable modules, and project-specific definitions in project repositories. The key is that reusable modules live somewhere with version tags, while environment-specific configuration stays close to the people who deploy it. For a deeper look at laying out these repositories, see structuring Terraform repositories for modules and environments.

Start local, move to remote when reuse appears

Start with local modules in a single repository. When a second configuration needs the same code, move that module into its own repository, tag it with a semantic version, and reference it by Git ref or registry version. You can make this move cheaply because resource addresses do not change.

Two rules apply regardless of which source you choose. First, do not configure providers inside reusable modules; AWS Prescriptive Guidance recommends declaring provider configuration only once in root modules, the top-level configuration that calls other modules, because hardcoding a provider inside a module couples it to a specific account, region, or credentials. Second, pin what you depend on. For remote modules, use an exact version or a fixed Git ref. For providers, commit the lock file so init selects the same versions. The five essential best practices for Terraform code cover these habits in more detail.

More in Infrastructure Back to top ↑