Terraform
Mastering Terraform — เอกสารสอน Terraform ตั้งแต่พื้นฐานจนถึง enterprise-level จัดเรียงตาม roadmap.sh / Terraform
Terraform คืออะไร?
Terraform เป็นเครื่องมือ Infrastructure as Code (IaC) จาก HashiCorp ที่ช่วยให้เราสร้าง จัดการ และทำลาย infrastructure (เช่น servers, networks, databases) ได้ผ่านการเขียน config file แทนการคลิกใน console
แทนที่จะคลิกสร้าง EC2 ทีละตัวใน AWS Console เราเขียนเป็น code ครั้งเดียว แล้วรัน:
terraform apply
Terraform จะไปสร้าง infrastructure ให้ตาม config — และเก็บ state ไว้ track ว่า resource ไหนถูกสร้างไปแล้ว
ทำไมต้องเรียน Terraform?
- Multi-cloud — รองรับ AWS, GCP, Azure, Cloudflare ฯลฯ ใช้ syntax เดียวกันหมด
- Declarative — เขียนว่า "อยากได้อะไร" ไม่ต้องเขียน "สร้างยังไง"
- Version Control — config อยู่ใน Git → review ได้, rollback ได้, audit ได้
- Industry Standard — เป็นมาตรฐาน DevOps ทั่วโลก, JD ส่วนใหญ่ขอ
Roadmap (อ้างอิง roadmap.sh)
ใน document นี้แบ่งเป็น 19 sections หลัก ตาม Mastering Terraform Roadmap:
| # | Section | สิ่งที่เรียน |
|---|---|---|
| 1 | Introduction | IaC, Terraform คืออะไร, ติดตั้ง |
| 2 | HCL Language | HashiCorp Configuration Language syntax |
| 3 | Providers & Initialization | Provider, Registry, terraform init |
| 4 | Resources | Resource block, lifecycle, meta-arguments |
| 5 | Variables | Input vars, types, validation, locals |
| 6 | Outputs | Output values, sensitive, preconditions |
| 7 | Format & Validate | fmt, validate, TFLint |
| 8 | Deployment | plan, apply |
| 9 | Clean Up | destroy |
| 10 | State Management | State file, remote backend, locking |
| 11 | State Commands | state list/mv/rm, import |
| 12 | Modules | Reusable code blocks |
| 13 | Provisioners | local-exec, remote-exec, file |
| 14 | Data Sources | อ่านข้อมูลจาก provider |
| 15 | CI/CD & Workspaces | GitHub Actions, GitLab CI, Jenkins |
| 16 | Testing | Unit, contract, integration, E2E |
| 17 | Scaling Terraform | Terragrunt, Infracost, parallelism |
| 18 | Security | Vault, Sentinel, Checkov, Trivy |
| 19 | HCP | HashiCorp Cloud Platform |
วิธีเรียนที่แนะนำ
- เริ่มจาก Section 1-3 ให้เข้าใจ IaC + ติดตั้ง + เขียน HCL ได้
- ลงมือจริงตั้งแต่ Section 4 — เปิด AWS Free Tier แล้วลอง
terraform applyกับ S3 bucket อันแรก - ห้ามข้าม State Management (Section 10) — เป็นหัวใจของ Terraform หลายคนใช้ผิด
- Modules (Section 12) จะใช้บ่อยมากใน production — ฝึกสร้างเอง
- Section 15-19 เก็บไว้เรียนตอนเริ่มทำงานจริงในทีม
Prerequisite
ก่อนเริ่มเรียน Terraform ควรมีพื้นฐานดังนี้:
- Cloud Concept — เข้าใจ EC2, S3, VPC (อย่างน้อยใน AWS section)
- Command Line — ใช้ Terminal (bash/zsh) ได้
- Git — push/pull/branch
- YAML/JSON — อ่านออก เขียนได้
ไม่จำเป็นต้องเก่ง programming — HCL syntax ง่ายกว่า general programming language มาก
เริ่มต้นที่นี่ →
ไปที่ Section 1: Introduction เพื่อเริ่มเรียน Terraform