![]() |
VOOZH | about |
Terraform variables give developers the idea of creating templates that are reusable throughout different environments with minimal duplication of code and greater flexibility of the code. Rather, if you manage infrastructure through variables rather than hardcoding specific values into your scripts, the usage of variables allows you to simplify the management of your infrastructure.
As with other languages, variables in the terraform have specific uses in the master plan. The best practices for using them and their types will be covered in this guide, along with recommendations that will assist in writing scalable and well-maintained infrastructure code.
Instead of hardcoding specific details like instance types or region names, Terraform variables act as placeholders that store dynamic values. This makes your code reusable and flexible. For instance, you can use the same configuration file for different environments by simply changing variable values instead of rewriting the whole script.
There are several variable types in Terraform that you can use depending on your specific needs. Below are some of the most important ones:
To start using variables in Terraform, you first need to declare them in your configuration file. Here’s an example of how to declare a string variable:
variable "instance_type" {
type = string
default = "t2.micro"
}
In this case, the variable instance_type is declared with a default value of t2.micro. The type of the variable is defined as string.
There are several ways to assign values to variables in Terraform:
Example of passing a value via the -var flag:
terraform apply -var="instance_type=t2.large"This overrides the default value for instance_type and sets it to t2.large.
Once a variable is declared and assigned a value, you can use it throughout your Terraform configurations. Here’s an example of how to use a variable in a resource block:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
}
In this case, the instance_type for the aws_instance resource is dynamically set using the value of the instance_type variable.
Terraform allows you to validate variables before they are used, ensuring they meet certain conditions. You can add validation rules to your variables to enforce rules like acceptable value ranges or formats.
Example:
variable "instance_type" {
type = string
default = "t2.micro"
validation {
condition = contains(["t2.micro", "t2.small", "t2.medium"], var.instance_type)
error_message = "Instance type must be one of t2.micro, t2.small, or t2.medium."
}
}
This validation ensures that only allowed instance types can be used.
For better organization, especially in multi-environment setups, you can define variables in .tfvars files instead of hardcoding them into the configuration. This makes it easier to manage values for different environments (e.g., development, production).
Example .tfvars file (terraform.tfvars)
instance_type = "t2.medium"
ami_id = "ami-0c55b159cbfafe1f0"
You can then specify this file when running Terraform commands:
terraform apply -var-file="terraform.tfvars"To ensure your Terraform configurations are maintainable and scalable, here are some best practices for using variables: