Terraform กับ Cloudflare นำการจัดการ Infrastructure as Code (IaC) ไปสู่ระดับใหม่ ช่วยให้คุณจัดการ DNS Records, Firewall Rules, Page Rules, และการตั้งค่าทั้งหมดของ Cloudflare ด้วยโค้ด แทนที่จะกดปุ่มในแดชบอร์ด ทำให้การจัดการ Infrastructure กลายเป็นเรื่องง่าย ปลอดภัย และสามารถควบคุมเวอร์ชันได้
ทำไมต้อง Terraform + Cloudflare
Infrastructure as Code (IaC): ทุกอย่างเป็นโค้ด ทำให้ง่ายต่อการจัดการ ถูกเอนกประสงค์และควบคุมเวอร์ชัน (Git versioning)
ทำซ้ำได้: เพียงรัน Terraform apply ครั้งเดียว และสามารถสร้าง Environment ใหม่ได้ (Dev, Staging, Prod) โดยไม่ต้องคลิกปุ่มในแดชบอร์ด
ควบคุม Git: ผู้ใช้สามารถตรวจสอบว่าใครเปลี่ยนการตั้งค่า Cloudflare ในไหน และเมื่อไร ผ่าน Git history
Automation: รวม Terraform เข้ากับ CI/CD Pipeline ทำให้การปรับใช้อัตโนมัติ
ติดตั้ง Terraform และ Cloudflare Provider
1. ติดตั้ง Terraform: https://www.terraform.io/downloads.html
2. สร้าง Directory ใหม่:
mkdir terraform-cloudflare
cd terraform-cloudflare
3. สร้างไฟล์ main.tf และตั้งค่า Provider:
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
4. สร้างไฟล์ variables.tf:
variable "cloudflare_api_token" {
type = string
sensitive = true
}
variable "cloudflare_zone_id" {
type = string
}
variable "domain" {
type = string
}
5. สร้างไฟล์ terraform.tfvars (ไม่ให้เป็น Public):
cloudflare_api_token = "YOUR_API_TOKEN"
cloudflare_zone_id = "YOUR_ZONE_ID"
domain = "example.com"
ตัวอย่าง: จัดการ DNS Records
resource "cloudflare_record" "www" {
zone_id = var.cloudflare_zone_id
name = "www"
type = "CNAME"
value = "example.com"
ttl = 3600
}
resource "cloudflare_record" "mail" {
zone_id = var.cloudflare_zone_id
name = "mail"
type = "A"
value = "192.0.2.1"
ttl = 3600
proxied = false # ไม่ proxy ผ่าน Cloudflare
}
resource "cloudflare_record" "api" {
zone_id = var.cloudflare_zone_id
name = "api"
type = "A"
value = "192.0.2.2"
ttl = 3600
proxied = true # proxy ผ่าน Cloudflare (สีส้ม)
}
ตัวอย่าง: Firewall Rules
resource "cloudflare_firewall_rule" "block_china" {
zone_id = var.cloudflare_zone_id
description = "Block China traffic"
filter_id = cloudflare_firewall_filter.china.id
action = "block"
}
resource "cloudflare_firewall_filter" "china" {
zone_id = var.cloudflare_zone_id
description = "China country filter"
expression = "(ip.geoip.country eq \"CN\") and (cf.bot_management.score < 50)"
}
# Rate Limiting
resource "cloudflare_rate_limit" "login" {
zone_id = var.cloudflare_zone_id
disabled = false
threshold = 5
period = 60
match {
request {
url {
path {
matches = "/login"
}
}
}
}
action {
response_code = 429
}
}
ตัวอย่าง: Page Rules (แบบเก่า)
# ใช้ Cache Rules แทน (แบบใหม่)
resource "cloudflare_cache_rules" "static" {
zone_id = var.cloudflare_zone_id
rules {
description = "Cache static files"
expression = "(cf.mime_type matches \"image.*\" or cf.mime_type eq \"text/css\" or cf.mime_type eq \"application/javascript\")"
action = "set_cache_settings"
action_parameters {
cache = true
ttl = 86400
}
}
}
# Page Rules (ถ้ายังใช้)
resource "cloudflare_page_rule" "cache_everything" {
zone_id = var.cloudflare_zone_id
target = "example.com/cache/*"
actions {
cache_level = "cache_everything"
}
}
resource "cloudflare_page_rule" "bypass_cache" {
zone_id = var.cloudflare_zone_id
target = "example.com/admin/*"
actions {
cache_level = "bypass"
}
}
รัน Terraform
1. ตั้งค่า API Token:
export TF_VAR_cloudflare_api_token="YOUR_API_TOKEN"
export TF_VAR_cloudflare_zone_id="YOUR_ZONE_ID"
export TF_VAR_domain="example.com"
2. ตรวจสอบสิ่งที่จะเปลี่ยนแปลง:
terraform plan
3. ใช้การเปลี่ยนแปลง:
terraform apply
4. ลบทรัพยากร (ถ้าต้องการ):
terraform destroy
5. ดูสถานะปัจจุบัน:
terraform show
การใช้ Terraform Modules
สร้าง Directory: modules/cloudflare_dns
# modules/cloudflare_dns/main.tf
resource "cloudflare_record" "main" {
zone_id = var.zone_id
name = var.subdomain
type = var.type
value = var.value
ttl = var.ttl
proxied = var.proxied
}
# modules/cloudflare_dns/variables.tf
variable "zone_id" {}
variable "subdomain" {}
variable "type" {}
variable "value" {}
variable "ttl" { default = 3600 }
variable "proxied" { default = false }
# main.tf
module "dns_www" {
source = "./modules/cloudflare_dns"
zone_id = var.cloudflare_zone_id
subdomain = "www"
type = "CNAME"
value = "example.com"
proxied = true
}
module "dns_mail" {
source = "./modules/cloudflare_dns"
zone_id = var.cloudflare_zone_id
subdomain = "mail"
type = "A"
value = "192.0.2.1"
proxied = false
}
เทคนิคขั้นสูง: Remote State
# backend.tf - เก็บ State ใน AWS S3
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "cloudflare/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
# หรือใช้ Terraform Cloud
terraform {
cloud {
organization = "my-org"
workspaces {
name = "cloudflare-prod"
}
}
}
สรุป
Terraform กับ Cloudflare ทำให้การจัดการ Infrastructure กลายเป็นเรื่องง่าย ปลอดภัย และสามารถทำซ้ำได้ ด้วยการเขียน DNS Records, Firewall Rules, และการตั้งค่าทั้งหมดเป็นโค้ด คุณสามารถจัดการ Cloudflare ได้เสมือนเป็นส่วนหนึ่งของสภาพแวดล้อมการพัฒนา รวมกับการใช้ VPS เป็น Origin ทำให้ Infrastructure ของคุณเป็นระบบอัตโนมัติและน่าเชื่อถือ

