terraform fmt
terraform fmt= built-in formatter ที่จัด HCL code ให้ consistent style — เหมือนgofmtของ Go
terraform fmt ทำอะไร?
จัด:
- Indentation — ใช้ 2 spaces
- Alignment —
=ของ argument ใน block เดียวกัน - Quote style — ลบ quote ที่ไม่จำเป็น
- Comment style —
//→# - Newlines — ตัด blank line เกิน
Usage
Format ทั้งหมดใน folder ปัจจุบัน
terraform fmt
Recursive (สำหรับ subfolder)
terraform fmt -recursive
Check only (ไม่แก้ — ใช้ใน CI)
terraform fmt -check
# Exit code 0 = format ถูกแล้ว
# Exit code 3 = ต้องการ format
Diff (ดูว่าจะเปลี่ยนอะไร)
terraform fmt -diff
Specific file
terraform fmt main.tf
terraform fmt main.tf variables.tf
Stdin (pipe)
cat ugly.tf | terraform fmt -
ตัวอย่าง: ก่อน vs หลัง
ก่อน (ugly):
resource "aws_instance" "web" {
ami="ami-12345"
instance_type="t2.micro"
tags={
Name= "web"
Environment="prod"
}
}
หลัง terraform fmt:
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t2.micro"
tags = {
Name = "web"
Environment = "prod"
}
}
รวมกับ Editor
VS Code
ติดตั้ง HashiCorp Terraform extension
ตั้ง format on save ใน settings.json:
{
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true,
"editor.tabSize": 2
},
"[terraform-vars]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true,
"editor.tabSize": 2
}
}
Neovim
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.tf",
command = "silent! !terraform fmt %",
})
หรือใช้ tflint-lsp + terraform-ls
IntelliJ
HashiCorp Terraform plugin มี format on save built-in
Pre-commit Hook
ใช้ pre-commit auto format ก่อน commit:
.pre-commit-config.yaml
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.96.0
hooks:
- id: terraform_fmt
pre-commit install
# ตอนนี้ทุก commit จะรัน terraform fmt อัตโนมัติ
CI/CD
GitHub Actions
.github/workflows/terraform.yml
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.9.8
- name: Format Check
run: terraform fmt -check -recursive
ถ้าใครลืม format → CI fail
GitLab CI
.gitlab-ci.yml
fmt:
image: hashicorp/terraform:1.9.8
script:
- terraform fmt -check -recursive
Format ในไฟล์ JSON / Variable File
terraform fmt format:
*.tf✅*.tfvars✅*.tf.json❌ (JSON, ใช้jqหรือ Prettier)
Format Style Rules
ดูที่ Terraform Style Conventions:
Indentation: 2 spaces
resource "x" "y" {
argument = "value" # 2 spaces
}
Alignment ของ =
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t2.micro" # ← align ตามตัวที่ยาวสุด
monitoring = true
}
Top-level blocks separated by blank line
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
}
Sub-blocks ไม่ต้องเว้นบรรทัด
resource "aws_security_group" "web" {
name = "web"
ingress {
from_port = 80
}
ingress {
from_port = 443
}
}
Limitations
terraform fmt ไม่:
- ❌ จัด argument ตามลำดับ (
name,description, etc.) - ❌ Refactor / restructure code
- ❌ ตรวจ logic error (ใช้
terraform validateหรือtflint) - ❌ Sort
tagsmap
ต้องการความสามารถเพิ่ม → ใช้ tflint + terraform-docs
ตัวอย่าง: รวม Workflow
# 1. Format
terraform fmt -recursive
# 2. Validate
terraform validate
# 3. Lint
tflint --recursive
# 4. Plan
terraform plan
สรุป
terraform fmt= built-in formatter- ใช้
-recursiveสำหรับ subfolder,-checkใน CI - รวมกับ editor (VS Code/Neovim/IntelliJ) → format on save
- Pre-commit hook + CI check ให้ code style consistent
- ไม่ใช่ linter — แค่ formatter (ใช้ tflint สำหรับ lint)
ต่อไป → terraform validate