terraform validate
terraform validate= เช็ค syntax และ internal consistency ของ config — ไม่เรียก cloud API ไม่ต้องมี credentials
Validate ทำอะไร?
✅ ตรวจ:
- Syntax ผิด (typo, missing brace)
- Unknown argument / resource type
- Missing required argument
- Type mismatch
- Reference ไป variable / resource ที่ไม่มี
- Circular dependency
❌ ไม่ตรวจ:
- ค่าจะถูกตอน apply ไหม (เช่น AMI ID มีจริงไหม)
- Cloud API จะ accept ไหม
- IAM permission พอไหม
Usage
terraform validate
ต้อง terraform init ก่อน — เพราะ validate ใช้ provider schema:
terraform init
terraform validate
ตัวอย่าง Errors
Syntax Error
main.tf
resource "aws_instance" "web" {
ami = "ami-12345"
}
} # ← } เกิน
$ terraform validate
╷
│ Error: Argument or block definition required
│
│ on main.tf line 4:
│ 4: }
Unknown Argument
resource "aws_instance" "web" {
ami = "ami-12345"
bad_arg = "x" # ← ไม่มี argument นี้
}
$ terraform validate
╷
│ Error: Unsupported argument
│
│ An argument named "bad_arg" is not expected here.
Missing Required Argument
resource "aws_s3_bucket_policy" "data" {
# ขาด bucket และ policy
}
$ terraform validate
╷
│ Error: Missing required argument
│
│ The argument "bucket" is required.
Wrong Type
variable "count" {
type = number
}
resource "aws_instance" "web" {
count = var.count # type = number — OK
# ...
}
# แต่ส่งค่า:
$ terraform plan -var="count=abc"
# Error: Invalid value for input variable
Reference Error
resource "aws_instance" "web" {
subnet_id = aws_subnet.does_not_exist.id # ← resource ไม่มี
}
$ terraform validate
╷
│ Error: Reference to undeclared resource
เปรียบเทียบ: fmt vs validate vs plan
| Command | ตรวจอะไร | ต้องมี init? | ต้อง credentials? |
|---|---|---|---|
terraform fmt | format style | ❌ | ❌ |
terraform validate | syntax + types | ✅ | ❌ |
terraform plan | จะสร้าง/แก้/ลบอะไร | ✅ | ✅ |
terraform apply | ลงมือสร้างจริง | ✅ | ✅ |
validate = fastest sanity check — รันใน CI ก่อน plan
CI/CD
GitHub Actions
- name: Init
run: terraform init -backend=false
- name: Format
run: terraform fmt -check -recursive
- name: Validate
run: terraform validate
ใช้ -backend=false เพื่อ skip backend init (เร็วกว่า + ไม่ต้องมี credentials)
GitLab CI
validate:
image: hashicorp/terraform:1.9.8
script:
- terraform init -backend=false
- terraform validate
JSON Output (เหมาะ machine parse)
terraform validate -json
{
"format_version": "1.0",
"valid": false,
"error_count": 1,
"warning_count": 0,
"diagnostics": [
{
"severity": "error",
"summary": "Unsupported argument",
"detail": "...",
"range": {
"filename": "main.tf",
"start": {"line": 5, "column": 3, "byte": 100},
"end": {"line": 5, "column": 10, "byte": 107}
}
}
]
}
ใช้ใน CI เพื่อ parse + อ้างอิง bug ใน PR
Multi-Module Validate
ถ้ามีหลาย directory:
# Validate root
terraform validate
# Validate module ตรงๆ
cd modules/vpc
terraform init -backend=false
terraform validate
หรือใช้ shell loop:
find . -name "*.tf" -exec dirname {} \; | sort -u | while read dir; do
echo "Validating $dir..."
(cd "$dir" && terraform init -backend=false && terraform validate)
done
รวมกับ Editor
VS Code + HashiCorp extension จะ run validate inline ตอนเขียนโค้ด:
resource "aws_instance" "web" {
ami = "ami-12345"
bad_arg = "x" # ← red squiggle ทันที!
}
ไม่ต้องรัน CLI ทุกครั้ง
When to Run
ทุกครั้ง ก่อน plan/apply — รวดเร็ว แค่วินาทีเดียว
Limitations
resource "aws_instance" "web" {
ami = "ami-DOES-NOT-EXIST" # ← validate ไม่จับ!
instance_type = "t999.gigalarge" # ← validate ไม่จับ!
}
validate ไม่เช็คว่าค่าจริงมีหรือไม่ — รอให้ provider validate ตอน plan/apply
รวมกับ Other Tools
Workflow ที่แนะนำ
# 1. Format
terraform fmt -check -recursive
# 2. Validate (syntax + types)
terraform init -backend=false
terraform validate
# 3. Lint (best practices)
tflint --recursive
# 4. Security scan
checkov -d .
# 5. Plan
terraform plan
สรุป
terraform validate= syntax + type + reference check- ต้อง
terraform initก่อน (provider schema) - ไม่ต้องใช้ credentials — รันใน CI ได้เลย
- ใช้
-jsonตอนต้อง parse output - ส่วน editor (VS Code) มี inline validation ในตัว
ต่อไป → TFLint