Skip to content

angosr/aws-deploy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Affine AWS 部署配置

AWS 部署脚本,用于在 ECS Fargate 上部署 Affine API Server,包含 API 限速和滚动部署功能。

功能

  1. API 限速 (AWS WAF)

    • /samples/scoring: 5分钟3次
    • 其他 API: 5分钟30次
    • 基于客户端 IP 限速
  2. 滚动部署 (ECS Fargate)

    • 无状态服务,支持水平扩展
    • 自动根据 Docker 镜像更新
    • 零停机部署(最大200%,最小50%健康实例)
    • 自动回滚功能

快速开始

1. 安装依赖

pip install -r requirements.txt

2. 配置 AWS 凭证

aws configure
# 或设置环境变量
export AWS_ACCESS_KEY_ID=your_key
export AWS_SECRET_ACCESS_KEY=your_secret
export AWS_DEFAULT_REGION=us-east-1

3. 部署

# 完整部署
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(逗号分隔) 自动创建

配置说明

ECS 任务配置

CPU: 512 (0.5 vCPU)
Memory: 1024 MB
Port: 8000
Launch Type: Fargate
Health Check: /health (30s间隔)

WAF 限速规则

规则1: /samples/scoring
  - 限制: 5分钟3次
  - 优先级: 1

规则2: 其他接口
  - 限制: 5分钟30次
  - 优先级: 2

环境变量

SERVICE_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 不生效

# 检查 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>

最佳实践

  1. 生产环境: 至少2个实例,跨多个可用区
  2. 监控: 启用 CloudWatch Container Insights
  3. 日志: 保留7-30天日志便于排查
  4. 安全: 使用 HTTPS + SSL 证书
  5. 备份: 定期备份 WAF 配置

支持

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages