Remote State Backend
📖 text • 12 นาทีRemote State Backend
Backend คืออะไร?
Backend คือที่เก็บ state file — บอก Terraform ว่าจะเก็บ state ไว้ที่ไหน
S3 Backend (แนะนำสำหรับ AWS)
Setup ยอดนิยมที่สุด: S3 + DynamoDB
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "ap-southeast-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
| Argument | หน้าที่ |
|---|---|
bucket |
ชื่อ S3 bucket เก็บ state |
key |
path ของ state file ใน bucket |
region |
region ของ S3 bucket |
dynamodb_table |
ตาราง DynamoDB สำหรับ state locking |
encrypt |
เข้ารหัส state file (AES-256) |
โครงสร้าง State ใน S3
s3://my-terraform-state/
├── prod/terraform.tfstate
├── staging/terraform.tfstate
└── dev/terraform.tfstate
แยก state ตาม environment ด้วย key ที่ต่างกัน
Google Cloud Storage Backend
terraform {
backend "gcs" {
bucket = "my-terraform-state"
prefix = "prod"
}
}
Azure Blob Storage Backend
terraform {
backend "azurerm" {
resource_group_name = "terraform-rg"
storage_account_name = "tfstate12345"
container_name = "tfstate"
key = "prod.terraform.tfstate"
}
}
สร้าง S3 Backend ด้วยมือ
ก่อนใช้ S3 backend ต้องสร้าง S3 bucket และ DynamoDB table ก่อน:
1. สร้าง S3 Bucket
aws s3api create-bucket \
--bucket my-terraform-state \
--region ap-southeast-1 \
--create-bucket-configuration LocationConstraint=ap-southeast-1
2. เปิด Versioning
aws s3api put-bucket-versioning \
--bucket my-terraform-state \
--versioning-configuration Status=Enabled
Versioning ทำให้สามารถ recover state file เวอร์ชันก่อนหน้าได้
3. เปิด Encryption
aws s3api put-bucket-encryption \
--bucket my-terraform-state \
--server-side-encryption-configuration '{
"Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]
}'
4. สร้าง DynamoDB Table
aws dynamodb create-table \
--table-name terraform-locks \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH \
--billing-mode PAY_PER_REQUEST \
--region ap-southeast-1
หมายเหตุ: DynamoDB table ต้องมี primary key ชื่อ
LockID(ชนิด String)
Migrate จาก Local ไป Remote
ถ้ามี state อยู่แล้วบนเครื่อง:
1. เพิ่ม backend configuration
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "myproject/terraform.tfstate"
region = "ap-southeast-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
2. Init ใหม่
terraform init
Terraform จะถาม:
Do you want to copy existing state to the new backend?
Enter a value: yes
State จะถูกย้ายไป S3 อัตโนมัติ
3. ตรวจสอบ
# ดูว่า state ยังทำงานปกติ
terraform plan
# ลบ local state (optional)
rm terraform.tfstate terraform.tfstate.backup
Data Source: terraform_remote_state
อ่าน state ของโปรเจคอื่น — ใช้แชร์ข้อมูลระหว่างโปรเจค:
# โปรเจค Network (สร้าง VPC)
output "vpc_id" {
value = aws_vpc.main.id
}
# โปรเจค App (ใช้ VPC จากโปรเจค Network)
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "my-terraform-state"
key = "network/terraform.tfstate"
region = "ap-southeast-1"
}
}
resource "aws_instance" "app" {
subnet_id = data.terraform_remote_state.network.outputs.vpc_id
# ...
}
Best Practices
- เปิด versioning — recover ได้ถ้า state เสีย
- เปิด encryption — state มี sensitive data
- ใช้ state locking — ป้องกัน concurrent access
- แยก state ตาม environment — prod/staging/dev ควรมี state คนละไฟล์
- จำกัด access — ไม่ใช่ทุกคนควรเข้าถึง state ได้
สรุป
| Concept | คำอธิบาย |
|---|---|
| Backend | กำหนดที่เก็บ state file |
| S3 Backend | S3 (เก็บ state) + DynamoDB (locking) |
| Migration | terraform init ย้าย state อัตโนมัติ |
| Remote State Data | อ่าน output จากโปรเจคอื่น |
| Versioning | เปิดเพื่อ recover state ได้ |
บทถัดไปเราจะเรียนรู้ State Commands ที่ใช้บ่อย