Quay lại

Bài 7: Bài lab triển khai RDS với Terraform Workspace – Dev, Stage, Prod hiệu quả Chuyên mục Devops    2025-05-15    1 Lượt xem    1 Lượt thích    comment-3 Created with Sketch Beta. 0 Bình luận

🎯 Mục tiêu

  • Tạo một hệ thống RDS (MySQL hoặc PostgreSQL) sử dụng Terraform.

  • Quản lý nhiều môi trường (dev, stage, prod) bằng workspace.

  • Mỗi môi trường sẽ có database instance riêng, cấu hình riêng.

  • State được tách biệt tự động, không ảnh hưởng lẫn nhau.


🧱 Cấu trúc thư mục

terraform-rds-lab-workspace/
├── main.tf
├── variables.tf
├── outputs.tf
├── envs/
│   ├── dev.tfvars
│   ├── stage.tfvars
│   └── prod.tfvars​

📦 1. File variables.tf

variable "region" {
  default = "us-east-1"
}

variable "db_engine" {
  default = "mysql"
}

variable "db_engine_version" {
  default = "8.0"
}

variable "instance_class" {}
variable "db_name" {}
variable "username" {}
variable "password" {}​

⚙️ 2. File main.tf

provider "aws" {
  region = var.region
}

resource "aws_db_instance" "rds" {
  identifier              = "${terraform.workspace}-rds-instance"
  allocated_storage       = 20
  engine                  = var.db_engine
  engine_version          = var.db_engine_version
  instance_class          = var.instance_class
  db_name                    = var.db_name
  username                = var.username
  password                = var.password
  skip_final_snapshot     = true
  publicly_accessible     = true

  tags = {
    Environment = terraform.workspace
  }
}​

❗ Ghi chú:

  • skip_final_snapshot = true chỉ dùng trong lab/dev. Prod nên cấu hình backup chuẩn chỉnh.

  • Bạn có thể thêm parameter_group_name, vpc_security_group_ids, subnet_group_name… nếu muốn kiểm soát cao hơn.


📤 3. File outputs.tf

output "rds_endpoint" {
  value = aws_db_instance.rds.endpoint
}​

📂 4. Các file biến môi trường (envs/*.tfvars)

envs/dev.tfvars

instance_class = "db.t3.micro"
db_name        = "dev_db"
username       = "devuser"
password       = "devpassword123"​

envs/stage.tfvars

instance_class = "db.t3.small"
db_name        = "stage_db"
username       = "stageuser"
password       = "stagepassword123"​

envs/prod.tfvars

instance_class = "db.t3.medium"
db_name        = "prod_db"
username       = "produser"
password       = "prodpasswordSecure!"​

🛠 5. Khởi tạo và tạo workspace

 
terraform init

terraform workspace new dev
terraform workspace new stage
terraform workspace new prod​

🚀 6. Triển khai từng môi trường

✅ Môi trường Dev:

terraform workspace select dev
terraform apply -var-file="envs/dev.tfvars"​

✅ Môi trường Stage:

terraform workspace select stage
terraform apply -var-file="envs/stage.tfvars"​

✅ Môi trường Prod:

terraform workspace select prod
terraform apply -var-file="envs/prod.tfvars"​

💡 Mỗi workspace sẽ có một state riêng nằm trong thư mục .terraform/terraform.tfstate.d/{workspace}/terraform.tfstate.


🔍 Kiểm tra kết quả

Bạn có thể thấy endpoint RDS trong output sau khi apply:

Apply complete! Resources: 1 added.
Outputs:
rds_endpoint = "dev-rds-instance.xxxxxx.us-east-1.rds.amazonaws.com:3306"​

Vào AWS Console → RDS → chọn instance theo tag Environment để xem thông tin chi tiết.


🧠 Tổng kết & Bài học

✅ Điều bạn học được 📌 Mô tả
Workspace Quản lý nhiều môi trường với state tách biệt
Tái sử dụng mã Một file cấu hình, deploy được nhiều môi trường
Quản lý biến Sử dụng .tfvars để thay đổi thông số
Bảo mật Tách password từng môi trường, có thể thêm secrets manager
 

🚀 Nâng cao (tuỳ chọn mở rộng):

  • Kết hợp với RDS Subnet Group, Security Group riêng cho từng môi trường.

  • Dùng remote backend S3 + DynamoDB để lưu state tập trung.

  • Kết hợp với CI/CD (GitHub Actions, GitLab CI, Jenkins…) để tự động deploy từng môi trường.

Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough

Bài viết liên quan

Learning English Everyday