liveBook · Manning

Module

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

An application module is a Terraform module that contains the infrastructure configuration for an application.

Launching an API can mean configuring a load balancer, application tasks or instances, certificates, DNS, networking, and often a database. An application module packages the Terraform configuration for that application so deployments can reuse it rather than rebuild those pieces by hand. section 1.5.2 section 8.4.1

An application module is a Terraform module: a collection of related resources, data sources, and assets written in HCL. A root module is the entry point that configures providers and can call other modules. The application module may serve as that root, or a separate root module may call it. Input variables customize a call; outputs expose values such as the application URL. section 3.1 section 8.4.2 section 3.0

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
}

The environment’s main.tf calls a registry-hosted application module at version 1.1.1, supplies network, database, task-count, and sensitive logging inputs, then exposes its URL as an output. section 8.4.2

Choose the root-module arrangement according to how independently environments must change:

  • Use the application module itself as the root and supply environment-specific tfvars files. This keeps one codebase, but every environment shares the same underlying infrastructure configuration; variables do not let an environment retain an older module version. section 8.4.1
  • Give each environment a thin root module that calls the application module. Each root can select its own module version and set its own parameters, allowing independent rollouts. Keep sensitive values out of source control. section 8.4.2

A module boundary organizes code, but it is not an execution barrier. Terraform plans relationships between underlying resources, so a resource in a dependent module can sometimes be created before another resource in the module it calls on. Express the resource relationships the deployment needs rather than relying on module order alone. section 5.2.3

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

How deep it goes
Explains root-module arrangements, version selection, inputs, outputs, and the resource-graph caveat without detailing an application module’s full implementation.
Examples
Contrasts environment tfvars files with staging and production root modules, including a versioned registry call that outputs an application URL.
What it assumes
Builds on Terraform module calls, providers, input variables, and outputs rather than teaching HCL from zero.
What you can do after
Operate/configure it by choosing a root-module arrangement and setting environment-specific inputs and module versions.
How it connects
Connects application modules to root modules, input variables, outputs, providers, module registries, and the Terraform resource graph.

Also explained in

Compare how 35 books explain module →

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.