Skip to main content

Variable Definition File (.tfvars)

ไฟล์ .tfvars = ที่เก็บ "ค่า" ของ variables — แยกออกจาก variable declaration เพื่อให้ reuse + multi-env ได้สะดวก

ไฟล์ .tfvars คืออะไร?

ใช้เก็บ ค่า ของ variable (ไม่ใช่การประกาศ) — syntax คล้าย HCL แต่ไม่มี variable block

terraform.tfvars
region          = "ap-southeast-1"
environment = "prod"
instance_count = 3
instance_type = "t3.large"
enable_monitoring = true

tags = {
Project = "myapp"
Owner = "platform-team"
CostCenter = "engineering"
}

Auto-Loaded Files

Terraform โหลดไฟล์ต่อไปนี้อัตโนมัติ (ไม่ต้องระบุ flag):

  1. terraform.tfvars
  2. terraform.tfvars.json
  3. *.auto.tfvars
  4. *.auto.tfvars.json
my-project/
├── main.tf
├── variables.tf
├── terraform.tfvars # ← auto-load
└── secret.auto.tfvars # ← auto-load
terraform apply
# ← ใช้ค่าจาก terraform.tfvars + secret.auto.tfvars เลย

Manual Load: -var-file

terraform apply -var-file="prod.tfvars"
terraform apply -var-file="prod.tfvars" -var-file="overrides.tfvars"

ใช้ตอนต้อง switch ค่า dev/staging/prod

Multi-Environment Pattern

Pattern 1: ไฟล์แยกตาม env

project/
├── main.tf
├── variables.tf
├── terraform.tfvars # default values
├── envs/
│ ├── dev.tfvars
│ ├── staging.tfvars
│ └── prod.tfvars
terraform apply -var-file=envs/dev.tfvars
terraform apply -var-file=envs/prod.tfvars

Pattern 2: Workspace + tfvars

terraform workspace new dev
terraform workspace new prod

terraform workspace select dev
terraform apply -var-file=envs/dev.tfvars

(ดูเพิ่มใน Section 15: Workspaces)

ตัวอย่าง: Production Setup

variables.tf
variable "environment" {
type = string
}

variable "vpc_cidr" {
type = string
default = "10.0.0.0/16"
}

variable "instance_count" {
type = number
}

variable "instance_type" {
type = string
}

variable "enable_backup" {
type = bool
default = false
}
envs/dev.tfvars
environment    = "dev"
vpc_cidr = "10.10.0.0/16"
instance_count = 1
instance_type = "t3.micro"
enable_backup = false
envs/prod.tfvars
environment    = "prod"
vpc_cidr = "10.0.0.0/16"
instance_count = 5
instance_type = "t3.large"
enable_backup = true

Deploy:

terraform apply -var-file=envs/dev.tfvars   # dev
terraform apply -var-file=envs/prod.tfvars # prod

JSON Format

ถ้าต้อง generate ค่าจาก script — ใช้ JSON ง่ายกว่า

terraform.tfvars.json
{
"region": "ap-southeast-1",
"environment": "prod",
"instance_count": 3,
"tags": {
"Project": "myapp",
"Owner": "platform-team"
}
}

ใช้:

terraform apply -var-file=terraform.tfvars.json

Secrets ใน .tfvars

ห้าม commit .tfvars ที่มี secret
  • .tfvars จะถูก commit ลง Git ถ้าไม่ระวัง → secret leak
  • ใส่ใน .gitignore:
.gitignore
# Sensitive tfvars
secrets.auto.tfvars
*.secret.tfvars

หรือใช้:

  • Environment variable (TF_VAR_*)
  • AWS Secrets Manager / Vault → fetch ผ่าน data block
  • Encrypted SOPS files

Pattern: Secret + Public แยกไฟล์

terraform.tfvars (commit ลง Git)
region        = "ap-southeast-1"
environment = "dev"
instance_type = "t3.micro"
secrets.auto.tfvars (.gitignore!)
db_password   = "super-secret"
api_key = "sk_live_..."

→ Public config เห็น, secret อยู่ local เท่านั้น

Best Practices

✅ DO:
- ใช้ .tfvars แยกค่าออกจาก declaration
- ใช้ ฟอลเดอร์ envs/ จัดการ multi-env
- Auto-load ใช้ terraform.tfvars เป็น default
- Secrets แยกไฟล์ + .gitignore

❌ DON'T:
- ห้ามใส่ secret ใน committed .tfvars
- ห้ามมี circular dependencies (.tfvars import .tfvars — ทำไม่ได้)
- ห้าม hard-code env-specific value ใน main.tf

ตัวอย่าง: Real-World Project

my-app/
├── main.tf
├── variables.tf
├── outputs.tf
├── providers.tf
├── versions.tf
├── terraform.tfvars # common defaults (commit)
├── secrets.auto.tfvars # secrets (gitignore)
├── .gitignore
└── envs/
├── dev.tfvars # dev overrides (commit)
├── staging.tfvars # staging overrides (commit)
└── prod.tfvars # prod overrides (commit)

สรุป

  • .tfvars = ค่าของ variable (separate from declaration)
  • Auto-load: terraform.tfvars, *.auto.tfvars (+ .json)
  • Manual: -var-file=...
  • Multi-env: 1 .tf + หลาย .tfvars
  • Secrets อย่า commit — ใช้ .gitignore หรือ external secret manager

ต่อไป → Environment Variables