The module block syntax in Terraform is a fundamental concept that allows you to organize and reuse infrastructure code effectively. A module block is used to call and instantiate a child module from your root module or another parent module.
The basic syntax of a module block consists of the foll…The module block syntax in Terraform is a fundamental concept that allows you to organize and reuse infrastructure code effectively. A module block is used to call and instantiate a child module from your root module or another parent module.
The basic syntax of a module block consists of the following structure:
module "module_name" {
source = "path/to/module"
# Input variables
variable1 = value1
variable2 = value2
}
Key components of the module block syntax include:
1. **Module Keyword**: Every module block starts with the 'module' keyword, indicating you are referencing external or local module code.
2. **Module Name**: The label following the module keyword (in quotes) serves as a unique identifier within your configuration. This name is used to reference the module's outputs elsewhere in your code.
3. **Source Argument**: The source argument is mandatory and specifies where Terraform should find the module code. Sources can be local paths, Terraform Registry, GitHub, S3 buckets, or other supported locations.
4. **Input Variables**: After the source, you pass values to the module's input variables. These arguments correspond to variables defined within the called module using variable blocks.
5. **Optional Meta-Arguments**: Module blocks support several meta-arguments including 'version' (for registry modules), 'providers' (to pass provider configurations), 'count' and 'for_each' (to create multiple instances), and 'depends_on' (to establish explicit dependencies).
Example with meta-arguments:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.0.0"
count = var.create_vpc ? 1 : 0
cidr = "10.0.0.0/16"
name = "my-vpc"
}
To access module outputs, use the syntax: module.module_name.output_name
Understanding module block syntax enables you to build modular, maintainable, and scalable Terraform configurations while promoting code reuse across your infrastructure projects.
Module Block Syntax in Terraform
Why Module Block Syntax is Important
Understanding module block syntax is fundamental to writing reusable, maintainable, and organized Terraform code. Modules allow you to encapsulate resources into reusable components, reducing code duplication and enabling consistent infrastructure patterns across your organization. For the Terraform Associate exam, this topic is essential as it tests your ability to properly call and configure modules.
What is a Module Block?
A module block in Terraform is used to call a child module from a parent module (typically your root module). It defines which module to use, where to find it, and what input values to pass to it. Every Terraform configuration has at least one module - the root module - which consists of the resources defined in the .tf files in your main working directory.
Basic Module Block Syntax
The structure of a module block looks like this:
module "module_name" { source = "path/to/module" version = "1.0.0" # Input variables variable_name = value }
Key Components of Module Blocks
1. module keyword: Declares you are calling a module
2. Label: A unique name for this module instance (e.g., "module_name")
3. source (Required): Specifies where to find the module code. This can be: - Local path: "./modules/vpc" - Terraform Registry: "hashicorp/consul/aws" - GitHub: "github.com/example/module" - S3 bucket: "s3::https://bucket.s3.amazonaws.com/module.zip"
4. version (Optional but recommended): Constrains which version of the module to use
5. Input Variables: Arguments that pass values into the module
Meta-Arguments for Modules
Module blocks support several meta-arguments:
- count: Create multiple instances of a module - for_each: Create instances based on a map or set - providers: Pass provider configurations to the module - depends_on: Explicitly declare dependencies
To reference outputs from a called module, use: module.module_name.output_name
Exam Tips: Answering Questions on Module Block Syntax
1. Remember that 'source' is the only required argument in a module block. Questions may try to trick you by suggesting 'version' or other arguments are mandatory.
2. Know the different source types: Be familiar with local paths, Terraform Registry format, Git URLs, and cloud storage sources. The exam often tests whether you can identify valid source strings.
3. Understand module versioning: Version constraints only work with modules from registries, not local paths or certain other sources.
4. Pay attention to the 'providers' argument: This is used when a module needs a provider configuration different from the default. Know the syntax for passing providers.
5. Distinguish between root and child modules: The root module calls child modules. Resources in the root module are defined alongside module blocks.
6. Remember module output syntax: module.NAME.OUTPUT is the pattern for accessing child module outputs.
7. Meta-arguments matter: Know that modules support count, for_each, providers, and depends_on, but NOT lifecycle blocks.
8. When in doubt about source paths: Local modules start with ./ or ../ to indicate relative paths.