🖥️ Terraform Series Part 3: First EC2 Deployment (Step-by-Step)

📘 Terraform Series — Part 3

Post 2 မှာတော့ Terraform environment ကို setup လုပ်ပြီးပါပြီ။

ဒီ post မှာတော့ အရေးကြီးဆုံးအပိုင်းကို စတင်ပါမယ် —

👉 Terraform ကို အသုံးပြုပြီး AWS EC2 instance တစ်ခုကို တည်ဆောက်ခြင်း


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

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

  • Terraform နဲ့ EC2 instance တစ်ခု တည်ဆောက်နိုင်မယ်
  • init, plan, apply ကို နားလည်မယ်
  • Infrastructure ကို code နဲ့ control လုပ်နိုင်မယ်

👉 In short: You will deploy your first real infrastructure.


📁 Step 1 — Project Structure ဖန်တီးခြင်း

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

mkdir terraform-ec2
cd terraform-ec2

File တွေ ဖန်တီးပါ —

touch provider.tf main.tf

🔹 Step 2 — AWS Provider Configure

provider.tf ကို ဖွင့်ပြီး အောက်ပါ code ထည့်ပါ —

provider "aws" {
  region = "ap-southeast-1"
}

👉 AWS region ကို သင့်အသုံးပြုမည့် region နဲ့ ပြောင်းနိုင်ပါတယ်။

👉 In short: This tells Terraform which cloud and region to use.


🔹 Step 3 — EC2 Instance တည်ဆောက်ခြင်း

main.tf ထဲမှာ အောက်ပါ code ထည့်ပါ —

resource "aws_instance" "web_server" {
  ami           = "ami-xxxxxxxxxxxx"
  instance_type = "t2.micro"

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

⚠️ AMI ID ကို သင့် AWS region မှာရှိတဲ့ valid AMI နဲ့ ပြောင်းရန် လိုအပ်ပါတယ်။

👉 In short: This defines your EC2 instance in code.


🔹 Step 4 — Terraform Initialize

terraform init

👉 Terraform က provider ကို download လုပ်ပြီး project ကို prepare လုပ်ပေးပါတယ်။


🔹 Step 5 — Plan လုပ်ကြည့်ခြင်း

terraform plan

Output မှာ အောက်ပါအတိုင်းတွေ့ရပါမယ် —

Plan: 1 to add, 0 to change, 0 to destroy.

👉 ဆိုလိုတာက resource တစ်ခု (EC2) ကို create လုပ်မယ်ဆိုတာပါ။

👉 In short: Terraform shows what will be created.


🔹 Step 6 — Apply (တည်ဆောက်ခြင်း)

terraform apply

ပြီးရင် —

yes

👉 Terraform က AWS ကို API call လုပ်ပြီး EC2 instance ကို တည်ဆောက်ပေးပါလိမ့်မယ်။


🔍 AWS Console မှာ Verify လုပ်ခြင်း

AWS Console → EC2 → Instances သို့ သွားပြီး —

👉 Instance run နေတဲ့အခြေအနေကို ကြည့်နိုင်ပါတယ် 🎉

👉 In short: Your EC2 is now live.


🧠 Terraform က ဘာလုပ်ခဲ့သလဲ?

Terraform က —

  1. .tf file တွေကို ဖတ်တယ်
  2. Desired state နဲ့ current state ကို compare လုပ်တယ်
  3. AWS API ကို call ခေါ်တယ်
  4. Resource ကို create လုပ်တယ်

👉 ဒါဟာ Infrastructure as Code ရဲ့ လက်တွေ့ example ဖြစ်ပါတယ်။


⚠️ Common Error — AMI Not Found

အောက်ပါ error ကို ကြုံရနိုင်ပါတယ် —

InvalidAMIID.NotFound

❓ ဘာကြောင့်ဖြစ်တာလဲ?

👉 AMI ID တွေဟာ region အလိုက် မတူပါဘူး။


✅ ဖြေရှင်းနည်း

  • AWS Console → EC2 → AMIs
  • သင့် region မှာရှိတဲ့ AMI ID ကို copy လုပ်ပါ
  • Code ထဲမှာ ပြန်ထည့်ပါ

👉 In short: Use a valid AMI for your region.


🧹 Clean Up (အရေးကြီး)

Resource မလိုတော့ရင် —

terraform destroy

👉 AWS charges မဖြစ်အောင် ဖျက်ထားသင့်ပါတယ်။


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

  • Terraform နဲ့ EC2 တည်ဆောက်နည်း
  • init, plan, apply အသုံးပြုပုံ
  • Infrastructure as Code ကို လက်တွေ့ အသုံးပြုပုံ

💡 DevOps Insight

အခု code မှာ AMI ကို hardcode လုပ်ထားပါတယ် —

👉 ဒီနည်းက learning အတွက် ok ဖြစ်ပေမယ့် production မှာတော့ မသင့်ပါဘူး။

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

  • Variables
  • Data sources

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

👉 In short: We will make this production-ready next.


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

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

  • Variables အသုံးပြုခြင်း
  • Outputs
  • Terraform State

👉 Terraform ကို reusable ဖြစ်အောင် ပြောင်းမယ် 🔥


📚 Terraform Learning Series

  • Part 1: Why Terraform
  • Part 2: Setup Guide
  • Part 3: First EC2 Deployment (ဒီ post)
  • Part 4: Variables, Outputs & State (နောက်လာမယ့် post)

👉 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