ทำความเข้าใจ State

📖 text • 10 นาที

ทำความเข้าใจ State

State คืออะไร?

State คือไฟล์ที่ Terraform ใช้เก็บข้อมูลว่า infrastructure ปัจจุบันมีอะไรอยู่บ้าง

Code (.tf files)     = สิ่งที่คุณ "ต้องการ" (desired state)
State (.tfstate)     = สิ่งที่ Terraform "รู้" (known state)
Real Infrastructure  = สิ่งที่ "มีจริง" บน cloud

เมื่อรัน terraform plan — Terraform จะเทียบ 3 สิ่งนี้:

Code ←→ State ←→ Real Infrastructure
         ↓
    "ต้องเปลี่ยนอะไรบ้าง?"

ทำไมต้องมี State?

1. Performance

ไม่ต้อง query cloud API ทุกครั้ง — Terraform อ่านจาก state แทน

2. Mapping

เชื่อมโยง resource ใน code กับ resource จริง:

aws_instance.web  →  i-0abc123def456789

3. Metadata

เก็บข้อมูลเพิ่มเติม เช่น dependency ระหว่าง resource

4. Planning

ใช้คำนวณว่าต้อง create, update, หรือ delete อะไร

State File หน้าตาเป็นยังไง?

terraform.tfstate คือ JSON file:

{
  "version": 4,
  "terraform_version": "1.9.0",
  "resources": [
    {
      "mode": "managed",
      "type": "aws_instance",
      "name": "web",
      "instances": [
        {
          "attributes": {
            "id": "i-0abc123def456789",
            "ami": "ami-0c55b159cbfafe1f0",
            "instance_type": "t2.micro",
            "public_ip": "54.123.45.67",
            "tags": {
              "Name": "web-server"
            }
          }
        }
      ]
    }
  ]
}

สำคัญ: ห้ามแก้ไข state file ด้วยมือ! ใช้ terraform state commands เท่านั้น

State มี Sensitive Data

State file อาจมีข้อมูลลับ:

  • Database passwords
  • API keys
  • Private IPs
  • Connection strings

ดังนั้น:

  • ห้าม commit terraform.tfstate เข้า git
  • เพิ่มใน .gitignore:
*.tfstate
*.tfstate.backup
.terraform/

Local State vs Remote State

Local State (default)

my-project/
├── main.tf
├── terraform.tfstate        ← อยู่บนเครื่องเรา
└── terraform.tfstate.backup

ปัญหา:

  • ทำงานคนเดียวเท่านั้น
  • ถ้าเครื่องพัง = state หาย
  • ไม่ปลอดภัย (sensitive data อยู่บนเครื่อง)

Remote State

my-project/
├── main.tf
└── (state อยู่บน S3/Cloud Storage)

ข้อดี:

  • ทำงานเป็นทีมได้
  • มี backup อัตโนมัติ
  • เข้ารหัสได้ (encryption at rest)
  • ใช้ state locking ป้องกันแก้พร้อมกัน

State Locking

ป้องกันไม่ให้ 2 คน apply พร้อมกัน:

Developer A: terraform apply  →  Lock ✓  →  Apply...
Developer B: terraform apply  →  Lock ✗  →  "Error: state locked"
                                             (ต้องรอ A เสร็จก่อน)

Backend ที่รองรับ locking:

  • S3 + DynamoDB
  • Google Cloud Storage
  • Azure Blob Storage
  • Terraform Cloud

State Drift

เกิดเมื่อมีคนไปแก้ infrastructure ตรงๆ (ไม่ผ่าน Terraform):

Code: instance_type = "t2.micro"
State: instance_type = "t2.micro"
Real:  instance_type = "t3.large"   ← ถูกแก้ผ่าน AWS Console

วิธีจัดการ:

# ตรวจหา drift
terraform plan

# refresh state ให้ตรงกับ reality
terraform apply -refresh-only

-refresh-only จะอัพเดท state ให้ตรงกับ infrastructure จริง โดยไม่แก้ไข infrastructure

สรุป

Concept คำอธิบาย
State ไฟล์ JSON เก็บข้อมูล infrastructure ปัจจุบัน
Local State เก็บบนเครื่อง (default, ใช้คนเดียว)
Remote State เก็บบน cloud (ทำงานเป็นทีม)
State Locking ป้องกัน 2 คน apply พร้อมกัน
State Drift infrastructure ถูกแก้นอก Terraform
.gitignore ห้าม commit .tfstate เข้า git

บทถัดไปเราจะเรียนรู้วิธีตั้งค่า Remote State