
π 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 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" } } π
Continue reading on Dev.to Tutorial
Opens in a new tab


