Course contentsShow
← Cloud and Infrastructure
Lesson 801 of 2,98915. Terraform in PracticeFree lesson

Calling Modules with module Blocks

Using the module block syntax to instantiate and configure child modules in your code.

Calling Modules with module Blocks

What you'll learn: How to instantiate and configure child modules in your Terraform code using the module block syntax.

The Core Idea

Think of a module block as a function call in programming. Just as you'd call a function by name and pass it arguments, you call a Terraform module by referencing its location and providing the input variables it needs. The module block is your gateway to reusing infrastructure code—it tells Terraform "go fetch that module and create its resources with these specific settings."

Basic Syntax

The module block follows a simple pattern:

module "identifier" {
  source = "./path/to/module"
  
  # Input variables for the child module
  variable_name = value
}

The identifier is a unique name you choose (like "web_server" or "database"). The source argument tells Terraform where to find the module—this could be a local path, a Git repository, or a registry URL. Any additional arguments you provide become input variables passed to the child module.

Example Usage

module "vpc_network" {
  source = "./modules/networking"
  
  environment    = "production"
  cidr_block     = "10.0.0.0/16"
  enable_logging = true
}

Here, you're instantiating the networking module and configuring it with three input variables. Terraform will read that module's code, create all its resources, and make any outputs available to you.

Referencing Module Outputs

Access outputs from your module using this syntax:

resource "aws_instance" "app" {
  subnet_id = module.vpc_network.subnet_id
}

Key Takeaway: The module block lets you instantiate reusable infrastructure components by specifying their source location and providing required input variables, turning child modules into configurable building blocks for your infrastructure.