Passing values to modules in Terraform is a fundamental concept that enables code reusability and flexibility. When you create a module, you define input variables that act as parameters, allowing the module to be customized for different use cases.
To pass values to a module, you use the module b…Passing values to modules in Terraform is a fundamental concept that enables code reusability and flexibility. When you create a module, you define input variables that act as parameters, allowing the module to be customized for different use cases.
To pass values to a module, you use the module block in your root configuration. Within this block, you specify arguments that correspond to the input variables defined in the module. These arguments can be literal values, references to resources, data sources, local values, or outputs from other modules.
Here is a basic example:
module "vpc" {
source = "./modules/vpc"
vpc_cidr = "10.0.0.0/16"
environment = var.environment
tags = local.common_tags
}
In this example, three values are passed to the vpc module: a hardcoded CIDR block, a variable reference, and a local value reference.
Modules can have required and optional input variables. Required variables must be provided when calling the module, while optional variables have default values defined in the module's variables.tf file. If you do not supply a value for an optional variable, Terraform uses the default.
You can also pass complex data types such as lists, maps, and objects to modules. This allows for sophisticated configurations:
module "subnets" {
source = "./modules/subnets"
subnet_config = {
public = ["10.0.1.0/24", "10.0.2.0/24"]
private = ["10.0.3.0/24", "10.0.4.0/24"]
}
}
Best practices include using descriptive variable names, providing descriptions for each input variable, and validating inputs using validation blocks. This approach ensures modules are self-documenting and helps prevent configuration errors.
Understanding how to effectively pass values to modules is essential for building maintainable, scalable Terraform configurations and is a key topic for the Terraform Associate certification exam.
Passing Values to Modules in Terraform
Why Passing Values to Modules is Important
Passing values to modules is a fundamental concept in Terraform that enables code reusability and flexibility. Modules become powerful building blocks when they can accept different configurations for different environments or use cases. This practice allows teams to create standardized infrastructure components that can be customized through input variables, reducing code duplication and maintaining consistency across deployments.
What is Passing Values to Modules?
When you call a module in Terraform, you can pass values to it through input variables. These values are defined as arguments in the module block and correspond to variables declared within the module itself. Think of it like calling a function with parameters - the module defines what inputs it expects, and the calling configuration provides the actual values.
How It Works
1. Module Declaration with Variables: Inside the module, you define variables using the variable block:
variable "instance_type" { type = string default = "t2.micro"}
2. Calling the Module: In your root configuration, you pass values when calling the module:
- Input variables are the only way to pass values into a module - Variables can have default values, making them optional - Required variables (those lacking defaults) must be provided when calling the module - Variable types include string, number, bool, list, map, and object - Sensitive variables can be marked to prevent their values from appearing in logs
Exam Tips: Answering Questions on Passing Values to Modules
Tip 1: Remember that module arguments correspond to input variables declared in the child module. If a question shows a module block with arguments, look for matching variable declarations.
Tip 2: Pay attention to variable types. If a module expects a list but receives a string, Terraform will produce an error. Questions often test type mismatches.
Tip 3: Know the difference between required and optional variables. Variables with default values are optional; those lacking defaults are required and must be specified in the module call.
Tip 4: Understand that you cannot reference resources from a calling module inside a child module - values must be explicitly passed through input variables.
Tip 5: Watch for questions about variable validation. Terraform allows custom validation rules within variable blocks to enforce constraints on passed values.
Tip 6: Be familiar with passing complex types like maps and objects. Questions may test your understanding of how to structure these values when calling modules.
Tip 7: Remember that locals and data sources can be used to compute values before passing them to modules, but the actual passing mechanism always uses the module block arguments.