HCL Syntax พื้นฐาน

📖 text • 12 นาที

HCL Syntax พื้นฐาน

HCL (HashiCorp Configuration Language) คือภาษาที่ Terraform ใช้ ออกแบบมาให้อ่านง่ายทั้งคนและเครื่อง

Block

ทุกอย่างใน HCL เขียนเป็น block:

block_type "label_1" "label_2" {
  argument = value
}

ตัวอย่างจริง:

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
  • resource = block type
  • "aws_instance" = label 1 (ประเภท resource)
  • "web" = label 2 (ชื่อที่เราตั้ง)

Data Types

String

name = "hello"

Number

count = 3
port  = 8080

Boolean

enabled = true
public  = false

List (Array)

availability_zones = ["ap-southeast-1a", "ap-southeast-1b"]
ports              = [80, 443, 8080]

เข้าถึง element:

first_az = var.availability_zones[0]  # "ap-southeast-1a"

Map (Object)

tags = {
  Name        = "web-server"
  Environment = "production"
}

เข้าถึง value:

env = var.tags["Environment"]  # "production"

String Interpolation

ใส่ค่าตัวแปรใน string ด้วย ${}:

name = "web-${var.environment}"
# ถ้า var.environment = "prod" → name = "web-prod"

Heredoc (Multi-line String)

description = <<-EOT
  This is a web server
  running on port 80
  in ${var.region}
EOT

Comments

# Single line comment

// Also single line comment

/*
  Multi-line
  comment
*/

Meta-Arguments

Meta-arguments ใช้ได้กับทุก resource:

count — สร้างหลาย resource

resource "aws_instance" "web" {
  count         = 3
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "web-${count.index}"  # web-0, web-1, web-2
  }
}

for_each — สร้างจาก map/set

resource "aws_s3_bucket" "buckets" {
  for_each = toset(["logs", "data", "backups"])

  bucket = "myapp-${each.key}"
}

depends_on — กำหนดลำดับ

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  depends_on = [aws_s3_bucket.my_bucket]
}

Expressions

Conditional

instance_type = var.environment == "prod" ? "t3.large" : "t3.micro"

For Expression

# สร้าง list ของ uppercase names
upper_names = [for name in var.names : upper(name)]

# สร้าง map จาก list
name_map = { for name in var.names : name => upper(name) }

Built-in Functions

Terraform มี function ให้ใช้มากมาย:

# String
lower("HELLO")           # "hello"
upper("hello")           # "HELLO"
replace("hello", "l", "r") # "herro"
join("-", ["a", "b", "c"]) # "a-b-c"

# Numeric
max(1, 2, 3)              # 3
min(1, 2, 3)              # 1

# Collection
length(["a", "b", "c"])   # 3
contains(["a", "b"], "a") # true
merge({a=1}, {b=2})       # {a=1, b=2}

# File
file("script.sh")         # อ่านไฟล์เป็น string
templatefile("user_data.tpl", { port = 8080 })

Tip: ทดสอบ function ได้ใน terraform console

$ terraform console
> upper("hello")
"HELLO"
> length([1,2,3])
3

สรุป

Concept ตัวอย่าง
Block resource "type" "name" { ... }
String "hello"
Number 42
Boolean true
List ["a", "b"]
Map { key = "value" }
Interpolation "${var.name}"
Conditional cond ? val1 : val2
Function upper("hello")

บทถัดไปเราจะเจาะลึกเรื่อง Provider