Skip to main content

Use Cases และ Benefits

Terraform ใช้ทำอะไรได้บ้าง? — รวบรวม use case ที่พบบ่อยในงานจริง พร้อมข้อดีที่จับต้องได้

Use Cases หลัก

1. Multi-Cloud Deployment

deploy infrastructure ข้าม cloud ด้วย config เดียว

provider "aws"   { region = "ap-southeast-1" }
provider "gcp" { project = "my-project" }
provider "azurerm" { features {} }

ตัวอย่าง: บริษัทใช้ AWS เป็นหลัก แต่ DR (disaster recovery) อยู่ GCP

2. Self-Service Infrastructure

ทีม Dev สร้าง dev environment เองได้ ไม่ต้องรอ DevOps

# Dev รันเอง
terraform workspace new feature-xyz
terraform apply

3. Multi-Environment (Dev/Staging/Prod)

ใช้ workspace หรือ module เดียว → 3 environments

terraform workspace select dev      && terraform apply
terraform workspace select staging && terraform apply
terraform workspace select prod && terraform apply

4. Disaster Recovery

infra พัง → terraform apply → กลับมาเหมือนเดิมใน 5 นาที

5. Compliance & Audit

ทุกการเปลี่ยน infra ต้อง PR → reviewer ดู → merge → deploy มี paper trail ครบทุก change

6. Cost Management

  • ปิด non-prod environment ตอนกลางคืน → ประหยัด 60%
  • ใช้ Infracost ดูราคาก่อน apply
infracost breakdown --path .
# → เห็นราคา/เดือน ก่อน deploy

7. Kubernetes Cluster Management

สร้าง EKS / GKE / AKS cluster + node groups + addons

8. SaaS Provisioning

Terraform ไม่ใช่แค่ infrastructure — manage SaaS ได้ด้วย:

  • GitHub repos / teams / branch protection
  • Datadog dashboards / monitors
  • PagerDuty schedules
  • Cloudflare DNS / firewall rules
resource "github_repository" "my_repo" {
name = "my-app"
description = "Managed by Terraform"
visibility = "private"
}

Benefits ที่จับต้องได้

🚀 Speed

TaskManualTerraform
สร้าง VPC + subnets + NAT30 นาที30 วินาที
สร้าง EKS cluster1 ชั่วโมง15 นาที
สร้าง dev environment ใหม่1 วัน5 นาที

💰 Cost Savings

  • Tear down dev/staging กลางคืน → ประหยัด 70% ของค่าใช้จ่าย dev
  • Right-sizing automation — เปลี่ยน instance type ผ่าน variable
  • Tagging อัตโนมัติ — ทำ cost allocation ง่าย

🔒 Risk Reduction

  • Plan ก่อน apply — ไม่มี surprise
  • Drift detection — รู้ทันทีถ้ามีคนแก้ใน console
  • Rollback ง่ายgit revert + terraform apply

👥 Team Collaboration

  • Code review infrastructure changes ผ่าน PR
  • Knowledge sharing — code = documentation
  • Onboarding เร็ว — dev ใหม่อ่าน .tf รู้ว่า infra หน้าตายังไง

📋 Compliance

  • Audit log ทุก change ผ่าน Git history
  • Policy as Code — Sentinel / OPA ป้องกัน config ไม่ตรง compliance

ตัวอย่างจริง: บริษัท X ก่อน vs หลังใช้ Terraform

Metricก่อนหลัง
เวลาสร้าง dev env2 วัน10 นาที
Production incidents จาก config drift4-5 ครั้ง/เดือน<1 ครั้ง/เดือน
Onboarding engineer ใหม่2 สัปดาห์3 วัน
ค่า cloud เดือนละ100% baseline65% (ลดลง 35%)

เมื่อไหร่ที่ ไม่ ควรใช้ Terraform?

ข้อจำกัด

Terraform ไม่ใช่ silver bullet — ในบางกรณีเครื่องมืออื่นเหมาะกว่า:

  • Configuration management ระดับ OS — ใช้ Ansible/Chef แทน (Terraform เน้น provisioning)
  • One-off scripts — bash script เพียงพอ
  • Application deployment — ใช้ Helm/ArgoCD/Flux แทน
  • State อยู่ใน control plane เอง (เช่น K8s) — ใช้ kubectl/Helm ตรงๆ

สรุป

  • Terraform เก่งเรื่อง provisioning infrastructure ในทุก cloud และ SaaS
  • ข้อดีหลัก: speed, cost saving, risk reduction, audit trail
  • เหมาะกับทีม 2 คนขึ้นไป ที่ต้องจัดการ infra หลาย environment
  • ไม่เหมาะ: OS-level config, app deployment, K8s objects

ต่อไป → CaC vs IaC เพื่อเข้าใจว่า Configuration as Code ต่างจาก Infrastructure as Code อย่างไร