Versions
Version constraints ในกำหนด Terraform version + provider version — สำคัญมากในการป้องกัน "works on my machine" และ breaking changes
ทำไมต้องล็อค Version?
Terraform + provider = software ที่อัปเดตบ่อย → ถ้าไม่ล็อค:
- 😱 breaking change อาจทำให้ apply ไม่ได้
- 😱 state file อาจ incompatible ระหว่าง version
- 😱 ทีมแต่ละคนใช้ version ต่างกัน → diff ใน plan ที่ไม่ควรมี
3 ที่ที่ต้องตั้ง Version
1. Terraform Version (CLI)
terraform {
required_version = ">= 1.5.0, < 2.0.0"
}
ถ้าใช้ Terraform CLI version ผิด → fail ทันทีตอน terraform init
2. Provider Version
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.70"
}
}
}
3. .terraform.lock.hcl (auto-generated)
หลัง terraform init Terraform จะสร้าง lock file นี้ → commit เข้า Git
provider "registry.terraform.io/hashicorp/aws" {
version = "5.70.0"
constraints = "~> 5.70"
hashes = [...]
}
Version Constraint Operators
| Operator | ความหมาย | ตัวอย่าง |
|---|---|---|
= หรือ no operator | exact version | 5.70.0 |
!= | ไม่ใช่ version นี้ | != 5.69.0 |
> < >= <= | comparison | >= 5.0 |
~> ⭐ | pessimistic constraint | ~> 5.70 |
~> (Pessimistic / Tilde-Greater)
อนุญาต patch version ใหม่ แต่ ไม่ allow minor/major ใหม่
| Constraint | Allowed | Not Allowed |
|---|---|---|
~> 5.70 | 5.70.x, 5.71.x, ... 5.x.x | 6.0.0 |
~> 5.70.0 | 5.70.0, 5.70.1, ... 5.70.x | 5.71.0 |
~> แบบไหนดี?~> 5.70(allow minor) → flexible แต่อาจเจอ breaking change~> 5.70.0(allow patch only) → ปลอดภัยกว่า แต่ต้อง bump บ่อย
production แนะนำ ~> X.Y (allow minor) + lock file ป้องกันความไม่แน่นอน
Multiple Constraints (AND)
version = ">= 5.0, < 6.0"
Semantic Versioning (SemVer)
Terraform + providers ใช้ SemVer: MAJOR.MINOR.PATCH
- MAJOR (5 → 6) — breaking change (ต้องดู migration guide)
- MINOR (5.70 → 5.71) — new feature, backwards compatible
- PATCH (5.70.0 → 5.70.1) — bug fix
ตัวอย่าง: Production-Grade Versions
terraform {
required_version = ">= 1.6.0, < 2.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.70"
}
random = {
source = "hashicorp/random"
version = "~> 3.5"
}
null = {
source = "hashicorp/null"
version = "~> 3.2"
}
}
}
วิธี Upgrade Provider
1. แก้ constraint
# จาก
version = "~> 5.50"
# เป็น
version = "~> 5.70"
2. Run init ด้วย -upgrade
terraform init -upgrade
3. รัน plan ดูว่ามี diff ไหม
terraform plan
4. อ่าน changelog ของ provider
ดูที่ provider GitHub releases เผื่อมี breaking change
5. Apply ใน dev → ทดสอบ → apply prod
ถ้า major version ขึ้น (เช่น aws v4 → v5) มักมี breaking change
ทำ:
- อ่าน upgrade guide ของ provider
- ใน dev environment ก่อน
- Test ทั้ง happy path + edge case
- Apply prod แบบควบคุม
ดู Version ปัจจุบัน
# CLI version
terraform version
# Provider version (จาก lock file)
cat .terraform.lock.hcl
# หรือ
terraform providers
ตัวอย่าง output:
Terraform v1.9.8
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v5.70.0
+ provider registry.terraform.io/hashicorp/random v3.6.3
Lock File: ลึกขึ้น
.terraform.lock.hcl รวม:
- version ที่เลือกใช้
- constraints จาก config
- hashes ของ provider binary (ป้องกัน supply chain attack)
provider "registry.terraform.io/hashicorp/aws" {
version = "5.70.0"
constraints = "~> 5.70"
hashes = [
"h1:pIG6vIpNlLHsRoZdtCAnmMb6DZK4o9VBBvfkGqLI8sk=",
"zh:0a76...",
"zh:1b87...",
]
}
ถ้าทีมมีคนใช้ทั้ง Mac (arm64) + Linux (amd64) → run
terraform providers lock \
-platform=linux_amd64 \
-platform=darwin_arm64 \
-platform=darwin_amd64 \
-platform=windows_amd64
จะ pre-fetch hashes สำหรับทุก platform ป้องกัน lock file mismatch ใน CI
Anti-Patterns
# ❌ ไม่ระบุ version เลย
provider "aws" {}
# ❌ pin patch ที่เก่ามาก
version = "= 5.0.0"
# ❌ open-ended (latest)
version = ">= 5.0"
# ✅ pessimistic constraint
version = "~> 5.70"
สรุป
- ตั้ง version ที่ 3 ที่:
required_version(CLI),required_providers(provider),.terraform.lock.hcl(auto) - ใช้
~>operator (pessimistic) — flexible แต่ปลอดภัย - Commit
.terraform.lock.hclเข้า Git - Upgrade ผ่าน
terraform init -upgrade+ อ่าน changelog - Cross-platform team → ใช้
terraform providers lock
ต่อไป → Section 4: Resources