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