flag92 flag92
部署教程

AWS ECS Fargate 跑 Chatwoot + Dify

Serverless 容器跑 AI 客服,不用管 EC2,按资源用量计费。本文给出 Terraform 模块、自动扩缩与成本控制。

AWS ECS Fargate + RDS + ElastiCache 180 分钟

为什么选 Fargate#

  • 不管 EC2,按 vCPU + GB 秒费
  • 自动扩缩,闲时几乎不花钱
  • 比 EC2 自建贵 30-50%,适合不想运维的小团队

推荐架构#

ALB · 公网

ECS Cluster · Fargate

chatwoot-rails
2-10 task

chatwoot-sidekiq
1-5 task

dify-api
2-10 task

dify-worker
1-5 task

RDS PostgreSQL
Multi-AZ

ElastiCache Redis

S3 · 附件

Terraform 骨架#

module "ecs" {
  source = "terraform-aws-modules/ecs/aws"
  cluster_name = "ai-support"
  
  fargate_capacity_providers = {
    FARGATE      = { default_capacity_provider_strategy = { weight = 50 } }
    FARGATE_SPOT = { default_capacity_provider_strategy = { weight = 50 } }
  }
}

resource "aws_ecs_service" "chatwoot" {
  name            = "chatwoot-rails"
  cluster         = module.ecs.cluster_id
  task_definition = aws_ecs_task_definition.chatwoot.arn
  desired_count   = 2
  launch_type     = "FARGATE"
}

resource "aws_appautoscaling_target" "chatwoot" {
  max_capacity       = 10
  min_capacity       = 2
  resource_id        = "service/${module.ecs.cluster_name}/chatwoot-rails"
  scalable_dimension = "ecs:service:DesiredCount"
  service_namespace  = "ecs"
}

成本估算#

资源配置月费
Fargate(每 Task)0.5 vCPU / 1 GB~$15
Fargate Spot 折扣-50%
RDS PG db.t3.medium~$80
ElastiCache r6g.large~$80
ALB~$20
S3 + 流量~$30
典型中小生产~$300/月

提示:50% 任务跑 Fargate Spot 能省 25-40%。

自动扩缩策略#

# 按 CPU 70% 扩缩
TargetTrackingScalingPolicy:
  TargetValue: 70
  PredefinedMetricSpecification:
    PredefinedMetricType: ECSServiceAverageCPUUtilization

三个 AWS 特有的坑#

  1. Task 启动慢:Fargate 冷启动 30-60s,promo 流量到达前要预热
  2. Spot 中断:Fargate Spot 任务可能 2 分钟提前通知后被回收,配 SIGTERM 优雅退出
  3. NAT 网关贵:私有子网出公网调 LLM 走 NAT,按 GB 计费;流量大时用 VPC Endpoint 走 PrivateLink

站内搜索

按 ⌘ K 随时唤起