Terraform Variables & Outputs
0 194
๐ฑ Introduction to Terraform Variables & Outputs
In Terraform, variables and outputs are the foundation for building reusable, scalable, and modular infrastructure. Variables allow you to parameterize your code and make it dynamic, while outputs let you extract and share important information after the infrastructure is provisioned. In this blog, we'll break down how to use both effectively.
๐ฆ Why Use Variables?
Variables are useful when you want to avoid hardcoding values like region names, instance sizes, or environment-specific data. They enhance code reusability and allow you to manage changes with minimal effort.
๐ง Defining Variables
Variables can be defined in a separate variables.tf
file using the variable
block. You can specify a default value or leave it open for user input.
variable "region" {
description = "The AWS region to deploy into"
type = string
default = "us-west-2"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
}
๐ Providing Variable Values
You can pass variable values in multiple ways:
- Using the
-var
flag - Through a
terraform.tfvars
file - Environment variables
terraform apply -var="instance_type=t2.micro"
Or use a terraform.tfvars
file:
region = "us-east-1"
instance_type = "t3.medium"
๐งฎ Variable Types
Terraform supports various types including string
, number
, bool
, list
, map
, and even custom object types.
variable "tags" {
type = map(string)
default = {
Environment = "dev"
Owner = "admin"
}
}
๐ Using Locals for Internal Values
locals
allow you to define internal variables within a module, useful for derived or computed values.
locals {
full_name = "${var.project_name}-${var.environment}"
}
๐ Introduction to Outputs
Outputs let you display useful information after Terraform has applied your configuration. They are commonly used to extract values like public IPs, resource IDs, or module exports.
๐ค Declaring Output Values
Use the output
block in your Terraform code to expose values.
output "instance_public_ip" {
description = "Public IP of the deployed EC2 instance"
value = aws_instance.my_ec2.public_ip
}
๐ Sharing Data Between Modules
Outputs are key for passing values between modules. One moduleโs output can serve as the input for another.
// In module A
output "vpc_id" {
value = aws_vpc.main.id
}
// In main.tf
module "vpc" {
source = "./modules/vpc"
}
module "ec2" {
source = "./modules/ec2"
vpc_id = module.vpc.vpc_id
}
๐ Naming Conventions & Documentation
Use clear, descriptive names for both variables and outputs. Document them using the description
argument so collaborators understand their purpose without guessing.
๐งช Best Practices
- Group your variables in logical files like
variables.tf
. - Use
terraform.tfvars
to manage environment-specific values. - Secure sensitive variables using
sensitive = true
to prevent them from showing in logs. - Validate input using
validation
blocks in Terraform 0.13+.
variable "environment" {
type = string
description = "Deployment environment"
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "Environment must be one of dev, staging, or prod."
}
}
โ Conclusion
Mastering Terraform Variables & Outputs is essential for writing clean, efficient, and maintainable IaC. By abstracting configuration values and exposing outputs smartly, you enable better reuse, automation, and team collaboration. Whether you're building a small project or managing enterprise-scale infrastructure, these concepts are fundamental to success.
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments