🔁 Terraform Series Part 4: Variables, Outputs & State (Make Your Code Reusable)

📘 Terraform Series — Part 4

Post 3 မှာတော့ Terraform နဲ့ EC2 instance တစ်ခုကို အောင်မြင်စွာ တည်ဆောက်နိုင်ခဲ့ပါပြီ။

ဒါပေမယ့် သင့် code ကို ပြန်ကြည့်လိုက်ရင် —

👉 Hardcoded ဖြစ်နေတယ် 👉 Reusable မဟုတ်ဘူး 👉 Production-ready မဖြစ်သေးဘူး

ဒီ post မှာ ဒီပြဿနာတွေကို ဖြေရှင်းကြမယ်။


🎯 ဒီ post မှာ သင်လေ့လာမယ့်အရာ

ဒီ guide အဆုံးမှာတော့ —

  • Variables ကို အသုံးပြုနိုင်မယ်
  • Outputs ကို နားလည်မယ်
  • Terraform State ကို အခြေခံနားလည်မယ်

👉 In short: You will make your Terraform code reusable and smarter.


🔁 Variables — Hardcoding မလုပ်တော့ဘူး

လက်ရှိ code က ဒီလို ဖြစ်နေနိုင်ပါတယ် —

instance_type = "t2.micro"

ဒီနည်းလမ်းက learning အတွက် ok ဖြစ်ပေမယ့် —

👉 environment ပြောင်းတဲ့အခါ 👉 reusable code လုပ်ချင်တဲ့အခါ

အဆင်မပြေပါဘူး။


🔹 Step 1 — Variables ဖန်တီးခြင်း

File တစ်ခုဖန်တီးပါ —

touch variables.tf

အောက်ပါ code ထည့်ပါ —

variable "instance_type" {
  default = "t2.micro"
}

👉 Variable တစ်ခု define လုပ်လိုက်တာပါ။


🔹 Step 2 — Variable ကို အသုံးပြုခြင်း

main.tf ကို update လုပ်ပါ —

resource "aws_instance" "web_server" {
  ami           = "ami-xxxxxxxxxxxx"
  instance_type = var.instance_type

  tags = {
    Name = "terraform-server"
  }
}

👉 var.instance_type ဆိုပြီး variable ကို reference လုပ်ပါတယ်။

👉 In short: Replace hardcoded values with variables.


🔹 Step 3 — terraform.tfvars အသုံးပြုခြင်း

File တစ်ခုထပ်ဖန်တီးပါ —

touch terraform.tfvars

အောက်ပါ code ထည့်ပါ —

instance_type = "t3.micro"

👉 Code မပြောင်းဘဲ behavior ကို ပြောင်းနိုင်ပါတယ်။

👉 In short: Change values without editing main code.


📤 Outputs — Result ကို ပြသခြင်း

EC2 public IP ကို သိချင်ရင် —

File တစ်ခုဖန်တီးပါ —

touch outputs.tf

Code ထည့်ပါ —

output "instance_public_ip" {
  value = aws_instance.web_server.public_ip
}

Run —

terraform apply

Output —

instance_public_ip = 18.xxx.xxx.xxx

👉 Terraform က result ကို ပြပေးပါတယ်။

👉 In short: Outputs show useful information after deployment.


🧠 Terraform State — Terraform ရဲ့ Brain

Terraform က infrastructure info ကို ဒီ file ထဲမှာ သိမ်းထားပါတယ် —

terraform.tfstate

ဒီ file မှာ —

  • Resource info
  • IDs
  • Current state

တို့ကို သိမ်းထားပါတယ်။


🔍 State ကို ကြည့်ခြင်း

terraform state list

Example —

aws_instance.web_server

🔎 Detail ကြည့်ခြင်း

terraform state show aws_instance.web_server

👉 Resource details အပြည့်အစုံကို မြင်နိုင်ပါတယ်။

👉 In short: Terraform uses state to track infrastructure.


⚠️ အရေးကြီးသော အချက်

Terraform က AWS ကို အမြဲ query မလုပ်ပါဘူး —

👉 State file ကို အဓိကအသုံးပြုပါတယ်။

ဒါကြောင့် state က အလွန်အရေးကြီးပါတယ်။


💡 DevOps Insight

အခု သင်အသုံးပြုနေတဲ့ state က —

Local state → terraform.tfstate

ဒီနည်းလမ်းက learning အတွက် ok ဖြစ်ပေမယ့် —

❌ Team work အတွက် မသင့်ပါ ❌ Secure မဖြစ်ပါ

👉 နောက် post မှာ —

  • S3 backend
  • DynamoDB locking

ကို အသုံးပြုပြီး production-ready ဖြစ်အောင် ပြောင်းမယ်။

👉 In short: We will move state to remote backend next.


🎯 ဒီနေ့ သင် လေ့လာခဲ့တာ

  • Variables အသုံးပြုနည်း
  • Outputs ပြသနည်း
  • Terraform State အခြေခံ

💡 နောက်ဆုံး အတွေး

Terraform ကို အသုံးပြုနိုင်တာနဲ့ မပြီးသေးပါဘူး —

👉 Reusable ဖြစ်အောင် design လုပ်နိုင်မှ DevOps ဖြစ်ပါတယ်။


🚀 နောက်ထပ် ဘာလာမလဲ?

နောက် post မှာတော့ —

  • Modules
  • Remote Backend (S3 + DynamoDB)

👉 Terraform ကို Production Level ထိ သွားကြမယ် 🔥


📚 Terraform Learning Series

  • Part 1: Why Terraform
  • Part 2: Setup Guide
  • Part 3: First EC2 Deployment
  • Part 4: Variables, Outputs & State (ဒီ post)
  • Part 5: Modules & Remote Backend (လာမယ်)

👉 Let’s continue this journey together 🚀

👨‍💻 English Version “Read full English version on Dev.to → visit Here

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
Share via
Copy link