Skip to main content

Resources Basics

Resource = building block หลักของ Terraform — ทุกอย่างที่ Terraform "สร้าง" เป็น resource ทั้งหมด

Resource Block Syntax

resource "<TYPE>" "<NAME>" {
<ARGUMENT> = <VALUE>
...
}
  • TYPE — ชนิดของ resource จาก provider (เช่น aws_instance, aws_s3_bucket)
  • NAME — ชื่อ local ใน Terraform — ใช้อ้างอิงเท่านั้น ไม่ส่งไป cloud

ตัวอย่างพื้นฐาน

resource "aws_s3_bucket" "data" {
bucket = "my-data-bucket-12345"
}

อ่านยังไง:

  • resource = block type
  • aws_s3_bucket = resource type (จาก AWS provider)
  • data = local name (อ้างอิงด้วย aws_s3_bucket.data)
  • bucket = "..." = argument (ชื่อ bucket จริงใน AWS)

Reference Resource

resource "aws_s3_bucket" "data" {
bucket = "my-data-bucket-12345"
}

# อ้างอิง attribute
resource "aws_s3_bucket_versioning" "data_versioning" {
bucket = aws_s3_bucket.data.id # ← reference!

versioning_configuration {
status = "Enabled"
}
}

Pattern: <TYPE>.<NAME>.<ATTRIBUTE>

Resource Attributes 2 ประเภท

1. Arguments (Input)

สิ่งที่เรากำหนด — เช่น bucket, instance_type, tags

2. Attributes (Output / Computed)

สิ่งที่ AWS คืนกลับมา — เช่น id, arn, public_ip

resource "aws_instance" "web" {
ami = "ami-12345" # argument (input)
instance_type = "t2.micro" # argument
# public_ip = ... # ← computed by AWS, ไม่ต้องระบุ
}

output "ip" {
value = aws_instance.web.public_ip # ← attribute (output)
}

ดูว่า resource ตัวไหนมี attribute อะไรบ้าง → อ่าน Terraform Registry docs

ตัวอย่าง: AWS Resources หลากหลาย

EC2 Instance

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

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

S3 Bucket

resource "aws_s3_bucket" "logs" {
bucket = "my-app-logs-bucket"
}

resource "aws_s3_bucket_versioning" "logs" {
bucket = aws_s3_bucket.logs.id
versioning_configuration {
status = "Enabled"
}
}

VPC

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true

tags = { Name = "main-vpc" }
}

Security Group

resource "aws_security_group" "web" {
name = "web-sg"
description = "Allow HTTP traffic"
vpc_id = aws_vpc.main.id

ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

IAM Role

resource "aws_iam_role" "lambda" {
name = "my-lambda-role"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}]
})
}

Resource Naming Conventions

# ✅ snake_case + descriptive
resource "aws_instance" "web_server" {}
resource "aws_s3_bucket" "user_uploads" {}

# ❌ camelCase (ไม่ใช่ Terraform style)
resource "aws_instance" "webServer" {}

# ❌ ใส่ type ซ้ำใน name
resource "aws_instance" "web_instance" {} # ซ้ำ "instance"

# ❌ generic เกินไป
resource "aws_instance" "main" {} # main อะไร?
Naming Tips
  • ใช้ snake_case เสมอ
  • ชื่อ บอกหน้าที่ ไม่ใช่ type (web_server ไม่ใช่ web_instance)
  • ถ้ามีตัวเดียวใช้ this หรือ main ก็พอ
  • ถ้าหลายตัว ใส่ identifier (web_us, web_eu)

Multi-Resource Pattern

count

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

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

for_each

resource "aws_instance" "servers" {
for_each = {
web = "t2.micro"
api = "t2.small"
db = "t2.medium"
}

ami = "ami-12345"
instance_type = each.value

tags = {
Name = each.key # web, api, db
}
}

ลึกขึ้นใน Meta Arguments

Resource Dependencies

Terraform คำนวณ dependency อัตโนมัติจาก reference:

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}

# Terraform รู้ว่า subnet ต้องรอ vpc สร้างเสร็จก่อน (จาก aws_vpc.main.id)
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
}

ถ้าไม่มี reference แต่ยังต้องการ ordering → ใช้ depends_on (อ่านใน Meta Arguments)

Workflow

# 1. เขียน resource ใน .tf
# 2. Format
terraform fmt

# 3. Validate
terraform validate

# 4. Plan
terraform plan

# 5. Apply
terraform apply

สรุป

  • Resource = ทุกอย่างที่ Terraform จัดการ
  • Syntax: resource "<TYPE>" "<NAME>" { ... }
  • Reference: <TYPE>.<NAME>.<ATTRIBUTE>
  • 2 ประเภท attribute: input (เรากำหนด) + computed (cloud คืนมา)
  • Multi-resource: count (numeric) หรือ for_each (key-value)
  • Dependencies คำนวณอัตโนมัติจาก reference

ต่อไป → Resource Behavior