Input Variables

📖 text • 12 นาที

Input Variables

ทำไมต้องใช้ Variables?

ถ้า hardcode ทุกค่า — เปลี่ยน environment ทีต้องแก้หลายที่:

# ❌ Hardcoded — ใช้ได้แค่ production
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.large"      # แพง!

  tags = {
    Environment = "production"    # ต้องแก้ทุกที่
  }
}

ใช้ variable — เปลี่ยนค่าที่เดียว ใช้ได้ทุก environment:

# ✅ ใช้ variable — flexible
resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = var.instance_type

  tags = {
    Environment = var.environment
  }
}

การประกาศ Variable

variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t2.micro"
}
Argument หน้าที่ จำเป็น?
description อธิบายว่า variable นี้คืออะไร แนะนำ
type ชนิดข้อมูล แนะนำ
default ค่า default (ถ้าไม่ใส่ = ต้องกำหนดค่าเสมอ) ไม่
validation กฎตรวจสอบค่า ไม่
sensitive ซ่อนค่าใน output ไม่

Variable Types

Basic Types

variable "name" {
  type = string
}

variable "port" {
  type = number
}

variable "enabled" {
  type = bool
}

Collection Types

# List — ลำดับสำคัญ
variable "availability_zones" {
  type    = list(string)
  default = ["ap-southeast-1a", "ap-southeast-1b"]
}

# Set — ลำดับไม่สำคัญ ห้ามซ้ำ
variable "allowed_ports" {
  type    = set(number)
  default = [80, 443]
}

# Map — key-value pairs
variable "tags" {
  type = map(string)
  default = {
    ManagedBy = "terraform"
    Project   = "myapp"
  }
}

Object Type

กำหนด structure แน่นอน:

variable "instance_config" {
  type = object({
    instance_type = string
    ami_id        = string
    disk_size     = number
    monitoring    = bool
  })

  default = {
    instance_type = "t2.micro"
    ami_id        = "ami-0c55b159cbfafe1f0"
    disk_size     = 20
    monitoring    = false
  }
}

ใช้งาน:

resource "aws_instance" "web" {
  ami           = var.instance_config.ami_id
  instance_type = var.instance_config.instance_type
  monitoring    = var.instance_config.monitoring
}

วิธีกำหนดค่า Variable

เรียงตามลำดับ priority (ต่ำ → สูง):

1. Default Value (priority ต่ำสุด)

variable "region" {
  default = "ap-southeast-1"
}

2. terraform.tfvars

# terraform.tfvars
region        = "us-east-1"
instance_type = "t3.small"

Terraform อ่านไฟล์นี้อัตโนมัติ

3. *.auto.tfvars

# prod.auto.tfvars
environment = "production"

ไฟล์ที่ลงท้ายด้วย .auto.tfvars จะถูกอ่านอัตโนมัติ

4. -var-file flag

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

5. Environment Variables

export TF_VAR_region="us-west-2"
export TF_VAR_instance_type="t3.large"
terraform apply

ใช้ prefix TF_VAR_ ตามด้วยชื่อ variable

6. -var flag (priority สูงสุด)

terraform apply -var="region=eu-west-1" -var="instance_type=t3.xlarge"

Validation

ตรวจสอบค่า variable:

variable "environment" {
  type        = string
  description = "Deployment environment"

  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Environment must be dev, staging, or prod."
  }
}

variable "instance_type" {
  type = string

  validation {
    condition     = can(regex("^t[23]\\.", var.instance_type))
    error_message = "Instance type must be t2.* or t3.*"
  }
}

ถ้าใส่ค่าผิด:

Error: Invalid value for variable

  Environment must be dev, staging, or prod.

Sensitive Variables

ซ่อนค่าใน CLI output:

variable "db_password" {
  type      = string
  sensitive = true
}
terraform plan
  + resource "aws_db_instance" "main" {
      + password = (sensitive value)
    }

สำคัญ: sensitive = true ซ่อนแค่ใน CLI output — ค่ายังอยู่ใน state file แบบ plain text

สรุป

Concept ตัวอย่าง
ประกาศ variable variable "name" { type = string }
ใช้ variable var.name
Default default = "value"
tfvars file terraform.tfvars (อ่านอัตโนมัติ)
CLI flag -var="name=value"
Env var TF_VAR_name=value
Validation validation { condition = ... }
Sensitive sensitive = true

บทถัดไปเราจะเรียนรู้เรื่อง Outputs และ Locals