Danh sách bài viết trong series Terraform Associate (003)
- Bài 0: Giới Thiệu Series Và Tổng Quan Về Chứng Chỉ Terraform Associate (003)
- Bài 1: Terraform Là Gì? Tổng Quan Và Vai Trò Trong IaC
- Bài 2: Cài Đặt Terraform Trên Các Hệ Điều Hành
- Bài 3: Terraform Workflow: Init, Plan, Apply, Destroy
- Bải 4: Terraform CLI Nâng Cao: Lệnh Taint, Import, Refresh
- Bài 5: Quản Lý Provider Trong Terraform
- Bài 6: Quản Lý Resource Trong Terraform
- Bài 7: Sử Dụng Biến (Variables) Trong Terraform
- Bài 8: Sử Dụng Output Trong Terraform
- Bài 9: Hiểu Terraform State Quản Lý Trạng Thái Hạ Tầng
- Bài 10: Sử Dụng Remote State Với Backend Như S3 Trong Terraform
- Bài 11: Terraform Workspace Quản Lý Nhiều Môi Trường
- Bài 12: Terraform Module: Mô-đun Hóa Cấu Hình
- Bài 13: Terraform Data Sources Truy Vấn Thông Tin Hạ Tầng
- Bài 14: Quản Lý Vòng Đời Tài Nguyên Với Terraform
- Bài 15: Tự Động Hóa Và Tích Hợp CI/CD Với Terraform
- Bài 16: Xử Lý Lỗi Và Debug Trong Terraform
- Bài 17: Tối Ưu Hóa Hiệu Suất Với Terraform
- Bài 18: Bảo Mật Cấu Hình Terraform
- Bài 19: Triển Khai Hạ Tầng Đa Vùng Với Terraform
- Bài 20: Triển Khai Hạ Tầng Serverless Với Terraform
- Bài 21: Triển Khai Hạ Tầng Container Với Terraform (ECS, EKS)
- Bài 22: Triển Khai Hạ Tầng Multi-Cloud Với Terraform
- Bài 23: Tổng Kết Series Và Chuẩn Bị Cho Kỳ Thi Terraform Associate (003)
Terraform Workspace Là Gì?

