terraform apply
terraform apply= ลงมือสร้าง/แก้/ลบ infrastructure จริง — ตามที่ plan แสดง
Apply ทำอะไร?
- รัน plan ใหม่ (ถ้าไม่ได้ส่ง plan file)
- แสดง diff ให้ดู
- ถาม
Do you want to perform these actions? [yes/no] - ถ้า
yes→ เรียก provider API สร้าง/แก้/ลบ resource - Update state file
- แสดง outputs
Usage
แบบมาตรฐาน (interactive)
terraform apply
...
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
aws_s3_bucket.data: Creating...
aws_s3_bucket.data: Creation complete after 3s [id=my-data-bucket]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
bucket_name = "my-data-bucket"
แบบ Auto-Approve (ระวังใช้)
terraform apply -auto-approve
→ ไม่ถาม yes/no — ใช้ใน CI/CD
ใช้ -auto-approve เฉพาะ:
- ✅ ใน CI/CD ที่ผ่าน plan review แล้ว
- ✅ ใน dev environment
- ❌ ห้ามใช้ใน prod local terminal (เผลอกด!)
Apply Plan File (Recommended)
# 1. Plan
terraform plan -out=tfplan
# 2. Review
# 3. Apply เฉพาะ plan
terraform apply tfplan
→ ไม่ต้องคำนวณ plan ใหม่ → ไม่มี surprise
Useful Flags
-var / -var-file
terraform apply -var="environment=prod"
terraform apply -var-file="prod.tfvars"
-target
terraform apply -target="aws_s3_bucket.data"
(เหมือน plan -target — ใช้ระวัง)
-replace
# Force replace resource
terraform apply -replace="aws_instance.web"
แทน terraform taint ของเก่า
-refresh-only
# Update state ให้ตรง reality (ไม่ apply config)
terraform apply -refresh-only
ใช้ accept drift
-parallelism
# Default = 10
terraform apply -parallelism=20
ระวัง: บาง provider มี rate limit
Lock Mechanism
ตอน apply Terraform จะ lock state ป้องกันคนอื่นรันพร้อมกัน
Local State
.terraform.tfstate.lock.info
S3 Backend (with DynamoDB)
terraform {
backend "s3" {
bucket = "my-tfstate"
key = "terraform.tfstate"
region = "ap-southeast-1"
dynamodb_table = "terraform-locks" # ← lock ที่นี่
}
}
ดูเพิ่มใน State Locking
Output หลัง Apply
$ terraform apply
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
bucket_name = "my-data-bucket"
vpc_id = "vpc-0a1b2c3d4e5f"
ดู outputs ทีหลัง:
terraform output
terraform output -json
terraform output -raw bucket_name
Error Handling
Partial Apply
ถ้า apply fail กลางทาง — Terraform บันทึก resource ที่สร้างเสร็จไว้ใน state
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
Error creating resource X: ...
Apply complete with errors. Resources: 5 added, 0 changed, 0 destroyed.
→ run terraform apply อีกครั้ง — สร้างเฉพาะที่ยังไม่เสร็จ
Rollback?
Terraform ไม่มี rollback อัตโนมัติ — ถ้า apply ผิด:
- แก้ config กลับ
terraform applyอีกครั้ง
หรือถ้า resource เพิ่งสร้าง:
terraform destroy -target="aws_instance.bad"
Apply Stages
Phase 1: Refresh
Refreshing state...
aws_vpc.main: Refreshing state... [id=vpc-12345]
อ่าน state ปัจจุบัน
Phase 2: Diff
แสดง changes ที่จะทำ
Phase 3: Confirm
ถาม yes (skip ถ้า -auto-approve)
Phase 4: Apply
aws_s3_bucket.data: Creating...
aws_s3_bucket.data: Creation complete after 3s
เรียก provider API ตามลำดับ dependency
Phase 5: Output
Apply complete!
Outputs: ...
Production Workflow
Manual Apply
# 1. Plan
terraform plan -out=tfplan -var-file=prod.tfvars
# 2. Review (read carefully!)
terraform show tfplan
# 3. Apply
terraform apply tfplan
CI/CD Apply
on:
push:
branches: [main]
paths: ["**.tf", "**.tfvars"]
jobs:
apply:
runs-on: ubuntu-latest
environment: production # require approval
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- run: terraform init
- run: terraform plan -out=tfplan
- run: terraform apply -auto-approve tfplan
ตัวอย่าง: Hello World End-to-End
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "ap-southeast-1"
}
resource "aws_s3_bucket" "hello" {
bucket = "tf-hello-${random_id.suffix.hex}"
}
resource "random_id" "suffix" {
byte_length = 4
}
output "bucket_name" {
value = aws_s3_bucket.hello.id
}
# Init
terraform init
# Format + Validate
terraform fmt
terraform validate
# Plan
terraform plan -out=tfplan
# Apply
terraform apply tfplan
# ดู output
terraform output bucket_name
# ลบทิ้ง
terraform destroy
Common Errors
Error: configuration not initialized
Error: Module not installed
This module is not yet installed. Run "terraform init"
แก้: terraform init
Error: AccessDenied
Error: error creating S3 bucket: AccessDenied
แก้: เช็ค IAM permission
Error: bucket already exists
Error: BucketAlreadyExists
แก้: เปลี่ยน bucket name (S3 unique ทั่ว AWS)
Error: state lock
Error: Error acquiring the state lock
แก้: รอ apply อื่นเสร็จ หรือ terraform force-unlock <LOCK_ID>
สรุป
terraform apply= ลงมือสร้าง/แก้/ลบ infrastructure- ขั้นตอน: refresh → diff → confirm → apply → output
- ใช้
-out=tfplan+apply tfplan= no surprise -auto-approveใน CI/CD เท่านั้น- State lock ป้องกัน concurrent apply
- ไม่มี rollback — ต้องแก้ + apply ใหม่
ต่อไป → Section 9: Clean Up