Learn how to efficiently pass variables to your Terraform modules using both the command line interface (CLI) and tfvars files for flexible and reusable infrastructure deployments.
This article explains how to pass variables to Terraform modules using five different methods: .tfvars files, command-line arguments, environment variables, defining variables within modules, and passing variables from parent to child modules. Each method is explained with examples and code snippets. The article also highlights the order of precedence Terraform uses when resolving variable values.
To pass variables to Terraform modules, you have several options:
1. Using .tfvars
files:
.tfvars
extension (e.g., variables.tfvars
).variable_name = value
. For example:region = "us-west-2"
instance_type = "t2.micro"
-var-file
flag:terraform plan -var-file=variables.tfvars
2. Passing variables directly from the command line:
-var
flag followed by the variable name and value:terraform apply -var 'region=us-east-1' -var 'instance_type=t3.medium'
3. Using environment variables:
TF_VAR_
and set them in your environment:export TF_VAR_region="eu-central-1"
export TF_VAR_instance_type="m5.large"
terraform apply
4. Defining variables within your module:
.tf
file, use the variable
block to declare variables:variable "region" {
type = string
}
variable "instance_type" {
type = string
default = "t2.micro"
}
var.<variable_name>
syntax.5. Passing variables from a parent module to a child module:
module
block.module
block:module "child_module" {
source = "./path/to/child/module"
region = var.region
instance_type = var.instance_type
}
Note: Terraform prioritizes variable values in the following order (highest to lowest):
-var
and -var-file
)TF_VAR_
)Remember to choose the method that best suits your needs and maintain consistency throughout your project.
This Terraform code defines a root module and a child module "ec2" for creating an EC2 instance on AWS. It demonstrates variable passing from the root module to the child module, including the AWS region and instance type. The code also shows how to define default values for variables and use different methods for providing variable values, such as using a .tfvars file or environment variables. The child module uses the provided variables to create an EC2 instance with the specified type and tags in the defined region.
1. variables.tf
(in your root module):
variable "region" {
type = string
description = "The AWS region to deploy resources to."
}
variable "instance_type" {
type = string
description = "The type of EC2 instance to create."
default = "t2.micro"
}
2. main.tf
(in your root module):
# Configure the AWS Provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = var.region
}
# Define a module for creating an EC2 instance
module "ec2_instance" {
source = "./modules/ec2"
# Pass variables to the child module
region = var.region
instance_type = var.instance_type
}
3. variables.tf
(in modules/ec2
):
variable "region" {
type = string
description = "The AWS region to deploy resources to."
}
variable "instance_type" {
type = string
description = "The type of EC2 instance to create."
}
4. main.tf
(in modules/ec2
):
resource "aws_instance" "example" {
ami = data.aws_ami.amazon_linux_2.id
instance_type = var.instance_type
tags = {
Name = "Example EC2 Instance"
}
}
data "aws_ami" "amazon_linux_2" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
owners = ["amazon"]
}
How to use:
.tfvars
file: Create a file named terraform.tfvars
in the same directory as your root module's main.tf
and add the following:region = "us-west-2"
instance_type = "t3.medium"
terraform apply
export TF_VAR_region="us-east-1"
export TF_VAR_instance_type="t2.small"
Then run terraform apply
.
This example demonstrates how to pass variables from a parent module to a child module, define default values for variables, and use different methods for providing variable values. You can adapt this example to your specific needs and use the method that best suits your workflow.
General Best Practices:
description
attribute within the variable
block to explain the purpose of each variable.validation
block within the variable
block to enforce constraints on variable values (e.g., string length, allowed values).Specific to Methods:
.tfvars
files:
Additional Considerations:
By understanding the different methods for passing variables to Terraform modules and following best practices, you can write more organized, reusable, and maintainable infrastructure code.
Method | Description | Example | Priority |
---|---|---|---|
.tfvars files |
Define variables in a separate file with .tfvars extension. |
variables.tfvars : region = "us-west-2" instance_type = "t2.micro" Usage: terraform plan -var-file=variables.tfvars
|
1 (highest) |
Command-line arguments | Pass variables directly when running Terraform commands. | terraform apply -var 'region=us-east-1' -var 'instance_type=t3.medium' |
1 (highest) |
Environment variables | Set environment variables prefixed with TF_VAR_ . |
export TF_VAR_region="eu-central-1" export TF_VAR_instance_type="m5.large" terraform apply
|
2 |
Module definition | Declare variables within the module's .tf file. |
variable "region" { type = string } variable "instance_type" { type = string, default = "t2.micro" }
|
3 |
Parent-child module passing | Pass variables as arguments within the module block in the parent module. |
module "child_module" { source = "./path/to/child/module" region = var.region instance_type = var.instance_type }
|
3 |
Default values | Define default values for variables within the module definition. | variable "instance_type" { type = string, default = "t2.micro" } |
4 (lowest) |
Passing variables to Terraform modules is crucial for creating flexible and reusable infrastructure code. This article explored five methods: using .tfvars
files, command-line arguments, environment variables, defining variables within modules, and passing variables from parent to child modules. Each method offers unique advantages and suits different scenarios. Understanding the order of precedence Terraform uses when resolving variable values is essential for predictable outcomes. By choosing the appropriate method and following best practices, you can write well-organized, adaptable, and maintainable Terraform code for robust infrastructure management.