variables.tf, you can also declare them in the same file as your resources (e.g., main.tf).
Hard-Coded Resource Definitions
Below is an example of defining resources using hard-coded values:Defining Variables in a Separate File
By defining variables in a separate file, you can avoid repetition and make your configuration easier to update. The following example demonstrates how to define corresponding variables invariables.tf:
Using Variables in Resource Configurations
By referencing variables in your resource configurations using thevar. prefix, you can replace hard-coded values. This makes it easier to change values without having to update each resource block. See the example below:
variables.tf as shown earlier.
When you run terraform apply, Terraform processes your configuration and recognizes that variable values do not need to be enclosed in double quotes in concatenated expressions. If you update a variable value (for example, changing the content variable or increasing the length from 1 to 2), Terraform will detect the change and replace the affected resources accordingly.
Below is an example output after updating variable values:
Using variables in Terraform not only makes your configurations more readable but also simplifies maintenance when scaling your infrastructure.
AWS Instance Example Using Variables
Consider an example where you create an AWS instance using variables for the AMI and instance type. In the resource definition below, the values forami and instance_type are referenced from variables:
variables.tf, you can override them when applying the configuration. There are a few methods to do so:
-
Remove the Default Values:
You can remove the defaults invariables.tfand provide values explicitly during runtime. -
Pass Values with Command-Line Flags:
Provide variable values using the-varflag during execution: -
Use Environment Variables:
Export the variable values before running Terraform: -
Variable Definition File:
Supply variable values via a variable definition file (ending with.tfvarsor.tfvars.json). By default, Terraform automatically loads files namedterraform.tfvarsorterraform.tfvars.json. For custom-named files, use the-var-fileflag:
Variable Definition Precedence
Terraform provides multiple methods for assigning variable values. When a variable is defined in more than one way, Terraform follows a specific precedence order:| Precedence Level | Description |
|---|---|
| 1. Environment Variables | Values set via environment variables (e.g., export TF_VAR_instance_type="t2.micro") |
| 2. terraform.tfvars File | Values provided in terraform.tfvars or terraform.tfvars.json |
| 3. Auto-loaded Variable Files | Files ending with .auto.tfvars or .auto.tfvars.json loaded in alphabetical order |
| 4. Command-Line Flags | Values passed using the -var or -var-file flags (highest precedence) |
type is specified through multiple methods as shown below:
t2.medium as the final value for type since command-line flags have the highest precedence.
Always be aware of the variable precedence in Terraform to avoid unexpected behaviors during deployment. Using dedicated variable files or environment variables can improve consistency across different environments.
That concludes our article on variables in Terraform. Understanding how to define, reference, and override variables is essential for creating flexible and reusable Terraform configurations. Happy building!