TExcelTAD
Home › Infrastructure as Code: Terraform & OpenTofu › Module Sources, Versions & the Registry

Module Sources, Versions & the Registry

A module's source says where its code comes from. Local paths are for modules that live in the same repository; shared modules come from a registry or a Git repository, pinned to a version, so a change to the module never reaches your infrastructure until you choose to upgrade.

Source Example Version pinning
local path ./modules/network, ../../modules/app the repository's own commit
public registry terraform-aws-modules/vpc/aws version = "~> 6.0"
private registry app.terraform.io/acme/network/aws version = "2.3.1"
Git (HTTPS/SSH) git::https://github.com/acme/tf-modules.git//network?ref=v1.4.0 ?ref= a tag or commit SHA
GitHub shorthand github.com/acme/tf-modules//network?ref=v1.4.0 ?ref=
S3 / GCS / HTTP archive s3::https://s3-eu-west-1.amazonaws.com/acme-modules/network-1.4.0.zip the file name
OCI registry (OpenTofu 1.10+) oci://ghcr.io/acme/tf-modules/network?tag=1.4.0 ?tag= or ?digest=

// separates the repository from a subdirectory inside it, so one repository can hold many modules. The version argument only works with registry sources; everything else is pinned in the address itself.

Pinning and upgrading, for real

A Git repository on disk stands in for your organisation's module repository. Version 1.0.0 of a naming module is released, then 1.1.0 lowercases the prefix:

bash · try it yourself
mkdir -p repos/tf-modules/naming && cd repos/tf-modules
git init -q
cat > naming/main.tf <<'HCL'
variable "project" {
  type = string
}
variable "env" {
  type = string
}
output "prefix" {
  value = "${var.project}-${var.env}"
}
HCL
git add . && git commit -qm "feat: naming module" && git tag v1.0.0
sed -i 's/value = "${var.project}-${var.env}"/value = lower("${var.project}-${var.env}")/' naming/main.tf
git commit -qam "feat!: lowercase prefixes" && git tag v1.1.0
cd .. && git clone -q --bare tf-modules tf-modules.git && cd ~

mkdir app && cd app
cat > main.tf <<'HCL'
module "naming" {
  source  = "git::file:///home/learner/repos/tf-modules.git//naming?ref=v1.0.0"
  project = "Linkly"
  env     = "Prod"
}

output "prefix" {
  value = module.naming.prefix
}
HCL
tofu init | grep -E 'Downloading|^- '
tofu apply -auto-approve | tail -1
echo "--- bump the ref: init is required first"
sed -i 's/ref=v1.0.0/ref=v1.1.0/' main.tf
tofu plan 2>&1 | grep -E '^Error'
tofu init -upgrade | grep Downloading
tofu apply -auto-approve | tail -1
Downloading git::file:///home/learner/repos/tf-modules.git?ref=v1.0.0 for naming...
- naming in .terraform/modules/naming/naming
prefix = "Linkly-Prod"
--- bump the ref: init is required first
Error: Module source has changed
Downloading git::file:///home/learner/repos/tf-modules.git?ref=v1.1.0 for naming...
prefix = "linkly-prod"

The module's code is copied into .terraform/modules/, so the version you pinned is what every plan uses until someone changes the ref in a reviewed commit. With a real Git host, the address would be git::https://… or git::ssh://git@github.com/acme/tf-modules.git//naming?ref=v1.1.0, and the runner needs read access to the repository.

The public registry

The Terraform Registry (registry.terraform.io) and the OpenTofu Registry (registry.opentofu.org, also searchable at search.opentofu.org) list thousands of modules. The terraform-aws-modules collection is the best known for AWS:

hcl
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "~> 6.0"

  name = "linkly-prod"
  cidr = "10.20.0.0/16"

  azs             = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
  private_subnets = ["10.20.1.0/24", "10.20.2.0/24", "10.20.3.0/24"]
  public_subnets  = ["10.20.101.0/24", "10.20.102.0/24", "10.20.103.0/24"]

  enable_nat_gateway = true
  single_nat_gateway = true            # one NAT for dev; one per AZ in prod
}
bash
$ tofu init
Initializing modules...
Downloading registry.opentofu.org/terraform-aws-modules/vpc/aws 6.6.0 for vpc...   # the newest 6.x on that day
- vpc in .terraform/modules/vpc
…

Before adopting a public module:

  • Read its main.tf, not just the README. You become responsible for everything it creates.
  • Check maintenance (recent releases, open issues) and its provider constraints: a module that requires aws < 6.0 blocks your provider upgrade.
  • Pin it (~> 6.0 plus the lock-file-like record in .terraform/modules/modules.json) and upgrade deliberately, reading the changelog for moved blocks and breaking changes.
  • Wrapping a public module in a thin module of your own (your defaults, your tagging) is common; forking it is a last resort.

This course writes the AWS network by hand, so you know what such a module does inside.

Releasing your own modules

  • One repository per module (terraform-<provider>-<name>, required for registry publishing) or a monorepo with tags per module (network/v1.4.0).
  • Semantic versioning on the interface: major for removed or renamed variables and outputs, or for changes that replace resources; minor for new optional inputs and outputs; patch for fixes. Semantic Versioning covers the rules.
  • Automate releases from Conventional Commits (release-engineering), and run fmt -check, validate, tflint and test on every pull request (CI lessons).
  • A private registry (HCP Terraform, GitLab's Terraform module registry, Artifactory, Spacelift, an OCI registry with OpenTofu) adds version constraints (version = "~> 1.4") and discovery for internal modules.

Test yourself

What does //network mean in git::https://github.com/acme/tf-modules.git//network?ref=v1.4.0?

  1. A comment
  2. The module lives in the network subdirectory of the repository
  3. The protocol
  4. The branch name

Which sources accept the version argument?

  1. All of them
  2. Only registry sources
  3. Only Git sources
  4. Only local paths

You changed ?ref=v1.0.0 to ?ref=v1.1.0. What must happen before plan works?

  1. Nothing
  2. tofu init (or init -upgrade) to download the new module version
  3. tofu state mv
  4. tofu get -update=false