Let’s Make Terraform Flexible, Infra Coders!
Hey there, Infra coders! You’ve already got Terraform up and running, created your first resources, managed state like pros, and even built reusable modules. Now, it’s time to make your Terraform code even more powerful with variables and outputs. Think of variables as the ingredients you can swap out in a recipe and outputs as the final dish you present to the world. These features let you write flexible, reusable code that adapts to different needs without rewriting everything.
In this article, we’ll explore what variables and outputs are, how to use them, and some best practices to keep your Terraform projects clean and adaptable. Let’s dive in!

What Are Variables and Outputs?
Variables let you customize your Terraform code without hardcoding values. Instead of writing a fixed bucket name like my-unique-bucket-123
in your code, you use a variable to make it changeable. This is super handy when you want to use the same code for different environments (like dev or prod) or share it with others.
Outputs, on the other hand, are like the results you get after Terraform runs. They display useful information about your resources, like the ID of an S3 bucket or the IP address of a server. Outputs make it easy to share details with other parts of your project or your team.
Why Use Variables and Outputs?
Here’s why variables and outputs are a big deal:
- Flexibility: Variables let you change settings (like names or regions) without editing the core code.
- Reusability: Use the same code for different projects or environments by swapping variable values.
- Clarity: Outputs make it easy to see important details about your resources, like URLs or IDs.
- Automation: Outputs can feed into other tools or scripts, making your workflows smoother.
- Team-Friendly: Variables make modules easier to share, as teammates can customize them without touching the logic.
Using Variables in Terraform
Variables are defined in a file (usually variables.tf
) and used in your configuration. Let’s create a simple setup to create an AWS S3 bucket with variables for flexibility.
Step 1: Define Variables
Create a file called variables.tf
in your project folder and add:
variable "bucket_name" {
description = "Name of the S3 bucket"
type = string
default = "my-default-bucket"
}
variable "region" {
description = "AWS region for the bucket"
type = string
default = "us-east-1"
}
variable "environment" {
description = "Environment (e.g., dev, prod)"
type = string
}
Here’s what’s going on:
description
: Explains what the variable is for (great for teammates).type
: Sets the data type (likestring
,number
, orlist
).default
: Provides a fallback value if you don’t specify one (optional).
Step 2: Use Variables in Your Configuration
In your main.tf
, use these variables:
provider "aws" {
region = var.region
}
resource "aws_s3_bucket" "my_bucket" {
bucket = var.bucket_name
tags = {
Environment = var.environment
}
}
Notice the var.bucket_name
syntax? That’s how you reference variables. This code uses your variables to set the region, bucket name, and environment tag.
Step 3: Set Variable Values
You can provide variable values in several ways:
- Command Line: Pass values when running Terraform:
terraform apply -var="bucket_name=my-unique-bucket-123" -var="region=us-west-2" -var="environment=prod"
- Variables File: Create a file called
terraform.tfvars
:bucket_name = "my-unique-bucket-123" region = "us-west-2" environment = "prod"
Terraform automatically loads this file.
- Environment Variables: Set variables with
TF_VAR_
prefix:export TF_VAR_bucket_name="my-unique-bucket-123" export TF_VAR_region="us-west-2" export TF_VAR_environment="prod"
For now, let’s use a terraform.tfvars
file for simplicity.
Using Outputs in Terraform
Outputs let you display or share information about your resources. Let’s add an output to show the S3 bucket’s ID.
Step 4: Define Outputs
Create a file called outputs.tf
and add:
output "bucket_id" {
description = "The ID of the S3 bucket"
value = aws_s3_bucket.my_bucket.id
}
output "bucket_arn" {
description = "The ARN of the S3 bucket"
value = aws_s3_bucket.my_bucket.arn
}
These outputs will show the bucket’s ID and ARN (Amazon Resource Name) after Terraform runs.
Step 5: Running Your Configuration
Let’s put it all together. Your project folder should look like this:
my-project/
├── main.tf
├── variables.tf
├── outputs.tf
├── terraform.tfvars
Run these commands in your project folder:
- Initialize: Sets up the AWS provider:
terraform init
- Plan: Checks what Terraform will do:
terraform plan
- Apply: Creates the bucket and shows outputs:
terraform apply
Type
yes
to confirm. After applying, you’ll see the bucket ID and ARN printed in the terminal.

Using Variables and Outputs in Modules
Variables and outputs shine when used with modules. Remember the S3 module from our last article? It already used variables and outputs! You can call that module and pass variables like this:
module "my_s3_bucket" {
source = "./s3-module"
bucket_name = var.bucket_name
environment = var.environment
}
output "module_bucket_id" {
value = module.my_s3_bucket.bucket_id
}
This makes your module reusable across projects with different bucket names or environments.
Best Practices for Variables and Outputs
To keep your Terraform code clean and effective, follow these tips:
- Use Descriptive Names: Name variables and outputs clearly (e.g.,
bucket_name
instead ofname
). - Add Descriptions: Always include
description
fields to explain what variables and outputs do. - Set Types: Use
type
in variables to catch errors (e.g.,type = string
). - Use Defaults Sparingly: Only set defaults for optional variables to avoid surprises.
- Protect Sensitive Outputs: If an output contains secrets (like passwords), mark it with
sensitive = true
. - Organize Variables: Group related variables in
variables.tf
and useterraform.tfvars
for project-specific values.
Cleaning Up
To remove the S3 bucket, run:
terraform destroy
Type yes
to confirm, and Terraform will delete the bucket to keep your AWS account tidy.
What’s Next?
Awesome work, Infra coders! You’ve learned how to use variables and outputs to make your Terraform code flexible and reusable. In the next article, we’ll explore Terraform workspaces to manage multiple environments like dev and prod with ease. Keep coding, and I’ll see you soon!
The post Variables and Outputs: Enhancing Terraform Flexibility appeared first on TecAdmin.
Source: Read More