Explains Terraform modules including structure, usage, sources, versioning and benefits for building reusable, consistent, maintainable infrastructure
Welcome to the Introduction to Terraform Modules.This lesson explains how Terraform modules let you package related infrastructure-as-code into reusable, logical components. Organizing configurations into modules reduces repetition, enforces consistency across environments, and makes it easier to share well‑tested patterns across teams.We’ll cover core concepts including module structure, usage, sources, and versioning so you can build, consume, and maintain reusable Terraform modules. Let’s dive into why modules are essential for scalable, maintainable infrastructure.
A module is a container for related Terraform resources that are commonly used together — for example, the resources that make up a subnet, a VPC, or an application stack. Modules are the recommended way to package and reuse configurations.When you run Terraform, the configuration in the working directory is the root (or parent) module. Any other module called from the root module is a child (reusable) module. Using modules helps encapsulate complexity and standardize patterns.
The root module is the directory where you run Terraform commands. Child modules live in other directories, remote registries, or version control and are referenced via module blocks.
Using modules delivers several operational and organizational advantages:
Improved organization: Break large configurations into smaller, easier-to-reason units.
Easier collaboration: Publish vetted modules to a registry or shared repo so teams consume standardized building blocks.
Consistent patterns: Centralize naming, tagging, sizing, and security controls in modules to enforce best practices.
When platform or security teams review and approve modules, your organization can ensure deployments follow standards without each consumer needing to implement the same checks.
Instead of placing many resource blocks (TLS certificate, load balancer, message queue, Kubernetes cluster, database, etc.) into a single configuration, define each piece once as a module and reuse it across multiple root modules.For example:
A Marketing Application might reference the TLS and Load Balancer modules.
A GenAI service might reference those plus GPU-backed cluster and specialized queue modules.
Root modules declare only the modules they need and configure them via inputs. This makes code DRY (Don’t Repeat Yourself), easier to test, and simpler to maintain.
variable "domain_name" { type = string description = "Domain name for the TLS certificate"}variable "cert_tags" { type = map(string) description = "Tags to apply to the certificate" default = {}}
Minimal outputs.tf inside the module:
output "certificate_arn" { description = "The ARN of the TLS certificate" value = aws_acm_certificate.example.arn}
Modules can be sourced from several places: local paths, Git repositories, Terraform Registry, or other VCS. Always prefer versioned sources for reproducible builds when pulling remote modules.Examples of module sources
To reuse a module, declare a module block in your root module and set the source and any required inputs. The root module receives outputs from the child module as attributes.Example with multiple modules:
Modules are the primary mechanism in Terraform for packaging, sharing, and enforcing infrastructure patterns. Use them to:
Reduce duplication
Improve maintainability
Standardize security and operational controls
By organizing infrastructure as reusable modules and versioning them responsibly, teams gain scalability and governance while keeping root modules concise and readable.