In the previous post, you deployed your first EC2 instance using Terraform.
That was a huge step.
But if you look at your code now, youβll notice something:
π Everything is hardcoded
π Itβs not reusable
π Itβs not scalable
Letβs fix that.
π― What Youβll Learn
In this guide, youβll understand:
- How to use variables
- How to output useful data
- How Terraform tracks infrastructure (state)
These are core concepts used in real-world DevOps projects.
π Why Variables Matter
Right now, your code probably looks like this:
instance_type = "t2.micro"
This is fine for learning β but not for real projects.
π What if you want:
- different instance types for dev vs production?
- reusable code?
πΉ Step 1 β Create Variables
Create a new file:
touch variables.tf
Add:
variable "instance_type" {
default = "t2.micro"
}
πΉ Step 2 β Use Variables in Your Code
Update your main.tf:
resource "aws_instance" "web_server" {
ami = "ami-xxxxxxxxxxxx"
instance_type = var.instance_type
tags = {
Name = "terraform-server"
}
}
π Now your code is flexible.
πΉ Step 3 β Override with terraform.tfvars
Create:
touch terraform.tfvars
Add:
instance_type = "t3.micro"
π Now you can change behavior without editing code
π€ Outputs β See What Terraform Created
Sometimes you want to know:
π What is the public IP of my EC2?
Create:
touch outputs.tf
Add:
output "instance_public_ip" {
value = aws_instance.web_server.public_ip
}
After running:
terraform apply
Youβll see:
instance_public_ip = 18.xxx.xxx.xxx
π§ Terraform State β The Brain of Terraform
Terraform stores everything in:
terraform.tfstate
This file tracks:
- what resources exist
- their IDs
- their current state
π Inspect Your State
Run:
terraform state list
Example output:
aws_instance.web_server
π Deep Dive
terraform state show aws_instance.web_server
π Youβll see full details of your EC2.
β οΈ Important Insight
Terraform doesnβt query AWS every time.
π It relies on state file
Thatβs why state is critical in real systems.
π‘ DevOps Insight
Right now, your setup is:
Local state β terraform.tfstate
This works for learning.
But in real teams:
β Not safe
β Not shareable
π In the next post, weβll fix this using:
- S3 backend
- DynamoDB locking
π― What You Just Learned
- How to use variables
- How to make Terraform reusable
- How outputs work
- How Terraform state works
π‘ Final Thought
You are no longer just writing Terraform.
You are now:
π designing reusable infrastructure
Thatβs the difference between beginner and real DevOps.
π¨βπ» About the Author
Hi, Iβm Ahkar β sharing DevOps, AWS, and Infrastructure knowledge to help others grow π
I publish bilingual content (Myanmar π²π² + English πΊπΈ) focused on real-world cloud learning.
π Blog: https://mindgnite.com
If you found this helpful, consider following for more Terraform & DevOps content π₯
π Terraform Learning Series
- Part 1: Why Terraform
- Part 2: Setup Guide
- Part 3: First EC2 Deployment
- Part 4: Variables, Outputs & State (this post)
- Part 5: Modules & Remote Backend (coming next)
π Follow to continue the journey π
Top comments (0)