Learn how to effectively pass variables from a Terraform root module to a child module for streamlined infrastructure provisioning and increased code reusability.
In Terraform, modules promote code reusability and organization. To leverage this effectively, you often need to pass data, such as variables, from a root module to a child module. This allows for customization and dynamic configuration of the child module's behavior. This article outlines the steps to pass variables from a root module to a child module in Terraform, enabling seamless data flow and modularity in your infrastructure code.
To pass variables from a root module to a child module in Terraform, follow these steps:
Define the variable in the child module:
variables.tf file, declare the variable using the variable block.Pass the variable value from the root module:
main.tf or a similar file, locate the module block where you call the child module.module block, assign a value to the child module's variable using the variable's name as the key.Example:
# child-module/variables.tf
variable "instance_name" {
type = string
description = "Name of the instance"
}# root-module/main.tf
module "child" {
source = "../child-module"
instance_name = "my-instance"
}Additional Notes:
.tfvars files to define variable values externally and pass them to both root and child modules.This code demonstrates passing variables from a root Terraform module to child modules. It defines a network module with variables for VPC and subnet CIDR blocks, and a server module with variables for instance name and subnet ID. The root module calls both child modules, passing values for their respective variables. The server module receives the subnet ID from an output of the network module, illustrating how to use outputs from one module as input for another.
This example demonstrates passing variables from a root module to a child module in Terraform.
Project Structure:
terraform-modules/
├── network/
│ └── variables.tf
└── server/
├── variables.tf
└── main.tf
1. Child Module (network/variables.tf):
variable "vpc_cidr_block" {
type = string
description = "The CIDR block for the VPC"
}
variable "subnet_cidr_block" {
type = string
description = "The CIDR block for the subnet"
}2. Child Module (server/variables.tf):
variable "instance_name" {
type = string
description = "Name of the EC2 instance"
}
variable "subnet_id" {
type = string
description = "ID of the subnet to deploy the instance in"
}3. Root Module (server/main.tf):
module "network" {
source = "../network"
vpc_cidr_block = "10.0.0.0/16"
subnet_cidr_block = "10.0.1.0/24"
}
module "server" {
source = "./"
instance_name = "my-instance"
subnet_id = module.network.subnet_id # Accessing output from network module
}Explanation:
network module and passes values for vpc_cidr_block and subnet_cidr_block.server module (itself in this case) and passes values for instance_name and subnet_id.subnet_id value is retrieved from the output of the network module using module.network.subnet_id.This example demonstrates how to define variables in child modules and pass values to them from the root module. It also shows how to access outputs from other modules and use them as input for other modules.
Understanding Variable Scope: Variables in Terraform have a specific scope. A variable defined in a child module is only accessible within that child module unless explicitly passed down from a parent module.
Output Values as Inputs: You can use the output values of one module as input variables for another. This is illustrated in the example where module.network.subnet_id fetches the subnet ID output from the network module and passes it to the server module.
Data Encapsulation: Modules help encapsulate data and logic. By passing variables, you control the information flow between modules, promoting better organization and reducing unintended side effects.
Reusability and Consistency: Modules, combined with variable passing, encourage code reusability. You can define a module once (e.g., for creating a network configuration) and reuse it across different parts of your infrastructure, customizing its behavior through variables.
Terraform State: Remember that Terraform manages the state of your infrastructure. When you change variable values passed to modules, Terraform will detect these changes and apply the necessary updates during the next terraform apply.
| Step | Description | Example |
|---|---|---|
| 1. Define Variable in Child Module | - Declare the variable in the child module's variables.tf file using the variable block. - Specify the variable's name, and optionally its type, description, and default value. |
terraform <br> # child-module/variables.tf <br> variable "instance_name" { <br> type = string <br> description = "Name of the instance" <br> } <br> |
| 2. Pass Variable Value from Root Module | - Locate the module block in your root module's main.tf (or similar) where you call the child module. - Inside the module block, assign a value to the child module's variable using its name as the key. - You can pass values directly, reference other variables, or use expressions. |
terraform <br> # root-module/main.tf <br> module "child" { <br> source = "../child-module" <br> instance_name = "my-instance" <br> } <br> |
Key Points:
.tfvars files to manage variable values externally.Passing variables from root to child modules is fundamental for creating modular and reusable Terraform code. By defining variables in child modules and passing values from the root module, you can customize the behavior of your infrastructure deployments. This approach promotes code organization, maintainability, and consistency, ultimately leading to more efficient and scalable infrastructure management. Understanding variable scope, leveraging output values as inputs, and adhering to Terraform's best practices for module composition are essential for harnessing the full potential of modular design in your infrastructure as code.
Accessing variables declared and defined in parent from child module | Hi, I am quite new in using Terraform, please bear with me if my questions are silly. My code structure is as follows: Root:----------- main.tf variables.tf main_values.tfvars nva-------(/* child module folder */) create_firewall.tf variables.tf firewall_values.tfvars I have tried executing just the child module as a separate standalone one, it worked successfully. As a next step I wanted to try out module configuration. Root: ---- consists of only creation of vnet and subnets. Chi...
Passing variables to child module with Terraform - Rico - Medium | Today, I got some problem when tried to passing values to module, so i wrote this article to keep track things. Then if you got same…
Input Variables - Configuration Language | Terraform | HashiCorp ... | ... values using CLI options and environment variables. When you declare them in child modules, the calling module should pass values in the module block. If ...
Pass variables values inside terraform modules - GeralexGR | In this article I will explain two different ways to pass variables values inside terraform modules. Modules let you separate your code into small units and help the engineer structure its project better. https://www.terraform.io/language/modules/syntax Modules in terraform (terraform files) can be placed on folders and their location should be provided on the module directive. Lets […]
Variable precedence and overrides between parent and child tfvars ... | In my experience it would appear that variables with the same name/key defined in both a parent and child tfvars file will always use the parent’s definition as the override value. This strikes me as odd. Generally I would envision the parent var being of a more generic nature and the child var being able to override that. Consider this example… live ├── test │ ├── thing1 │ │ └── terraform.tfvars │ └── terraform.tfvars If I have foo = "chinchilla" defined in live/test/terraform.tfva...