DEV Community

Cover image for πŸ” Terraform Variables, Outputs & State β€” Make Your Code Reusable (Part 4)
Ahkar Swe
Ahkar Swe

Posted on

πŸ” 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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Add:

variable "instance_type" {
  default = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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"
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Now your code is flexible.


πŸ”Ή Step 3 β€” Override with terraform.tfvars

Create:

touch terraform.tfvars
Enter fullscreen mode Exit fullscreen mode

Add:

instance_type = "t3.micro"
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

Add:

output "instance_public_ip" {
  value = aws_instance.web_server.public_ip
}
Enter fullscreen mode Exit fullscreen mode

After running:

terraform apply
Enter fullscreen mode Exit fullscreen mode

You’ll see:

instance_public_ip = 18.xxx.xxx.xxx
Enter fullscreen mode Exit fullscreen mode

🧠 Terraform State β€” The Brain of Terraform

Terraform stores everything in:

terraform.tfstate
Enter fullscreen mode Exit fullscreen mode

This file tracks:

  • what resources exist
  • their IDs
  • their current state

πŸ” Inspect Your State

Run:

terraform state list
Enter fullscreen mode Exit fullscreen mode

Example output:

aws_instance.web_server
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Deep Dive

terraform state show aws_instance.web_server
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

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)