liveBook · Manning

Module block

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A module block is a Terraform configuration block that declares a module and its source, version, and provider settings.

Suppose a VPC needs a VPN, but you do not want to define every resource yourself. A module block calls reusable Terraform code that creates those resources. Its single label names the call; its arguments select the module and supply the values it needs. section 2.8 section 2.2.2

Listing 2.33 Module example
module "vpn" {
  source  = "tedivm/dev-vpn/aws"                     #1
  version = "~> 1.0"                                 #2
 
  identifier = "my-vpn"                              #3
  subnet_ids = data.aws_subnets.default.ids          #4
}

The VPN example selects a registry module, constrains its version, and passes an identifier and subnet IDs. The subnet IDs come from a data lookup rather than being hard-coded. section 2.8

The block's key settings have different jobs:

  • source is required and locates the module. It can point to a registry, a filesystem path, or a Git repository. section 2.8
  • version constrains updates when source is a registry module; providers can pass provider aliases from the calling module. section 2.8
  • Other arguments supply the called module's input variables. Its output values can be read through the named module call. section 2.8 section 3.8.4
Listing 3.36 Testing our local module
data "aws_vpc" "default" {                                   #1
  default = true                                             #1
}                                                            #1
 
 
data "aws_subnets" "default" {                               #1
  filter {                                                   #1
    name   = "vpc-id"                                        #1
    values = [data.aws_vpc.default.id]                       #1
  }                                                          #1
}                                                            #1
 
module "test_instance" {
  source    = "../"
  subnet_id = data.aws_subnets.default.ids[0]                #2
}
 
output "aws_instance_arn" {                                  #3
  value = module.test_instance.aws_instance_arn
}

For a local test, source = "../" calls the module in the parent directory. Data lookups provide a subnet ID, and an output exposes a value returned by the module. section 3.8.4

Configure the calling workspace's provider, then run terraform init. Review the plan before applying it. For a published Git module, init downloads and installs the referenced module; a repository path can select a submodule. section 3.8.4 section 3.8.5

Listing 8.10 Environment main.tf example
variable "logging_api_key" {                     #1
  sensitive = true                               #2
}
 
module "application" {                           #3
  source  = "registry.example.com/example/aws/application"
  version = "1.1.1"                              #4
 
  network_name = "dev_network"                   #5
  db_size      = 20                              #5
  num_tasks    = 5                               #5
 
  logging_api_key = var.logging_api_key          #6
}
 
output "application_url" {                       #7
  value = module.application.url
}

An environment root module can be a thin wrapper around an application module. This configuration pins a version, sets environment-specific arguments, passes a sensitive input through a variable, and exposes the application URL. section 8.4.2

Separate root modules let environments choose application-module versions independently. Put ordinary environment settings on each module block, but avoid saving sensitive values directly in source control. section 8.4.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains block settings and module interfaces through configurations and a local test; stops short of detailing module implementation internals.
Examples
Uses a registry VPN, a locally tested module with a subnet lookup, and a versioned application module for an environment.
What it assumes
Introduces module-specific settings while relying on basic familiarity with Terraform blocks, providers, and data lookups in the worked configurations.
What you can do after
Operate/configure it by selecting a module source, supplying inputs, initializing the workspace, and reviewing the plan before applying.
How it connects
Connects module calls to input variables, output values, provider aliases, data sources, and root modules.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.