Terraform Workspace là một tính năng cho phép bạn quản lý nhiều môi trường (VD: dev, staging, prod) trong cùng một cấu hình Terraform mà không cần sao chép mã. Mỗi workspace có một file trạng thái (terraform.tfstate
) riêng, giúp tách biệt trạng thái hạ tầng giữa các môi trường. Theo mặc định, Terraform khởi tạo workspace default
, và bạn có thể tạo thêm các workspace khác như dev
, prod
. Workspace rất hữu ích trong việc triển khai hạ tầng đa môi trường, một kỹ năng được kiểm tra trong kỳ thi Terraform Associate (003), liên quan đến mục tiêu 6 (Implement and Maintain State).
Vai Trò Của Workspace Trong Quản Lý Hạ Tầng
Workspace đóng vai trò quan trọng trong quản lý hạ tầng:
- Tách biệt môi trường: Mỗi workspace lưu trạng thái riêng, tránh xung đột giữa các môi trường (VD: dev không ảnh hưởng đến prod).
- Tái sử dụng mã: Dùng cùng file
.tf
cho nhiều môi trường, giảm trùng lặp. - Quản lý trạng thái: Kết hợp với remote state để lưu trữ trạng thái an toàn.
- Hỗ trợ CI/CD: Dễ dàng triển khai qua pipeline với workspace khác nhau.
- Tăng tính linh hoạt: Dùng biến kết hợp với workspace để tùy chỉnh tài nguyên (VD:
t2.micro
cho dev,t2.large
cho prod).
Quản Lý Terraform Workspace
Tạo Và Chuyển Đổi Workspace
- Input: Tạo workspace mới.
terraform workspace new dev
- Output:
Created and switched to workspace "dev"!
- Input: Tạo workspace
prod
.terraform workspace new prod
- Output:
Created and switched to workspace "prod"!
- Input: Chuyển đổi giữa các workspace.
terraform workspace select dev
- Output:
Switched to workspace "dev".
Xem Danh Sách Workspace
- Input:
terraform workspace list
- Output:
default * dev prod
- Ý nghĩa: Dấu
*
chỉ ra workspace đang hoạt động (dev
).
Xóa Workspace
- Input: Xóa workspace
prod
.terraform workspace select default terraform workspace delete prod
- Output:
Switched to workspace "default". Deleted workspace "prod".
- Lưu ý: Không thể xóa workspace đang hoạt động, phải chuyển sang workspace khác trước.
Sử Dụng Workspace Với Remote State
Workspace thường được kết hợp với remote state để quản lý trạng thái an toàn. Mỗi workspace sẽ có một file state riêng trên backend (VD: S3).
- File
main.tf
(với S3 backend):terraform { backend "s3" { bucket = "my-terraform-state" key = "state/${terraform.workspace}/terraform.tfstate" region = "us-east-1" dynamodb_table = "terraform-locks" encrypt = true } }
- Ý nghĩa:
${terraform.workspace}
đảm bảo state file được lưu theo tên workspace (VD:state/dev/terraform.tfstate
,state/prod/terraform.tfstate
).
Ví Dụ Thực Tế: Quản Lý Nhiều Môi Trường Với Workspace
-
File
variables.tf
:variable "instance_type" { type = string description = "EC2 instance type" default = "t2.micro" } variable "instance_count" { type = number description = "Number of EC2 instances" default = 1 }
-
File
main.tf
:terraform { backend "s3" { bucket = "my-terraform-state" key = "state/${terraform.workspace}/terraform.tfstate" region = "us-east-1" dynamodb_table = "terraform-locks" encrypt = true } } provider "aws" { region = "us-east-1" access_key = "YOUR_ACCESS_KEY" secret_key = "YOUR_SECRET_KEY" } locals { instance_type = terraform.workspace == "prod" ? "t2.large" : var.instance_type instance_count = terraform.workspace == "prod" ? 3 : var.instance_count } resource "aws_instance" "web_server" { count = local.instance_count ami = "ami-0c55b159cbfafe1f0" instance_type = local.instance_type tags = { Name = "WebServer-${terraform.workspace}-${count.index}" } } output "instance_ids" { value = aws_instance.web_server[*].id } output "instance_public_ips" { value = aws_instance.web_server[*].public_ip }
- Quy trình:
- Khởi tạo backend:
terraform init
Output:
Initializing the backend... Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes. Initializing provider plugins... Finding hashicorp/aws versions matching "~> 5.0"... Installing hashicorp/aws v5.17.0... Installed hashicorp/aws v5.17.0 (signed by HashiCorp) Terraform has been successfully initialized!
- Tạo workspace
dev
:terraform workspace new dev
Output:
Created and switched to workspace "dev"!
- Áp dụng cấu hình cho
dev
:terraform apply -auto-approve
Output:
aws_instance.web_server[0]: Creating... aws_instance.web_server[0]: Creation complete after 45s [id=i-0abcdef123456789] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: instance_ids = [ "i-0abcdef123456789" ] instance_public_ips = [ "54.123.45.67" ]
- Tạo workspace
prod
:terraform workspace new prod
Output:
Created and switched to workspace "prod"!
- Áp dụng cấu hình cho
prod
:terraform apply -auto-approve
Output:
aws_instance.web_server[0]: Creating... aws_instance.web_server[1]: Creating... aws_instance.web_server[2]: Creating... aws_instance.web_server[0]: Creation complete after 45s [id=i-0prod123456789] aws_instance.web_server[1]: Creation complete after 45s [id=i-1prod123456789] aws_instance.web_server[2]: Creation complete after 45s [id=i-2prod123456789] Apply complete! Resources: 3 added, 0 changed, 0 destroyed. Outputs: instance_ids = [ "i-0prod123456789", "i-1prod123456789", "i-2prod123456789" ] instance_public_ips = [ "54.123.45.68", "54.123.45.69", "54.123.45.70" ]
- Xem danh sách workspace:
terraform workspace list
Output:
default dev * prod
- Xóa workspace
prod
(sau khi xóa tài nguyên):terraform destroy -auto-approve terraform workspace select default terraform workspace delete prod
Output:
aws_instance.web_server[0]: Destroying... [id=i-0prod123456789] aws_instance.web_server[1]: Destroying... [id=i-1prod123456789] aws_instance.web_server[2]: Destroying... [id=i-2prod123456789] aws_instance.web_server[0]: Destruction complete after 30s aws_instance.web_server[1]: Destruction complete after 30s aws_instance.web_server[2]: Destruction complete after 30s Destroy complete! Resources: 0 added, 0 changed, 3 destroyed. Switched to workspace "default". Deleted workspace "prod".
- Khởi tạo backend:
Lưu Ý Quan Trọng Khi Sử Dụng Workspace
- Default workspace: Không thể xóa workspace
default
, luôn tồn tại. - Tách biệt tài nguyên: Đảm bảo tài nguyên giữa các workspace không xung đột (VD: tên S3 bucket phải khác nhau).
- Kết hợp với biến: Dùng biến hoặc
locals
để tùy chỉnh tài nguyên theo workspace (VD:instance_type
khác nhau). - Remote state: Kết hợp với backend để lưu state an toàn, tránh xung đột trong nhóm.
- Xóa cẩn thận: Xóa workspace chỉ khi tài nguyên đã được
destroy
, nếu không sẽ gặp lỗi.
Liên Hệ Với Chứng Chỉ Terraform Associate (003)
Kỳ thi Terraform Associate (003) kiểm tra khả năng sử dụng workspace. Bạn cần nắm:
- Câu hỏi mẫu:
- “Lệnh nào tạo workspace mới?”
Đáp án: `terraform workspace new`. - “Lệnh nào liệt kê các workspace?”
Đáp án:terraform workspace list
. - “Workspace ảnh hưởng đến state như thế nào?”
Đáp án: Mỗi workspace có file state riêng, tách biệt trạng thái giữa các môi trường.
- “Lệnh nào tạo workspace mới?”
- Thực hành: Kỳ thi có thể yêu cầu nhận diện lệnh workspace hoặc dự đoán kết quả khi chuyển đổi workspace.
Kết Luận
Terraform Workspace là công cụ mạnh mẽ để quản lý nhiều môi trường, giúp tái sử dụng mã và tách biệt trạng thái. Hiểu cách tạo, quản lý, và sử dụng workspace, cùng với thực hành thực tế, sẽ giúp bạn tự tin thi đỗ Terraform Associate (003). Ở bài tiếp theo, chúng ta sẽ tìm hiểu cách sử dụng Terraform Module để mô-đun hóa cấu hình.