AWS 部署脚本,用于在 ECS Fargate 上部署 Affine API Server,包含 API 限速和滚动部署功能。
-
API 限速 (AWS WAF)
/samples/scoring: 5分钟3次- 其他 API: 5分钟30次
- 基于客户端 IP 限速
-
滚动部署 (ECS Fargate)
- 无状态服务,支持水平扩展
- 自动根据 Docker 镜像更新
- 零停机部署(最大200%,最小50%健康实例)
- 自动回滚功能
pip install -r requirements.txtaws configure
# 或设置环境变量
export AWS_ACCESS_KEY_ID=your_key
export AWS_SECRET_ACCESS_KEY=your_secret
export AWS_DEFAULT_REGION=us-east-1# 完整部署
python3 aws_deploy_config.py \
--action setup \
--cluster-name affine-cluster \
--service-name affine-api \
--web-acl-name affine-api-waf \
--desired-count 2# 1. 完整部署(创建集群、服务、WAF)
python3 aws_deploy_config.py \
--action setup \
--cluster-name affine-cluster \
--service-name affine-api \
--web-acl-name affine-api-waf \
--desired-count 2
# 2. 扩容/缩容
python3 aws_deploy_config.py \
--action update-service \
--cluster-name affine-cluster \
--service-name affine-api \
--desired-count 5
# 3. 部署新版本
python3 aws_deploy_config.py \
--action deploy \
--cluster-name affine-cluster \
--service-name affine-api \
--image thebes1618/affine:v2.0
# 4. 更新 WAF 规则
python3 aws_deploy_config.py \
--action update-waf \
--web-acl-name affine-api-waf# 查看服务状态
aws ecs describe-services \
--cluster affine-cluster \
--services affine-api \
--query 'services[0].{Name:serviceName,Status:status,Running:runningCount,Desired:desiredCount}' \
--output table
# 查看任务列表
aws ecs list-tasks \
--cluster affine-cluster \
--service-name affine-api
# 实时查看日志
aws logs tail /ecs/affine-api-task --follow| 参数 | 说明 | 必需 | 默认值 |
|---|---|---|---|
--action |
操作类型 | 是 | - |
--cluster-name |
ECS 集群名称 | 部分 | - |
--service-name |
ECS 服务名称 | 部分 | - |
--web-acl-name |
WAF Web ACL 名称 | 部分 | - |
--desired-count |
期望实例数 | 否 | 2 |
--image |
Docker 镜像 | 否 | thebes1618/affine:latest |
--region |
AWS 区域 | 否 | us-east-1 |
--subnets |
VPC 子网 ID(逗号分隔) | 否 | 默认VPC子网 |
--security-groups |
安全组 ID(逗号分隔) | 否 | 自动创建 |
CPU: 512 (0.5 vCPU)
Memory: 1024 MB
Port: 8000
Launch Type: Fargate
Health Check: /health (30s间隔)规则1: /samples/scoring
- 限制: 5分钟3次
- 优先级: 1
规则2: 其他接口
- 限制: 5分钟30次
- 优先级: 2SERVICE_MODE=true
API_SERVICES_ENABLED=true
API_HOST=0.0.0.0
API_PORT=8000
API_WORKERS=2
API_RATE_LIMIT_ENABLED=true# 查看任务失败原因
aws ecs describe-tasks \
--cluster affine-cluster \
--tasks $(aws ecs list-tasks --cluster affine-cluster --service-name affine-api --query 'taskArns[0]' --output text)
# 查看日志
aws logs tail /ecs/affine-api-task --follow# 检查 WAF 关联
aws wafv2 list-resources-for-web-acl \
--web-acl-arn <waf-arn> \
--resource-type APPLICATION_LOAD_BALANCER| 资源 | 月成本(us-east-1) |
|---|---|
| ECS Fargate (2任务, 0.5vCPU, 1GB) | ~$24 |
| Application Load Balancer | ~$16 |
| WAF (Web ACL + 规则) | $5 + 按请求 |
| 总计 | ~$45-50/月 |
# 删除服务
aws ecs update-service --cluster affine-cluster --service affine-api --desired-count 0
aws ecs delete-service --cluster affine-cluster --service affine-api
# 删除集群
aws ecs delete-cluster --cluster affine-cluster
# 删除 WAF
aws wafv2 delete-web-acl --name affine-api-waf --scope REGIONAL --id <id> --lock-token <token>- 生产环境: 至少2个实例,跨多个可用区
- 监控: 启用 CloudWatch Container Insights
- 日志: 保留7-30天日志便于排查
- 安全: 使用 HTTPS + SSL 证书
- 备份: 定期备份 WAF 配置