Module sources and versioning

Intermediate · 24 min

Where a module can come from (path, registry, Git), how to pin it with version or ref, and why the lock file does not cover modules.

Before thisWriting a module: inputs, outputs and structureVersion constraints and the lock file

HashiCorp, an IBM Company · 24 min

Module sources and versioning

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by HashiCorp, an IBM CompanyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `source` says where the code lives: a local path, the public or a private registry, a Git repository, or an archive on HTTP or object storage. `terraform init` fetches it.
  • Registry modules take a `version` constraint such as `~> 6.0`. Git sources have no `version`, and you pin them with `?ref=` to a tag or, more firmly, a commit SHA.
  • `.terraform.lock.hcl` records provider versions and checksums, not modules. An unpinned module source can change under you on the next `init`.
  • `//` in a source selects a subdirectory of a repository, and any `?ref=` goes after it.
  • Variables and locals in a module `source` or `version` work in OpenTofu 1.8 and later and Terraform 1.15 and later. In Terraform the variable must be declared `const = true`.

The module that changed on Tuesday

A pipeline that passed on Monday fails on Tuesday with no change to the repository. A module’s author pushed an update, the module source was unpinned, and init pulled the new code. Providers are pinned by a lock file. Modules are not, so pinning them is your job, through version or ref. Before pinning anything, it helps to know where a module can live.

Sources

The source argument of a module block takes several forms. This is written from the module block docs. I cannot run Terraform here, so none of these blocks was run:

# a directory in this repository
module "network" { source = "./modules/network" }

# the public registry: namespace/name/provider
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 6.0"
}

# Git over HTTPS, pinned to a tag
module "queue" { source = "git::https://example.com/org/queue.git?ref=v1.4.2" }

# a subdirectory of a Git repository (note the double slash, then ref)
module "alarms" { source = "git::https://example.com/org/modules.git//alarms?ref=v2.0.0" }

Local paths need ./ or ../. The docs advise against absolute paths. A registry address has three parts, and app.terraform.io/... prefixed addresses point at an HCP Terraform private registry. Git sources run git clone, using your local Git credentials, and for SSH your keys, which is why CI for private modules usually uses SSH or a token in Git configuration. A // marks a subdirectory inside the cloned package. The docs say query parameters such as ref go after the subdirectory segment. Terraform copies the whole package and reads the module from the subdirectory, so modules in the same repository can call each other with local paths.

Pinning a registry module

A registry module has real versions, and version takes a constraint. I used the registry’s public API to try the real version list of a popular module. The list has 246 stable releases of terraform-aws-modules/vpc/aws, the newest 6.9.0 when I looked. Then I wrote a small version-constraint evaluator, a Python stand-in for Terraform’s own, and asked which release each constraint would select:

version = "~> 6.0"           -> 6.9.0
version = "~> 6.8.0"         -> 6.8.0
version = "~> 5.0"           -> 5.21.0
version = ">= 5.0, < 6.0"    -> 5.21.0
version = "6.9.0"            -> 6.9.0

~> 6.0 takes the newest 6.x, so it floats forward through minor releases and stops before 7.0. ~> 6.8.0 allows only 6.8.x patches. An exact version never moves. The constraints lesson covers the operators for providers, and the same syntax applies. For a module from a registry you do not control, I would pin exactly or to a minor range, and let a tool such as Dependabot or Renovate propose updates as reviewed pull requests.

The registry protocol is plain HTTP. Asking where a version’s code lives gets an empty 204 response with one header:

$ curl -s -i https://registry.terraform.io/v1/modules/terraform-aws-modules/vpc/aws/6.9.0/download
HTTP/2 204
x-terraform-get: git::https://github.com/terraform-aws-modules/terraform-aws-vpc?ref=de96717ffa9ef9315ea14687055c0d53f85db5c6

That header is the real answer for 6.9.0: a Git URL pinned to a commit SHA. A registry version is, underneath, an immutable pointer to one commit.

Pinning a Git module

There is no version for Git sources. The docs show two forms of ref: a tag, as in ?ref=v1.2.0, and a commit SHA, as in ?ref=51d462.... A tag is readable. A SHA is exact. Tags in Git can be deleted and recreated on another commit, so a tag is a promise from whoever controls the repository, and a SHA is a fact. For modules from outside your organisation I use the SHA, with a comment naming the release.

An internal registry removes the choice. HCP Terraform and Terraform Enterprise include a private registry, and you can also run a service that implements the registry protocol. The versions then behave like the public ones.

What the lock file does not cover

.terraform.lock.hcl records which provider versions and checksums init chose. It says nothing about modules. A module with version = "~> 6.0" can resolve to a newer release on the next fresh init, and a Git source without ref follows the default branch. Pin tightly enough that the plan you reviewed is the plan you apply.

Dynamic sources

Historically source and version had to be literal strings, so a module could not be pointed at a different version per environment with a variable. OpenTofu 1.8 allowed variables and locals in module sources and backend configuration, with limits. Terraform followed in 1.15, per its changelog: variables and locals are allowed in source and version, and the docs say any input variable used in source must declare const = true. Check which tool and version your team uses before relying on it.

About the video

The embed is “Git as Terraform module registry”, a HashiCorp-hosted community session, published December 2023, 23 minutes. Its description says it covers handling a module locally and then moving it to a Git repository, referring to sub-folders, and semantic versioning and best practices. The speaker is a community member, not a HashiCorp employee. It focuses on Git, so registry version constraints and private registries come from the docs in this lesson. It has no chapters. Mathis Van Eetvelde’s 2022 video in deeper is a live tutorial of the same topic with timestamps, and it has no caption track, so its language was judged from the title and description only. For the embed, English was judged from the title, description and the English auto-caption track.

Where this fits

A pinned module is a stable module. The next question is how modules should fit together: composition over nesting, and for_each on modules.

Check yourself

  1. 01You write `source = "git::https://example.com/net.git"` with no `ref`. What do you get on the next `terraform init` in a fresh checkout?

    The repository's default branch as it is then. The code can change between two inits, and nothing in the lock file stops it. Pin with `?ref=v1.2.0` or a commit SHA.

  2. 02Why is a commit SHA a stronger pin than a tag?

    A Git tag can be moved or deleted and re-created on a different commit. A commit SHA names exactly one tree of files, so the code can't change without the reference changing.

  3. 03What does `version = "~> 6.8.0"` allow, compared with `~> 6.0`?

    `~> 6.8.0` allows patch releases only, 6.8.x. `~> 6.0` allows any 6.x at or above 6.0, so minor releases are accepted but 7.0 is not.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark