đ 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"