ใช้ Module จาก Registry
📖 text • 8 นาทีใช้ Module จาก Registry
Terraform Registry
Terraform Registry คือ marketplace ของ module สำเร็จรูป — ไม่ต้องเขียนเอง
URL: registry.terraform.io
ใช้ Registry Module
ตัวอย่าง: VPC Module
สร้าง VPC พร้อม subnets, NAT gateway, route tables ด้วย code ไม่กี่บรรทัด:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["ap-southeast-1a", "ap-southeast-1b"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
enable_nat_gateway = true
single_nat_gateway = true
tags = {
Environment = "prod"
ManagedBy = "terraform"
}
}
ถ้าเขียนเอง ต้องสร้าง: VPC, Subnets, Internet Gateway, NAT Gateway, Route Tables, Route Table Associations — กว่า 100 บรรทัด
ตัวอย่าง: Security Group Module
module "web_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "5.0.0"
name = "web-sg"
description = "Security group for web servers"
vpc_id = module.vpc.vpc_id
ingress_with_cidr_blocks = [
{
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = "0.0.0.0/0"
description = "HTTP"
},
{
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = "0.0.0.0/0"
description = "HTTPS"
},
]
egress_rules = ["all-all"]
}
ตัวอย่าง: S3 Module
module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "4.0.0"
bucket = "my-app-data"
versioning = {
enabled = true
}
server_side_encryption_configuration = {
rule = {
apply_server_side_encryption_by_default = {
sse_algorithm = "AES256"
}
}
}
block_public_access = true
}
Module Source Format
source = "<NAMESPACE>/<NAME>/<PROVIDER>"
version = "<VERSION_CONSTRAINT>"
terraform-aws-modules= namespace (organization)vpc= module nameaws= providerversion= ใช้ constraint เหมือน provider (~>,>=, etc.)
Module ยอดนิยม
| Module | หน้าที่ | Resource ที่สร้าง |
|---|---|---|
vpc/aws |
สร้าง VPC | VPC, Subnets, NAT, Routes |
security-group/aws |
สร้าง SG | Security Groups, Rules |
ec2-instance/aws |
สร้าง EC2 | EC2, EBS, ENI |
rds/aws |
สร้าง RDS | RDS, Subnet Group, Parameters |
s3-bucket/aws |
สร้าง S3 | S3, Policies, Lifecycle |
eks/aws |
สร้าง EKS | EKS Cluster, Node Groups |
alb/aws |
สร้าง ALB | ALB, Listeners, Target Groups |
วิธีเลือก Module
ดูก่อนใช้
- Verified badge — module จาก HashiCorp partners (มีเครื่องหมาย ✓)
- Downloads — จำนวนคนใช้งาน
- Stars/Issues — ดูจาก GitHub repository
- Documentation — มี examples และ README ดีไหม
- Maintained — อัพเดทล่าสุดเมื่อไร
Red Flags
- ไม่มี version tags
- ไม่มีการอัพเดทมานาน (> 1 ปี)
- ไม่มี documentation
- Issues เปิดค้างเยอะ
Module Version Pinning
# ✅ Pin version — safe
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
}
# ✅ Pessimistic constraint — ได้ patch updates
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.0"
}
# ❌ ไม่ pin version — อันตราย!
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
}
Best Practice: Pin version เสมอ — ป้องกัน breaking changes
เมื่อไรเขียนเอง vs ใช้ Registry?
| สถานการณ์ | แนะนำ |
|---|---|
| Standard infrastructure (VPC, SG, S3) | ใช้ Registry |
| Company-specific patterns | เขียนเอง |
| ต้องการ customization เยอะ | เขียนเอง |
| เริ่มต้น/prototype | ใช้ Registry |
| ต้องการควบคุมทุก detail | เขียนเอง |
สรุป
| Concept | คำอธิบาย |
|---|---|
| Terraform Registry | marketplace ของ module สำเร็จรูป |
| Source format | namespace/name/provider |
| Version pinning | ใช้ version = "~> 5.0" เสมอ |
| Verified modules | มี badge ✓ จาก HashiCorp |
| เขียนเอง vs Registry | standard = registry, custom = เขียนเอง |
บทถัดไปเรามา Lab สร้าง reusable module ของตัวเอง