Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Modules and Dependency Management

What you'll learn

  • Break infrastructure into reusable modules with clear interfaces.
  • Pass variables between root modules and child modules.
  • Manage module versioning and the module registry workflow.
  • Use depends_on, for_each, and data sources to handle relationships.

Topics

Cheat sheet

  • Call a module:
    module "network" {
      source = "./modules/network"
      cidr_block = "10.0.0.0/16"
    }
  • Pin module versions: version = "~> 2.0"
  • Explicit dependency: depends_on = [module.network]

Official documentation

Hands-on task

Create a simple module structure:

mkdir -p modules/random_pet
cat <<'HCL' > modules/random_pet/main.tf
resource "random_pet" "this" {
  prefix = var.prefix
}

output "name" {
  value = random_pet.this.id
}
HCL
cat <<'HCL' > modules/random_pet/variables.tf
variable "prefix" {
  type = string
}
HCL

Then consume it in main.tf and run terraform init && terraform apply to observe the generated pet name.