π Terraform Series β Day 7

Variables & Outputs (Make Your Code Smart π₯)
In real-world DevOps, writing flexible and reusable code is very important.
Today, we will learn how to use Variables and Outputs in Terraform to make our infrastructure clean, dynamic, and production-ready.
Till now, we were writing Terraform codeβ¦
but there was one problem π
π Everything was hardcoded
And in real DevOps, hardcoding = BIG mistake β
So today we fix that π‘
π§© Step 1: The Real Problem
Imagine this:
instance_type = "t2.micro"
Looks simpleβ¦ but π
β Want to upgrade instance? Change everywhere
β Want reuse? Not possible
β Working in team? Becomes messy
π Basically: Not scalable
π‘ Step 2: The Smart Solution β Variables
Instead of fixing values in code, we use variables
Think like this:
π βKeep values separate, keep code cleanβ
β Why Variables?
Change once β Apply everywhere
Clean & readable code
Reusable infrastructure
Industry-level practice
π In short:
Variables = Flexibility + Clean Code + DevOps Standard
π Step 3: Create variables.tf
variable "ec2_instance_type" {
default = "t2.micro"
type = string
}
variable "ec2_root_storage_size" {
default = 10
type = number
}
variable "ec2_ami_id" {
default = "ami-0cb91c7de36eed2cb"
type = string
}
π§ Simple Understanding:
variableβ variable namedefaultβ default valuetypeβ data type
π Values are now separated from the main code β
π Step 4: Use Variables in ec2.tf
Now the real magic π₯
# Create Key Pair
resource "aws_key_pair" "my_key" {
key_name = "terra-key-aws"
public_key = file("terra-key-aws.pub")
}
# Default VPC
resource "aws_default_vpc" "default" {}
# Security Group
resource "aws_security_group" "my_groups" {
name = "my-group"
description = "Security group for EC2"
vpc_id = aws_default_vpc.default.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow SSH"
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow HTTP"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow all outbound"
}
tags = {
Name = "automate-sg"
}
}
# EC2 Instance
resource "aws_instance" "my_instance" {
ami = var.ec2_ami_id
instance_type = var.ec2_instance_type
key_name = aws_key_pair.my_key.key_name
vpc_security_group_ids = [aws_security_group.my_groups.id]
root_block_device {
volume_size = var.ec2_root_storage_size
volume_type = "gp3"
}
tags = {
Name = "terra-ec2"
}
}
β‘ Important Line:
var.<variable_name>
π Example:
var.ec2_instance_type
π Step 5: Real Power of Variables
Before:
instance_type = "t2.micro"
After:
default = "t3.micro"
β Change in one place
β Applied everywhere automatically
π₯ Thatβs the power
π€ Step 6: Now Letβs Talk About Outputs
Deployment is doneβ¦ but now π
π How do you get EC2 Public IP?
π Problem
β You have to manually check AWS Console
π‘ Solution β Outputs
Terraform will show it directly π₯
π Step 7: Create outputs.tf
output "ec2_public_ip" {
value = aws_instance.my_instance.public_ip
}
output "ec2_public_dns" {
value = aws_instance.my_instance.public_dns
}
π§ What Happens Now?
When you run:
terraform apply
π At the end, Terraform shows:
β Public IP
β Public DNS
Directly in terminal π―
π¨βπ» About the Author
βA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.β
π¬ Let's Stay Connected
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar





