Lesson 1 of 4 Terraform Modules

Module Sources and the Terraform Registry

Every Terraform configuration is made of modules, and the source argument tells Terraform where each child module comes from. This lesson covers root and child modules and every kind of module source.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish root and child modules.
  • Identify local, registry, Git and other module sources.
  • Interpret Git references and subdirectory syntax in module sources.

Root and child modules

A module is a set of .tf files in one directory. The directory where you run Terraform is the root module. It can call other modules with module blocks; those are child modules, and they can call modules in turn. A module groups related resources behind an interface of input variables and outputs, so a caller can create a whole pattern, such as a network or a static website, with a few arguments.

module "network" {
  source = "./modules/network"
  cidr   = "10.0.0.0/16"
}

Modules installed by Terraform are stored in .terraform/modules, and terraform init (or terraform get, which only installs modules) downloads them. A module's code runs with the caller's credentials, so treat third-party modules like any other dependency: review them, prefer verified publishers and pin versions.

Local paths and registries

The source argument is required and, in Terraform 1.12, must be a literal string. The two most common forms are local paths and registry addresses.

  • Local path: begins with ./ or ../, such as "./modules/network". The files are used in place, not copied, so edits take effect at once, and the version argument is not allowed.
  • Public Terraform Registry: NAMESPACE/NAME/PROVIDER, such as "terraform-aws-modules/vpc/aws". Supports version.
  • Private registry: HOSTNAME/NAMESPACE/NAME/PROVIDER, such as "app.terraform.io/example-org/vpc/aws" for an HCP Terraform private registry. Supports version and needs credentials, for example from terraform login.

The public registry marks some modules as partner modules, published by HashiCorp technology partners, and every module page lists its inputs, outputs, dependencies and versions.

The leading ./ matters

A path without ./, such as "modules/network", is not treated as a local path. Terraform interprets it as a registry address and fails.

Sort into groups

Name the module source

Exam skills 5.1.2 · 5.1.3

Identify the kind of module source each address uses.

Log in to try this activity Interactive activities are free with an account.

Version control and other sources

Modules can also be installed from version control systems and from archives. The following table lists the forms.

Source typeExample
GitHub shorthandgithub.com/acme/terraform-aws-network
Bitbucket shorthandbitbucket.org/acme/terraform-aws-network
Generic Git over HTTPS or SSHgit::https://git.example.com/network.git or git::ssh://[email protected]/network.git
Mercurialhg::https://hg.example.com/network
HTTP archivehttps://example.com/network-module.zip
Amazon S3 buckets3::https://s3-eu-west-1.amazonaws.com/acme-modules/network.zip
Google Cloud Storagegcs::https://www.googleapis.com/storage/v1/acme-modules/network.zip

For Git sources, append ?ref= to select a tag, branch or commit, as in git::https://git.example.com/network.git?ref=v1.4.0. A double slash selects a subdirectory of the repository: git::https://git.example.com/infra.git//modules/vpc?ref=v1.4.0. Git uses the credentials of the machine running Terraform, such as an SSH key.

The following diagram breaks a Git module source address into its parts.

Reading a Git module source. The git:: prefix and SSH address select the repository, the double slash selects the network subdirectory, and ?ref selects the network-v2.3.0 tag. The version argument is valid only for registry sources, so it cannot be added here; a private registry would allow version constraints.
The address is illustrative.
Scenario questions

Git module addresses

Exam skills 5.1.3

Build Git module source addresses that select the right code.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing a source

A platform team keeps its modules in a private Git repository, one module per subdirectory, and tags releases such as network-v2.3.0. An application team wants version 2.3.0 of the network module. Which source should they use, and can they add version = "2.3.0"?

The decisive fact is the source type.

The source is git::ssh://[email protected]/platform/modules.git//network?ref=network-v2.3.0: the double slash selects the subdirectory and ref selects the tag. The version argument is not valid for Git sources. If the platform team published to an HCP Terraform private registry instead, callers could use a registry address with version constraints.

Check yourself

How do you select tag v2.0.0 of a module in a Git repository?

Show answer

Add ?ref=v2.0.0 to the source address; the version argument works only for registry modules.

Exam Tips

Remember for the exam

  • The root module is the working directory; called modules are child modules.
  • Local paths start with ./ or ../; registry addresses are NAMESPACE/NAME/PROVIDER.
  • Git: ?ref= for revisions, // for subdirectories.
  • In Terraform 1.12, source must be a literal string.