Quay lại

Tạo Hạ Tầng AWS theo Best Practice với Amazon Q + MCP + Terraform Module + Workspace Chuyên mục Devops    2025-05-26    1 Lượt xem    1 Lượt thích    comment-3 Created with Sketch Beta. 0 Bình luận

🎯 Mục tiêu

Trong bài viết này, bạn sẽ học cách:

  • Sử dụng Amazon Q kết hợp MCP để sinh mã Terraform tự động.

  • Tổ chức mã Terraform theo best practices: dùng module, chia theo môi trường (dev/stage/prod).

  • Viết prompt chất lượng để Q tạo ra đúng những gì bạn cần.


💡 Tại sao nên dùng Amazon Q và MCP?

Khi triển khai hạ tầng trên AWS, việc viết Terraform thủ công có thể:

  • Tốn thời gian.

  • Sai sót dễ xảy ra.

  • Thiếu tổ chức khi dự án mở rộng.

Amazon Q + MCP là combo AI mới giúp bạn:

✅ Sinh mã Terraform chất lượng, có mô-đun rõ ràng
✅ Hỗ trợ multi-environment với workspace
✅ Ghi chú, đặt tag tài nguyên đúng chuẩn
✅ Tạo tài nguyên phức tạp như ECS, RDS, ALB, WAF, API Gateway...


🏗️ Chuẩn bị

  1. Đã cài đặt MCP CLI (uv, amazon-q-cli) trên máy.

  2. Có thư mục dự án (ví dụ: terraform-aws-infrastructure).

  3. Đã login Amazon Q và khởi tạo workspace Terraform.


✍️ Cách viết prompt hiệu quả

1. Prompt mẫu khởi tạo hạ tầng cơ bản:

q chat "Generate production-grade Terraform code using module structure to deploy the following infrastructure on AWS:

- A VPC with a CIDR block of 10.0.0.0/16
- Two public subnets and two private subnets across two availability zones
- An Internet Gateway for public subnets
- A NAT Gateway in the public subnet for private subnet egress
- Route tables and appropriate associations
- A Security Group for an ECS Fargate service (allowing HTTP/HTTPS from ALB)
- A Security Group for RDS (allowing access from ECS)
- An Application Load Balancer with a listener on port 80
- IAM roles and policies for ECS tasks and services​
Structure the Terraform code using modules and prepare for multi-environment deployment (e.g. dev/stage/prod) using workspaces. Use prefix 'liveapp' for resource naming. Provide variables.tf and outputs.tf where appropriate. Update the existing terraform-aws-infrastructure project. Do not create a new folder."

🧠 Mẹo: Thêm Update the existing ... để tránh Q tạo folder mới.


2. Prompt để tạo ECS + ALB:

q chat "Update the existing terraform-aws-infrastructure project.

Create an ECS Fargate Cluster and Task Definition (using image from ECR: app-image-backend-api), with a public Application Load Balancer forwarding traffic to ECS Tasks running in private subnets. Use two target groups (blue/green) and prepare for Blue/Green deployment via CodeDeploy."​

3. Prompt cho các database:

q chat "Update the existing terraform-aws-infrastructure project.

Provision the following databases:
- In dev and stage: RDS Postgres (standard), DocumentDB (no cluster), ElastiCache Redis (no cluster)
- In prod: RDS Aurora Postgres, RDS Postgres (non-multi-AZ), DocumentDB (cluster), ElastiCache Redis (cluster)

Place all DBs in private subnets of the existing VPC. Use appropriate SGs and parameter groups. Tag all resources with Project, Environment, and ManagedBy."​

📁 Cấu trúc thư mục sau khi tạo

terraform-aws-infrastructure/
├── main.tf
├── variables.tf
├── outputs.tf
├── modules/
│   ├── vpc/
│   ├── ecs/
│   ├── alb/
│   ├── rds/
│   ├── documentdb/
│   ├── redis/
│   └── apigateway/
├── envs/
│   ├── dev.tfvars
│   ├── stage.tfvars
│   └── prod.tfvars​

✅ Kết luận

Sử dụng Amazon Q + MCP giúp bạn:

 

  • Khởi tạo hạ tầng nhanh chóng, chuẩn hoá.

  • Tận dụng best practice về Terraform (modular, DRY, tagging, workspace).

  • Tập trung hơn vào logic hạ tầng thay vì syntax.

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