What modules are and the root module
Every Terraform directory is already a module. How the root module calls child modules, how addresses and scope change, and what init does for them.
What modules are and the root module
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A module is a directory of `.tf` files. The directory you run Terraform in is the root module, and any module it loads with a `module` block is a child module.
- A child module cannot see the caller's variables or resources. Values go in as the arguments of the `module` block and come out as the child's `output` values.
- Resources in a child module get a longer address, such as `module.network.aws_vpc.main`, and each call keeps its own part of state.
- `terraform init` finds every module block, downloads or links the source, and records it under `.terraform/modules`. Adding a module call or changing its source needs another init.
- A module call is a function call with a block's syntax. If a module only wraps one resource and adds nothing, it is probably not worth having.
Copy, paste, drift
Your team needs the same network in dev, staging and prod: a VPC, three subnets, route tables, a NAT gateway. The first time, you write sixty lines. The second, you copy them. The third, someone fixes a route in prod and forgets the others, and now the three environments differ in ways nobody listed. Functions solve this in ordinary code. A module is the same idea for Terraform: write the group of resources once, give it inputs, and call it from each place.
The devops lesson on modules shows the first module you will write. This group goes further, starting with the vocabulary.
Every directory is a module
The Terraform docs state it plainly: every workspace includes configuration files in its root directory, and Terraform calls that configuration the root module. Modules you load with module blocks are child modules. When you apply, the root module calls the children, and Terraform adds their resources to your workspace and manages them as part of the configuration. A child can call its own children.
So there is nothing special about a module’s files. A directory with main.tf, variables.tf and outputs.tf is a module. It becomes the root when you run Terraform inside it, and a child when another module points at it. This is written from the docs. I can’t run Terraform here, so the snippets are unrun:
# root module: ./main.tf
module "network" {
source = "./modules/network"
cidr_block = "10.0.0.0/16"
azs = ["eu-west-1a", "eu-west-1b"]
}
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.small"
subnet_id = module.network.private_subnet_ids[0]
}
source says where the module’s code is. The other arguments are the module’s inputs. module.network.private_subnet_ids reads one of the child’s outputs. That is the whole interface, and the next lesson covers how to write the other side.
Scope: nothing leaks
A child module cannot see the caller’s variables, locals or resources. It sees only what its own variable blocks declare, and the caller supplies those as arguments. In the other direction the caller sees only the child’s output blocks. This is the point of a module: the inside can change without breaking callers, as long as the inputs and outputs stay the same.
It also explains a common error. If you reference aws_vpc.main.id inside a child module and the VPC is defined in the root, Terraform says it is undeclared. Pass the ID in.
Addresses and state
Resources inside a module have longer addresses. A subnet that was aws_subnet.private["a"] in the root becomes module.network.aws_subnet.private["a"] once it lives in a module, and one in a nested module reads module.app.module.network.aws_subnet.private["a"]. State stores that address, which is why moving existing resources into a module needs a moved block, as in the state group. terraform state list shows the module prefix, and you can filter by it: terraform state list module.network.
Calling a module several times creates several sets of resources, each with its own address. A call can also use count or for_each, which puts [0] or ["key"] after module.network. The composition lesson deals with that.
What init does
terraform init reads every module block. For a local path it records a link. For a registry or Git source it downloads the code into .terraform/modules and writes a manifest listing what was fetched. Add a module block or change a source or version, and Terraform tells you to run terraform init again. This is why a fresh clone of a repo cannot plan until it has been initialised, and why .terraform/ goes in .gitignore while .terraform.lock.hcl is committed. The lock file covers providers, not modules, so module versions need pinning by version or a Git ref. That is the subject of module sources and versioning.
Are modules always worth it?
A module adds a layer: another directory, another set of inputs to keep in step, and an indirection when you debug. Use one when you repeat a group of resources, when a team publishes a standard way to build something, or when hiding detail helps callers. Skip it for a single resource wrapped in a module that exposes every argument. That is a rename, not an abstraction.
About the video
The embed is Ned Bellavance’s “Terraform Basics: Modules”, published June 2021, 15 minutes. His description says it asks what a module is and how to get information in and out, which is this lesson. It has no chapters and is five years old, so it predates later module features such as moved and removed blocks. The module syntax it shows is unchanged. English was judged from the title, description and the channel’s English auto-caption track.
Where this fits
You can now read a module call. The next lesson is the other side of it: writing a module with inputs, outputs and a sane structure.
Check yourself
Which directory is the root module?
The one you run `terraform plan` and `apply` in. Its `.tf` files are the root module, and the modules it calls with `module` blocks are children.
A child module needs the VPC ID that the root module creates. How does it get it?
The root passes it as an argument in the `module` block, such as `vpc_id = aws_vpc.main.id`, and the child declares a matching `variable "vpc_id"`. A child cannot reach into the caller on its own.
What does the address `module.network.aws_subnet.private["a"]` tell you?
It is the `private` subnet instance with key `a`, inside the `aws_subnet` resource, inside the child module called `network` from the root module.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.