Module input variables are a fundamental concept in Terraform that enable you to create flexible, reusable, and configurable infrastructure modules. They serve as the primary mechanism for passing values into a module from the calling configuration.
Input variables are declared within a module usi…Module input variables are a fundamental concept in Terraform that enable you to create flexible, reusable, and configurable infrastructure modules. They serve as the primary mechanism for passing values into a module from the calling configuration.
Input variables are declared within a module using the 'variable' block. Each variable can include several optional arguments: 'description' provides documentation about the variable's purpose, 'type' specifies the expected data type (string, number, bool, list, map, object, etc.), 'default' sets a fallback value when no value is provided, and 'validation' allows you to define custom validation rules.
Example variable declaration:
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
When calling a module, you pass values to these input variables using arguments within the module block:
module "web_server" {
source = "./modules/ec2"
instance_type = "t3.medium"
}
Input variables promote the DRY (Don't Repeat Yourself) principle by allowing you to write generic modules that can be customized for different environments or use cases. For instance, a single EC2 module can deploy development, staging, and production instances by varying the input parameters.
Best practices include always providing descriptions for documentation, using appropriate type constraints to catch errors early, setting sensible defaults where applicable, and using sensitive = true for confidential values like passwords or API keys to prevent them from appearing in logs.
Variables can also be marked as required by omitting the default value, forcing users to provide a value when using the module. This ensures critical configuration parameters are explicitly defined.
Understanding module input variables is essential for the Terraform Associate exam, as they form the foundation of module composition and code reusability in Terraform configurations.
Module Input Variables in Terraform
What Are Module Input Variables?
Module input variables are parameters that allow you to customize and configure Terraform modules when you call them. They act as the interface between the module and the calling configuration, enabling you to pass values into modules to make them flexible and reusable across different environments and use cases.
Why Are Module Input Variables Important?
Module input variables are essential for several reasons:
• Reusability: They allow a single module to be used multiple times with different configurations • Flexibility: You can customize module behavior based on environment (dev, staging, production) • Abstraction: They hide complex implementation details while exposing only necessary configuration options • Maintainability: Changes to common infrastructure patterns only need to be made in one place • Standardization: Teams can enforce consistent infrastructure patterns while allowing customization
How Module Input Variables Work
Module input variables are defined using the variable block inside the module:
• description: Documents the purpose of the variable • type: Specifies the expected data type (string, number, bool, list, map, object) • default: Provides a fallback value if none is specified • validation: Adds custom validation rules • sensitive: Marks the variable as sensitive to prevent it from being displayed in logs
Variable Types:
• string - Text values • number - Numeric values • bool - True or false • list(type) - Ordered collection of values • map(type) - Collection of key-value pairs • object({...}) - Structured data with named attributes
Required vs Optional Variables
Variables with a default value are optional. Variables lacking a default value are required and must be provided when calling the module.
Exam Tips: Answering Questions on Module Input Variables
1. Remember the syntax: Module variables are passed as arguments in the module block, not through separate variable files for the module
2. Understand required variables: If a module variable has no default, you must provide a value when calling the module or Terraform will produce an error
3. Know the difference: Root module variables can be set via CLI, environment variables, or .tfvars files. Child module variables are set in the module block of the calling configuration
4. Type constraints matter: If a variable has a type constraint, the passed value must match that type
5. Variable precedence: When calling a module, explicitly passed values override any defaults defined in the module
6. Watch for sensitive variables: Variables marked as sensitive = true will have their values redacted in plan and apply output
7. Common exam scenarios: - Identifying why a module call fails (missing required variable) - Understanding how to pass complex types like lists or maps to modules - Knowing that variables in modules create an API contract for that module
8. Remember: You cannot use -var or -var-file to set variables for child modules. Those flags only work for root module variables