一、背景与挑战
OpenClaw 是一款开源的 AI 驱动智能运维平台,专为中小企业和一人公司设计。本文详细介绍如何在 AWS 云平台上从零搭建面向生产环境验证的 OpenClaw 集群。
AWS 官方博客推荐
本文是对 AWS 官方博客文章《探索 OpenClaw 在电商平台中的应用场景》的深度实践补充。
1.1 为什么选择 AWS?
| 优势领域 | AWS 能力 | 价值 |
|---|---|---|
| 全球基础设施 | 30+ 区域 | 就近部署 |
| 容器服务 | ECS/EKS/Fargate | 无服务器容器 |
| 数据库 | RDS, Aurora | 托管数据库 |
| AI/ML | SageMaker, Bedrock | 大模型能力 |
1.2 技术选型
| 层级 | 选择 | 理由 |
|---|---|---|
| 容器编排 | ECS Fargate | 无需管理节点 |
| 数据库 | RDS PostgreSQL | 成熟稳定 |
| 缓存 | ElastiCache Redis | 高性能 |
| IaC | Terraform | 多云支持 |
二、架构设计
采用三层架构,核心服务部署在私有子网:
- 接入层:Route 53 + ALB + WAF
- 应用层:ECS Fargate 集群(API/Worker/AI)
- 数据层:RDS PostgreSQL + ElastiCache Redis + S3
三、Terraform 完整代码
3.1 VPC 配置
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags = { Name = "openclaw-vpc" }
}
resource "aws_subnet" "public" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = "10.0.\${count.index + 1}.0/24"
map_public_ip_on_launch = true
}
resource "aws_subnet" "private" {
count = 2
vpc_id = aws_vpc.main.id
cidr_block = "10.0.\${count.index + 10}.0/24"
}
3.2 ECS 集群
resource "aws_ecs_cluster" "main" {
name = "openclaw-cluster"
}
resource "aws_ecs_task_definition" "api" {
family = "openclaw-api"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = "1024"
memory = "2048"
container_definitions = jsonencode([{
name = "api"
image = "\${aws_ecr_repository.api.repository_url}:latest"
portMappings = [{ containerPort = 3000 }]
environment = [
{ name = "NODE_ENV", value = "production" }
]
healthCheck = {
command = ["CMD-SHELL", "curl -f http://localhost:3000/health"]
interval = 30
retries = 3
}
}])
}
resource "aws_ecs_service" "api" {
name = "openclaw-api"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.api.arn
desired_count = 2
launch_type = "FARGATE"
}
3.3 RDS 数据库
resource "aws_db_instance" "main" {
identifier = "openclaw-postgres"
engine = "postgres"
engine_version = "15.4"
instance_class = "db.r6g.xlarge"
allocated_storage = 100
storage_type = "gp3"
storage_encrypted = true
db_name = "openclaw"
username = "openclaw_admin"
password = random_password.db.result
multi_az = true
backup_retention_period = 7
deletion_protection = true
}
四、监控告警
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
alarm_name = "openclaw-cpu-high"
comparison_operator = "GreaterThanThreshold"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
threshold = 80
evaluation_periods = 2
dimensions = {
ServiceName = aws_ecs_service.api.name
ClusterName = aws_ecs_cluster.main.name
}
alarm_actions = [aws_sns_topic.alerts.arn]
}
五、成本分析
| 服务 | 配置 | 月费用 |
|---|---|---|
| ECS Fargate | 5 任务 | $150-300 |
| ALB | 标准 | $25-50 |
| RDS | Multi-AZ | $350-400 |
| Redis | 集群 | $120-150 |
| 其他 | NAT/S3/CW | $60-100 |
| 总计 | $700-1000/月 | |
成本优化建议
- Reserved Instances 可节省 30-50%
- Spot 实例可节省 70%
- 配置 HPA 自动伸缩
六、最佳实践
安全
- 核心服务部署在私有子网
- 最小权限 IAM 策略
- ALB 启用 HTTPS 和 WAF
- Secrets Manager 管理密钥
高可用
- 跨可用区部署
- 自动故障转移
- 健康检查
- 定期备份
七、总结
核心要点:
- 架构:VPC + ECS + RDS + Redis
- IaC:Terraform 管理
- 监控:CloudWatch 全覆盖
- 成本:$700-1000/月
下一步
AWS
Terraform
ECS
RDS
DevOps