🔁 Terraform Variables, Outputs & State — Make Your Code Reusable (Part 4)
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 r...

Source: DEV Community
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"