In Terraform, variables are a crucial part that allows you to parameterize your infrastructure configuration, making your code more reusable and manageable. Below, I’ll explain what variables are in Terraform, how to define them, how to use them, and provide some practical examples.
What are Variables in Terraform?
Variables in Terraform allow code reuse by abstracting values that can change across different environments, such as credentials, resource names, instance sizes, etc. This makes infrastructure configuration and maintenance easier.
Defining Variables
To define variables in Terraform, you use the variables.tf
file. Here, you specify variables with their name, type, description, and default values (if any). The basic syntax is as follows:
variable "variable_name" {
type = data_type
description = "Description of the variable"
default = default_value
}
Common Data Types
string
: a text string.number
: a number.bool
: a boolean value.list
: a list of values.map
: a set of key-value pairs.
Example of Variable Definitions
variable "region" {
type = string
description = "The region where resources will be deployed"
default = "us-west-1"
}
variable "instance_type" {
type = string
description = "The instance type for the servers"
default = "t2.micro"
}
variable "allowed_ips" {
type = list(string)
description = "List of IPs allowed to access"
default = ["0.0.0.0/0"]
}
variable "tags" {
type = map(string)
description = "Tags for the resources"
default = {
Environment = "Dev"
Project = "TerraformProject"
}
}
Using Variables
To use variables within Terraform configuration files (*.tf
), you use the syntax ${var.variable_name}
. Here’s an example:
provider "aws" {
region = var.region
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
tags = var.tags
}
Assigning Values to Variables
There are several ways to assign values to variables in Terraform:
1. Default Values: If a variable has a default value, it will be used unless overridden.
2. terraform.tfvars
or *.auto.tfvars
File: You can create a terraform.tfvars
file to define specific values for your variables.
region = "us-east-1"
instance_type = "m4.large"
3. Environment Variables: Terraform can read environment variables that start with TF_VAR_
. For example, TF_VAR_region="us-east-1"
.
$ export TF_VAR_filename="/root/myfile.txt"
$ export TF_VAR_content="Mr Robot"
$ terraform apply
4. Command-Line Flags: You can pass variables directly on the command line when running Terraform:
terraform apply -var="region=us-east-1" -var="instance_type=m4.large"
5. Interactive Input: If a value is not provided, Terraform will prompt the user for input during execution.
Complete Example
Imagine you have a main.tf
file that defines an EC2 instance on AWS:
provider "aws" {
region = var.region
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
tags = var.tags
}
And a variables.tf
file where you define the necessary variables:
variable "region" {
type = string
description = "The region where resources will be deployed"
default = "us-west-1"
}
variable "instance_type" {
type = string
description = "The instance type for the servers"
default = "t2.micro"
}
variable "tags" {
type = map(string)
description = "Tags for the resources"
default = {
Environment = "Dev"
Project = "TerraformProject"
}
}
Finally, you can have a terraform.tfvars
file to override some values:
region = "us-east-1"
instance_type = "m4.large"
Summary
Variables in Terraform are essential for creating reusable and manageable configurations. By defining and using variables, you can parameterize your infrastructure, making it easier to manage and deploy across multiple environments. Use the variables.tf
file to define your variables and assign values through .tfvars
files, environment variables, command-line flags, or default values.