Tổng quan Series
AI agents rất mạnh, nhưng chạy production là chuyện khác. Cần infrastructure reliable, scalable, secure — và AWS giải quyết được tất cả.
Series này dạy bạn xây dựng production-grade infrastructure cho AI agents trên AWS. Mỗi ngày là một piece của puzzle: deploy model, quản lý state, caching, routing traffic, và automate deployment.
Kiến trúc tổng thể — Chúng ta đang xây cái gì?
AI Agent trong Production │ ┌─────────────────────────┼─────────────────────────┐ │ │ │ ▼ ▼ ▼ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ Route53 + │ │ ALB │ │ CI/CD │ │ CloudFront │ │ (Load Balancer) │ Pipeline │ │ (Day 5) │ │ (Day 1) │ │ (Day 6) │ └──────┬───────┘ └──────┬───────┘ └──────────────┘ │ │ ┌──────▼───────────────────────▼──────┐ │ Compute Layer │ │ ┌────────────────┬──────────────┐ │ │ │ ECS Fargate │ Lambda + │ │ │ │ (Container) │ Bedrock │ │ │ │ (Day 1) │ (Day 4) │ │ │ └───────┬───────┴──────┬───────┘ │ └──────────┼──────────────┼──────────┘ │ │ ┌──────────▼──────────────▼──────────┐ │ Data Layer │ │ ┌──────────────┬──────────────┐ │ │ │ DynamoDB │ ElastiCache │ │ │ │ (State/SS) │ (Cache/LLM) │ │ │ │ (Day 2) │ (Day 3) │ │ │ └──────────────┴──────────────┘ │ └────────────────────────────────────┘Lộ trình 6 ngày
| Day | Chủ đề | AWS Services | Học gì? |
|---|---|---|---|
| 1 | Deploy MCP Server lên ECS Fargate | ECS, ECR, ALB, Secrets Manager | Containerize + deploy agent server với HTTPS, secrets, auto-scaling |
| 2 | Agent State với DynamoDB | DynamoDB Global Tables, DAX | Lưu conversation history, session state, replication đa region |
| 3 | LLM Caching với ElastiCache + Bedrock | ElastiCache (Redis), Bedrock | Semantic + prompt caching, giảm latency và cost |
| 4 | Serverless Agent với Lambda + Bedrock | Lambda, API Gateway, Bedrock, Step Functions | Build agent không cần server |
| 5 | Multi-Region Routing với Route53 | Route53, CloudFront, Global Accelerator | Global traffic routing, failover, latency-based |
| 6 | CI/CD cho AI Agents | CodePipeline, CodeBuild, ECR, ECS | Automated deployment, zero-downtime ship |
Day 1: Deploy MCP Server lên ECS Fargate
Bạn xây MCP server. Nó chạy localhost. Giờ đưa lên internet để agent nào cũng gọi được.
ECS Fargate là sweet spot: không quản lý EC2, auto-scaling sẵn, có load balancer built-in. Ship Docker image, Fargate lo phần còn lại.
Hôm nay deploy:
Agent ─▶ HTTPS :443 ─▶ ALB ─▶ ECS Fargate ─▶ MCP Server (Docker)Các bước:
- Dockerize MCP server
- Push lên ECR
- Lưu secrets (GitHub token) vào Secrets Manager
- Tạo ECS Fargate cluster + task definition
- ALB với HTTPS
- Auto-scaling
- CI/CD tự động
Step 1: Dockerize
FROM node:20-alpine AS builderWORKDIR /appCOPY package*.json ./RUN npm ci --omit=dev
FROM node:20-alpine AS runtimeWORKDIR /appRUN addgroup -S mcp && adduser -S mcp -G mcpCOPY --from=builder /app/node_modules ./node_modulesCOPY dist/ ./dist/COPY package.json ./USER mcpEXPOSE 3001HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:3001/health || exit 1ENV NODE_ENV=production PORT=3001CMD ["node", "dist/index.js"]Multi-stage build: builder stage có devDependencies để compile, runtime chỉ có production code.
Step 2: ECR
# Tạo repository với vulnerability scanningaws ecr create-repository \ --repository-name github-issue-mcp \ --image-scanning-configuration scanOnPush=true
# Push imageaws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account>.dkr.ecr.us-east-1.amazonaws.comdocker push <account>.dkr.ecr.us-east-1.amazonaws.com/github-issue-mcp:latestStep 3: Secrets Manager
aws secretsmanager create-secret \ --name "github-issue-mcp/github-token" \ --secret-string "ghp_your_token_here"ECS injects secret vào container khi start. Không lưu trong image, không lưu trong git.
Step 4: ECS Cluster + Service
# Clusteraws ecs create-cluster --cluster-name mcp-server-cluster \ --capacity-providers FARGATE FARGATE_SPOT
# Task definition (CPU 256, RAM 512MB, secret từ Secrets Manager)aws ecs register-task-definition --family github-issue-mcp \ --network-mode awsvpc --requires-compatibilities FARGATE \ --cpu 256 --memory 512 \ --container-definitions '[ {"name":"mcp-server","image":"<account>.dkr.ecr.us-east-1.amazonaws.com/github-issue-mcp:latest", "portMappings":[{"containerPort":3001}], "secrets":[{"name":"GITHUB_TOKEN","valueFrom":"<secret-arn>"}], "logConfiguration":{"logDriver":"awslogs","options":{"awslogs-group":"/ecs/github-issue-mcp"}}} ]'
# Service (2 tasks, private subnets, ALB attached)aws ecs create-service --cluster mcp-server-cluster \ --service-name github-issue-mcp --task-definition github-issue-mcp \ --desired-count 2 --launch-type FARGATE \ --network-configuration "awsvpcConfiguration={subnets=[subnet-private-a,subnet-private-b],securityGroups=[<task-sg>],assignPublicIp=DISABLED}" \ --load-balancers "targetGroupArn=<tg-arn>,containerName=mcp-server,containerPort=3001"Step 5: ALB
Internet ─▶ HTTPS :443 ─▶ ALB ─▶ HTTP :3001 ─▶ ECS Task (private subnet)- Security groups: ALB SG allow :443, Task SG allow :3001 từ ALB SG
- Target group health check: GET /health
- Cần ACM certificate cho HTTPS
Step 6: Auto-Scaling
aws application-autoscaling register-scalable-target \ --service-namespace ecs \ --resource-id service/mcp-server-cluster/github-issue-mcp \ --scalable-dimension ecs:service:DesiredCount \ --min-capacity 2 --max-capacity 20
# Scale out khi request > 100/targetaws application-autoscaling put-scaling-policy \ --service-namespace ecs \ --resource-id service/mcp-server-cluster/github-issue-mcp \ --scalable-dimension ecs:service:DesiredCount \ --policy-name request-count-target \ --policy-type TargetTrackingScaling \ --target-tracking-scaling-policy-configuration '{ "TargetValue": 100.0, "PredefinedMetricSpecification": { "PredefinedMetricType": "ALBRequestCountPerTarget" } }'Step 7: CI/CD
buildspec.yml
version: 0.2phases: pre_build: commands: - npm ci && npm run build - aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_REPOSITORY_URI build: commands: - docker build -t $ECR_REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION . - docker tag $ECR_REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION $ECR_REPOSITORY_URI:latest post_build: commands: - docker push $ECR_REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION - printf '[{"name":"mcp-server","imageUri":"%s"}]' $ECR_REPOSITORY_URI:$CODEBUILD_RESOLVED_SOURCE_VERSION > imagedefinitions.jsonMỗi push lên main → build → push ECR → ECS deploy → ALB drain + route.
Kết nối Agent
const client = new McpClient({ transport: new SSEClientTransport({ url: "https://alb-dns/sse", headers: { "Authorization": "Bearer <sse-secret>" }, }),});Chi phí (~$69/tháng)
| Component | Cost |
|---|---|
| Fargate (2 tasks) | ~$30 |
| ALB | ~$22 |
| ECR + Secrets + CW | ~$7 |
| CodePipeline | ~$10 |
| Total | ~$69 |
Checklist
- Dockerfile multi-stage
- ECR scanOnPush
- Secrets Manager
- ECS task definition
- ALB + HTTPS + health check
- Auto-scaling
- CodePipeline
- CloudWatch dashboard
| Day | Chủ đề |
|---|---|
| 1 | Deploy MCP Server lên ECS Fargate ✅ |
| 2 | Agent State với DynamoDB Global Tables |
| 3 | LLM Caching với ElastiCache + Bedrock |
| 4 | Serverless Agent với Lambda + Bedrock |
| 5 | Multi-Region Agent Routing với Route53 |
| 6 | CI/CD cho AI Agents với CodePipeline |
Series: AWS cho AI/Agent Developers. Day 1: Deploy MCP server lên ECS Fargate với ALB, Secrets Manager, auto-scaling, và CI/CD.
Advertisement
Advertisement