Input variables in Terraform are defined using variable blocks and serve as parameters that allow you to customize your Terraform configurations. They make your code reusable and flexible by enabling users to pass different values at runtime rather than hardcoding them.
A variable block is declare…Input variables in Terraform are defined using variable blocks and serve as parameters that allow you to customize your Terraform configurations. They make your code reusable and flexible by enabling users to pass different values at runtime rather than hardcoding them.
A variable block is declared using the 'variable' keyword followed by the variable name. The basic syntax is:
variable "instance_type" {
description = "The EC2 instance type"
type = string
default = "t2.micro"
}
Key attributes within variable blocks include:
1. **description**: A human-readable explanation of the variable's purpose. This is optional but highly recommended for documentation.
2. **type**: Specifies the data type constraint. Common types include string, number, bool, list, map, set, object, and tuple. Type constraints help catch errors early.
3. **default**: Provides a fallback value if no value is supplied. Variables with defaults are optional; those lacking defaults are required.
4. **sensitive**: When set to true, Terraform redacts the value from logs and console output, useful for secrets and passwords.
5. **validation**: Allows custom validation rules to ensure input values meet specific criteria.
Variables can be assigned values through multiple methods:
- Command line using -var flag
- Environment variables prefixed with TF_VAR_
- terraform.tfvars or *.auto.tfvars files
- -var-file flag pointing to a variable definitions file
Terraform processes these sources in a specific order, with later sources taking precedence over earlier ones.
To reference a variable in your configuration, use the var prefix: var.instance_type
Input variables are essential for creating modular, maintainable infrastructure code. They enable separation of configuration from implementation, allow the same codebase to deploy resources across different environments, and promote collaboration by making configurations more understandable and customizable.
Input Variables (Variable Blocks) in Terraform
Why Input Variables Are Important
Input variables are fundamental to creating reusable, flexible, and maintainable Terraform configurations. They allow you to parameterize your infrastructure code, enabling the same configuration to be used across different environments (development, staging, production) with different values. This eliminates hardcoding and promotes the DRY (Don't Repeat Yourself) principle.
What Are Input Variables?
Input variables in Terraform are declared using variable blocks. They serve as parameters for Terraform modules, allowing users to customize aspects of the configuration at runtime or through various input methods.
A basic variable block looks like this:
variable "instance_type" { description = "The EC2 instance type" type = string default = "t2.micro"}
Variable Block Arguments
• default - A default value making the variable optional • type - Specifies the type constraint (string, number, bool, list, map, set, object, tuple) • description - Documents the variable's purpose • validation - Custom validation rules • sensitive - Marks the variable as sensitive (hides value in output) • nullable - Specifies whether the variable can be null
How Input Variables Work
Variables are referenced in your configuration using the var prefix:
variable "instance_type" { type = string validation { condition = contains(["t2.micro", "t2.small"], var.instance_type) error_message = "Instance type must be t2.micro or t2.small." }}
Exam Tips: Answering Questions on Input Variables
• Remember the precedence order: Command line options (-var, -var-file) have the HIGHEST precedence, while default values have the LOWEST
• Know the file naming conventions: terraform.tfvars and *.auto.tfvars are automatically loaded; other .tfvars files require -var-file flag
• Environment variable format: Must use TF_VAR_ prefix followed by the variable name (e.g., TF_VAR_instance_type)
• Required vs Optional: Variables are required if they have no default value set
• Sensitive variables: Setting sensitive = true prevents values from appearing in CLI output but does NOT encrypt the state file
• Type constraints: If no type is specified, Terraform accepts any type
• Variable files: When multiple *.auto.tfvars files exist, they are processed in alphabetical order
• Common exam scenarios: Questions often test your understanding of which value takes precedence when the same variable is set through multiple methods
• Local values vs Input variables: Know the difference - locals are for internal computation, variables are for external input