Variable definitions in Terraform allow you to parameterize your infrastructure configurations, making them reusable and flexible across different environments. Variables are declared using the 'variable' block in your Terraform configuration files, typically stored in a file named variables.tf.
A…Variable definitions in Terraform allow you to parameterize your infrastructure configurations, making them reusable and flexible across different environments. Variables are declared using the 'variable' block in your Terraform configuration files, typically stored in a file named variables.tf.
A basic variable definition includes the variable name and can optionally include a type constraint, default value, description, and validation rules. The syntax follows this pattern:
variable "instance_type" {
type = string
default = "t2.micro"
description = "The EC2 instance type to use"
}
Default values are crucial for providing fallback options when no explicit value is provided during execution. When a default is specified, the variable becomes optional. If no default exists, Terraform will prompt for the value during plan or apply operations, or you must supply it through other means.
Variables can be assigned values through multiple methods with the following precedence (lowest to highest): default values in variable blocks, environment variables (TF_VAR_name format), terraform.tfvars file, *.auto.tfvars files, and -var or -var-file command line options.
Terraform supports several variable types including primitive types (string, number, bool) and complex types (list, set, map, object, tuple). Type constraints help validate input and catch errors early in the development process.
Example with complex type:
variable "tags" {
type = map(string)
default = {
Environment = "development"
Project = "demo"
}
}
Sensitive variables can be marked with 'sensitive = true' to prevent their values from appearing in logs and console output. Validation blocks allow custom rules to ensure variable values meet specific requirements before Terraform proceeds with operations.
Proper use of variables and defaults enables teams to maintain consistent infrastructure code while adapting deployments to various scenarios and environments.
Variable Definitions and Defaults in Terraform
Why Variable Definitions and Defaults Matter
Variables are fundamental to creating reusable, flexible, and maintainable Terraform configurations. Understanding how to define variables and set defaults is essential for the Terraform Associate exam and real-world infrastructure management. Variables allow you to parameterize your configurations, making them adaptable across different environments.
What Are Variable Definitions?
In Terraform, input variables serve as parameters for your modules and configurations. They are defined using variable blocks within your Terraform files, typically in a file named variables.tf.
A basic variable definition looks like this:
variable "instance_type" { description = "The EC2 instance type" type = string default = "t2.micro"}
Key Components of Variable Definitions
1. description - Documents what the variable is used for (optional but recommended) 2. type - Specifies the data type (string, number, bool, list, map, set, object, tuple) 3. default - Provides a fallback value when no value is explicitly provided 4. validation - Custom validation rules for the variable 5. sensitive - Marks the variable as sensitive to hide values in output 6. nullable - Determines if the variable can be set to null
How Default Values Work
Default values make variables optional. When a default is specified:
- Users can override the default by providing their own value - If no value is provided, Terraform uses the default - Variables with no default value are required and must be provided
Variable Definition Precedence
Terraform loads variable values in this order (later sources override earlier ones):
1. Environment variables (TF_VAR_name) 2. terraform.tfvars file 3. terraform.tfvars.json file 4. *.auto.tfvars or *.auto.tfvars.json files (alphabetical order) 5. -var and -var-file options on the command line
Type Constraints
Terraform supports these type constraints:
- Primitive types: string, number, bool - Complex types: list(type), set(type), map(type), object({...}), tuple([...]) - any - accepts any type
Exam Tips: Answering Questions on Variable Definitions and Defaults
1. Remember the precedence order - Command line arguments (-var) have the highest priority, followed by auto.tfvars files, then terraform.tfvars, and finally environment variables have the lowest priority among explicit assignments.
2. Know when variables are required - A variable is required only when it has no default value defined. If a default exists, the variable becomes optional.
3. Understand type conversion - Terraform automatically converts between compatible types when possible. For example, the number 5 can be converted to the string "5".
4. File naming conventions matter - Files named exactly terraform.tfvars or ending in .auto.tfvars are loaded automatically. Other .tfvars files require the -var-file flag.
5. Environment variable format - Environment variables must be prefixed with TF_VAR_ followed by the variable name (e.g., TF_VAR_instance_type).
6. Sensitive variables - Setting sensitive = true prevents values from appearing in CLI output but does not encrypt them in state files.
7. Watch for null values - By default, if nullable = true (the default), passing null uses the default value. If nullable = false, null is not allowed.
8. Common exam scenarios - Questions often test whether you understand which value takes precedence when the same variable is set in multiple places.