Input Variables
Variable ทำให้ config reusable — ส่งค่าจากภายนอกแทน hard-code ใน
.tf
ทำไมต้องใช้ Variable?
# ❌ Hard-coded
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t2.micro"
subnet_id = "subnet-abc"
}
ปัญหา: deploy dev/staging/prod ต้อง copy file 3 ชุด → maintain ลำบาก
# ✅ ใช้ variable
variable "instance_type" {
type = string
default = "t2.micro"
}
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = var.instance_type
subnet_id = var.subnet_id
}
→ deploy หลาย env ใช้ config เดิม แค่เปลี่ยนค่า variable
Variable Block
variable "<NAME>" {
type = <TYPE>
default = <VALUE>
description = "..."
sensitive = false
nullable = true
validation { ... }
}
ทุก field optional ยกเว้น <NAME>
ตัวอย่างพื้นฐาน
variables.tf
variable "region" {
type = string
description = "AWS region"
default = "ap-southeast-1"
}
variable "environment" {
type = string
description = "Environment name (dev/staging/prod)"
# ไม่มี default → required
}
variable "instance_count" {
type = number
default = 1
}
variable "enable_monitoring" {
type = bool
default = false
}
variable "tags" {
type = map(string)
default = {
ManagedBy = "terraform"
}
}
ใช้ผ่าน var.<NAME>:
main.tf
provider "aws" {
region = var.region
}
resource "aws_instance" "web" {
count = var.instance_count
ami = "ami-12345"
instance_type = "t2.micro"
monitoring = var.enable_monitoring
tags = merge(var.tags, {
Name = "web-${var.environment}-${count.index}"
Environment = var.environment
})
}
ส่งค่า Variable เข้า Terraform — 6 วิธี
1. Default ใน variable block
variable "region" {
default = "ap-southeast-1"
}
2. CLI flag -var
terraform apply -var="region=us-east-1"
terraform apply -var="instance_count=3"
3. CLI flag -var-file
terraform apply -var-file="prod.tfvars"
4. ไฟล์ terraform.tfvars (auto-loaded)
terraform.tfvars
region = "us-east-1"
instance_count = 3
environment = "prod"
Terraform โหลดอัตโนมัติ
5. ไฟล์ *.auto.tfvars (auto-loaded)
dev.auto.tfvars
environment = "dev"
instance_type = "t2.micro"
โหลดอัตโนมัติเหมือน terraform.tfvars
6. Environment Variable (TF_VAR_*)
export TF_VAR_region="us-east-1"
export TF_VAR_environment="prod"
terraform apply
ลำดับความสำคัญ (Precedence)
ถ้าตั้งค่าหลายที่ — Terraform ใช้ตามลำดับนี้ (ตัวหลังชนะ):
- Default ใน variable block (ต่ำสุด)
- Environment variable (
TF_VAR_*) terraform.tfvarsterraform.tfvars.json*.auto.tfvars/*.auto.tfvars.json(alphabetical)- CLI
-var/-var-file(สูงสุด)
# CLI ชนะทุกอย่าง
terraform apply -var="region=eu-west-1"
ตัวอย่าง: Multi-Environment Setup
project/
├── main.tf
├── variables.tf
├── outputs.tf
├── dev.tfvars
├── staging.tfvars
└── prod.tfvars
dev.tfvars
environment = "dev"
instance_type = "t2.micro"
instance_count = 1
prod.tfvars
environment = "prod"
instance_type = "t3.large"
instance_count = 5
# Deploy dev
terraform apply -var-file=dev.tfvars
# Deploy prod
terraform apply -var-file=prod.tfvars
Sensitive Variables
variable "db_password" {
type = string
sensitive = true
}
→ ค่าจะ ไม่ปรากฏใน plan/apply output:
+ db_password = (sensitive value)
ส่งค่ายังไง:
# ผ่าน env (ปลอดภัยที่สุด)
export TF_VAR_db_password=$(vault kv get -field=password secret/db)
terraform apply
# ห้ามใส่ใน .tfvars ที่ commit เข้า Git!
Sensitive ≠ Secret
sensitive = true แค่ซ่อนค่าจาก output — แต่ state file ยังเก็บค่า plain text
ถ้าต้องการ secret จริงๆ ต้อง:
- ใช้ remote backend ที่ encrypted (เช่น S3 + KMS)
- ใช้ external secret manager (Vault, AWS Secrets Manager)
Description (อย่าลืม!)
variable "vpc_cidr" {
type = string
description = "CIDR block for VPC (e.g., 10.0.0.0/16)"
default = "10.0.0.0/16"
}
description แสดงใน:
terraform planตอนถาม value- Documentation ที่ generate (เช่น
terraform-docs) - Module registry
Best Practices
# ✅ ดี
variable "instance_type" {
type = string
description = "EC2 instance type. Production should use t3.large or above."
default = "t3.micro"
validation {
condition = can(regex("^t[23]\\.", var.instance_type))
error_message = "Only t2 and t3 instance types are allowed."
}
}
# ❌ ไม่ดี
variable "x" {} # ไม่มี type, description, default
variable "instance" { default = "" } # ใช้ name "instance" กว้างไป
สรุป
- Variable ทำให้ config reusable ข้าม environment
- 6 วิธีส่งค่า: default,
-var,-var-file,terraform.tfvars,*.auto.tfvars,TF_VAR_* - ใช้
sensitive = trueซ่อน secrets จาก output - ใส่
descriptionทุก variable - Multi-env pattern: 1
.tf+ หลาย.tfvars
ต่อไป → Type Constraints