Terraform Modules คืออะไร
📖 text • 10 นาทีTerraform Modules คืออะไร
Module = Package ของ Terraform
Module คือ กลุ่มของ .tf files ที่ใช้งานร่วมกัน — เหมือน function ในโปรแกรมมิ่ง
ไม่มี module:
main.tf ← 500 บรรทัด, resource ปนกันหมด
มี module:
modules/
├── networking/ ← VPC, Subnet, Route Table
├── compute/ ← EC2, Auto Scaling
└── database/ ← RDS, ElastiCache
ทำไมต้องใช้ Module?
1. Reusability — ใช้ซ้ำได้
# สร้าง web server สำหรับ dev
module "web_dev" {
source = "./modules/web-server"
environment = "dev"
instance_type = "t2.micro"
}
# สร้าง web server สำหรับ prod — code เดิม ค่าต่าง
module "web_prod" {
source = "./modules/web-server"
environment = "prod"
instance_type = "t3.large"
}
2. Organization — จัดระเบียบ
แทนที่จะมี main.tf ยาว 500 บรรทัด:
project/
├── main.tf ← เรียกใช้ modules
├── modules/
│ ├── networking/ ← VPC, Subnets
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ └── outputs.tf
│ └── compute/ ← EC2, Security Groups
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
3. Encapsulation — ซ่อนความซับซ้อน
User ของ module ไม่ต้องรู้รายละเอียดข้างใน:
# User แค่กำหนด input — module จัดการเอง
module "vpc" {
source = "./modules/networking"
cidr_block = "10.0.0.0/16"
az_count = 3
}
# ไม่ต้องรู้ว่าข้างในสร้าง subnet, route table, NAT gateway ยังไง
ประเภทของ Module
Root Module
ทุกโปรเจค Terraform มี root module คือ directory ที่รัน terraform apply:
my-project/ ← นี่คือ root module
├── main.tf
├── variables.tf
└── outputs.tf
Child Module
Module ที่ถูกเรียกใช้จาก root module:
# root module เรียก child module
module "web" {
source = "./modules/web-server" # ← child module
}
Module Sources
Module มาจากหลายที่:
# Local path
module "web" {
source = "./modules/web-server"
}
# Terraform Registry
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
}
# GitHub
module "web" {
source = "github.com/myorg/terraform-web-server"
}
# S3 Bucket
module "web" {
source = "s3::https://s3.amazonaws.com/my-modules/web-server.zip"
}
Module Structure
Module ที่ดีมีโครงสร้างแบบนี้:
modules/web-server/
├── main.tf # Resource definitions
├── variables.tf # Input variables (interface)
├── outputs.tf # Output values (return values)
└── README.md # Documentation
Module เหมือน Function
Function:
function createWebServer(instanceType, environment) → { ip, url }
Module:
module "web" {
source = "./modules/web-server"
instance_type = "t2.micro" ← parameters (variables)
environment = "dev"
}
output: module.web.public_ip ← return values (outputs)
การเรียกใช้ Module
module "web_server" {
source = "./modules/web-server"
# ส่งค่าให้ module (= input variables ของ module)
instance_type = "t2.micro"
environment = "dev"
subnet_id = aws_subnet.public.id
}
# ใช้ output ของ module
output "web_ip" {
value = module.web_server.public_ip
}
หลังเพิ่ม/เปลี่ยน module source:
terraform init # ดาวน์โหลด module
terraform plan # ดูการเปลี่ยนแปลง
terraform apply # สร้าง resource
สรุป
| Concept | คำอธิบาย |
|---|---|
| Module | กลุ่มของ .tf files ที่ใช้ร่วมกัน |
| Root Module | Directory ที่รัน terraform apply |
| Child Module | Module ที่ถูกเรียกใช้ |
| Source | ที่มาของ module (local, registry, git) |
| Input | Variables ที่ module รับ |
| Output | ค่าที่ module ส่งออกมา |
บทถัดไปเราจะเขียน module ของตัวเอง