Terraform, an open-source infrastructure as code (IaC) tool developed by HashiCorp, has rapidly gained popularity due to its simplicity, versatility, and cloud provider agnosticism. At the heart of Terraform’s configurability are its variables. This article delves into the specifics of Terraform variables, including their use cases, configuration parameters, and practical examples.
Understanding Terraform Variables
Terraform variables serve to make the Terraform scripts more flexible and adaptable. They can be utilized to tailor resources and configurations based on the user’s requirements, making code more maintainable and re-usable. You can define Terraform variable in many ways, like: in configuration file, dedicated file, command line or in form of environment variables. Here we will discuss all types one by one.
- Definition within the Terraform Configuration Files
The most straightforward way to define a variable is within the Terraform configuration itself using the variable keyword. Here’s an example:
12345variable "region" {description = "AWS Region"type = stringdefault = "us-west-2"}Consider the case where you need to deploy an AWS instance. The instance type should be adjustable based on the requirements (e.g., t2.micro, t2.small, etc.). A Terraform variable can be used to achieve this dynamic configuration.
12345678910variable "instance_type" {description = "Type of AWS instance"type = stringdefault = "t2.micro"}resource "aws_instance" "example" {ami = "ami-abc123"instance_type = var.instance_type}Here, the instance_type variable can be modified to deploy different AWS instances based on requirements.
- Creating a Dedicated File
Variables can be placed into a file named `terraform.tfvars` or any file name ending with `.auto.tfvars`. When Terraform runs, it automatically includes variables from these files.
For example, to define a variable named “region”, edit a file `terraform.tfvars` or `anyname.auto.tfvars` in Terrafrom directory and add the following code:
1region = "us-west-2" - Using Environment Variables
Terraform can also read the system’s environment variables in the form of TF_VAR_name to define variables. You can see that variable name must be prefixed with “TF_VAR_” followed by the original variable name like “region”. For example, setting TF_VAR_region in the environment will set the region variable.
1export TF_VAR_region="us-east-1" - From the Command Line:
You can also define variables directly from the command line with the -var option. For example, to define a region variable during the command `terraform apply`:
1terraform apply -var 'region=us-east-2'. - From Variable Definition Files at the Command Line
You can specify a different `.tfvars` file at the command line with the `-var-file` flag, which can be useful for having different variable files for different environments or situations.
1terraform apply -var-file="prod.tfvars"
Terraform Variables Precedence
Remember that Terraform loads variables in the following order, with later sources taking precedence over earlier ones:
- Environment variables
- The terraform.tfvars file, if present.
- The terraform.tfvars.json file, if present.
- Any *.auto.tfvars or *.auto.tfvars.json files, processed in lexical order of their filenames.
- Any -var and -var-file options on the command line, in the order they are provided.
This order of precedence allows you to create flexible configurations that adapt to different environments and use cases, such as using sensible defaults in the configuration file but overriding them for specific environments when running Terraform.
Conclusion
Terraform variables provide the necessary flexibility and adaptability required to write reusable and maintainable IaC scripts. By understanding and effectively using variables, you can manage complex, multi-environment infrastructures efficiently. Although the use cases discussed in this article are relatively simple, they form the basis for more complex and intricate real-world applications. The possibilities with Terraform variables are nearly endless, and mastering them is crucial for anyone seeking to tap into the full potential of Terraform.