Master Terraform Variables, Outputs, and Modules for Reusable Infrastructure
This tutorial explains Terraform's core concepts—variables, outputs, and modules—detailing their purposes, types, definition syntax, assignment methods, priority rules, and how to organize reusable infrastructure code with practical examples and step‑by‑step commands.
Variables: Parameterizing Terraform Configurations
Variables replace hard‑coded values, making configurations flexible and reusable.
Flexibility : Change values without editing code.
Reusability : Same configuration can produce different resources by supplying different variable values.
Reduced duplication : Common values are defined once.
Supported types:
string number bool list(...) map(...) object(...) tuple(...)Example variable definitions:
variable "instance_type" {
type = string
description = "The type of EC2 instance to launch"
default = "t2.micro"
}
variable "region" {
type = string
description = "The region to deploy to"
default = "ap-southeast-1"
}
variable "tags" {
type = map(string)
description = "Tags to apply to all resources"
default = {
Environment = "dev"
Project = "example"
}
}Key elements: variable – keyword.
Variable name (e.g., instance_type). type – optional, defaults to string. description – optional, explains purpose. default – optional, used when no external value is supplied.
Referencing a variable uses the var. prefix:
resource "aws_instance" "example" {
ami = "ami-0c55b31ad2299a796"
instance_type = var.instance_type
tags = var.tags
}Assigning values (order of precedence):
Command‑line: terraform apply -var="instance_type=t3.medium" Auto‑loading *.auto.tfvars files.
Explicit terraform.tfvars file.
Environment variables prefixed with TF_VAR_ (e.g., TF_VAR_instance_type=t3.medium).
Interactive input (if no other source provides a value).
Precedence: command‑line > *.auto.tfvars > terraform.tfvars > environment variables > default.
Outputs: Exposing Resource Attributes
Outputs make resource attributes visible after a run and allow passing data between modules.
output "instance_public_ip" {
value = aws_instance.example.public_ip
description = "The public IP address of the EC2 instance"
}
output "instance_id" {
value = aws_instance.example.id
description = "ID of the EC2 instance"
}Key fields: output – keyword.
Output name (e.g., instance_public_ip). value – usually a resource attribute. description – optional, explains the output.
View outputs after terraform apply with terraform output or a specific name with terraform output instance_public_ip.
Modules: Organizing Reusable Code
Modules package Terraform configurations into independent, version‑controlled units.
Typical module file layout: main.tf – primary resources. variables.tf – input variable definitions. outputs.tf – output definitions.
Example EC2 module:
# modules/ec2/main.tf
resource "aws_instance" "example" {
ami = var.ami
instance_type = var.instance_type
tags = var.tags
}
# modules/ec2/variables.tf
variable "ami" { type = string }
variable "instance_type" { type = string }
variable "tags" { type = map(string); default = {} }
# modules/ec2/outputs.tf
output "instance_public_ip" { value = aws_instance.example.public_ip }
output "instance_id" { value = aws_instance.example.id }Using the module in a root configuration:
module "ec2_instance" {
source = "./modules/ec2"
ami = "ami-0c55b31ad2299a796"
instance_type = "t3.medium"
tags = {
Name = "My EC2 Instance"
}
} module– keyword to reference a module.
Module instance name (e.g., ec2_instance). source – local path or remote URL.
Input variables ( ami, instance_type, tags).
Exercise: Refactor an EC2 Project into a Module
Steps to modularize an existing configuration:
Create a modules/ec2 directory.
Move the original main.tf into modules/ec2.
Add variables.tf and outputs.tf with the definitions shown above.
Replace the root configuration with a new main.tf that calls the module.
Resulting project structure:
.
├── main.tf # root configuration
└── modules
└── ec2
├── main.tf
├── variables.tf
└── outputs.tfRoot main.tf example:
provider "aws" {
region = "ap-southeast-1"
}
module "ec2_instance" {
source = "./modules/ec2"
ami = "ami-0c55b31ad2299a796"
instance_type = "t3.medium"
tags = {
Name = "My EC2 Instance"
}
}Typical workflow commands: terraform init – initialize the working directory and download providers. terraform plan -out=ftplan – preview changes and save the plan. terraform apply "ftplan" – apply the saved plan. terraform output – view defined outputs. terraform destroy – clean up resources.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
