在 AWS 上构建成本效益高的 AutoML 平台:$10-25/月 vs $150+ for SageMaker Endpoints
发布: (2025年12月3日 GMT+8 06:14)
4 min read
原文: Dev.to
Source: Dev.to
TL;DR
我构建了一个无服务器的 AutoML 平台,能够以 ≈ $10‑25 / month 的成本训练机器学习模型。上传 CSV,选择目标列,即可获得训练好的模型——无需机器学习专业知识。
Prerequisites
- AWS 账户(管理员权限)
- AWS CLI v2(已使用
aws configure配置) - Terraform ≥ 1.5
- Docker(已运行)
- Node.js 18+ 和 pnpm(前端)
- Python 3.11+(本地开发)
Deployment
Estimated time: ~15 minutes from clone to a working platform.
git clone https://github.com/cristofima/AWS-AutoML-Lite.git
cd AWS-AutoML-Lite/infrastructure/terraform
terraform init && terraform apply
Why a Custom Solution?
AWS SageMaker Autopilot 功能强大,但在原型开发时成本较高。
- 免费层:前 2 个月每月提供 50 h 的训练时长。
- 实时推理端点(例如
ml.c5.xlarge)的费用约为 $150 / month,全天候运行。
目标是为副项目提供一个 更便宜、无服务器 的替代方案。
Goals
| 目标 | 期望结果 |
|---|---|
| Upload CSV → Trained model | .pkl 文件 |
| Auto‑detect problem type | 分类 vs. 回归 |
| Automatic EDA reports | 开箱即用的数据概况 |
| Cost | 低于 $25 / month |
Architecture Overview
| 组件 | 技术 | 原因 |
|---|---|---|
| Backend API | FastAPI + Mangum | 异步,自动文档,适用于 Lambda |
| Training | FLAML + scikit‑learn | 快速 AutoML,生产就绪 |
| Frontend | Next.js 16 + Tailwind | 通过 Amplify 支持服务器端渲染 |
| Infrastructure | Terraform | 可复现,多环境 |
| CI/CD | GitHub Actions + OIDC | 无需存储 AWS 凭证 |
Problem‑type detection (auto)
# classification if: **Note:** If you add a parameter to `train.py`, you must also add it to `container_overrides`.
Time budget per dataset size
| 行数 | 时间预算 |
|---|---|
50 K | 20 min |
Real‑time status
前端每 5 seconds 轮询 DynamoDB 以显示训练进度。
Automatic reports
- EDA report – 使用数据概况库生成。
- Training report – 模型性能指标和特征重要性。
IAM Policy (GitHub OIDC)
{
"Statement": [
{
"Sid": "CoreServices",
"Effect": "Allow",
"Action": ["s3:*", "dynamodb:*", "lambda:*", "batch:*", "ecr:*"],
"Resource": "arn:aws:*:*:*:automl-lite-*"
},
{
"Sid": "APIGatewayAndAmplify",
"Effect": "Allow",
"Action": ["apigateway:*", "amplify:*"],
"Resource": "*"
},
{
"Sid": "IAMRoles",
"Effect": "Allow",
"Action": ["iam:*Role*", "iam:*RolePolicy*", "iam:PassRole"],
"Resource": "arn:aws:iam::*:role/automl-lite-*"
},
{
"Sid": "ServiceLinkedRoles",
"Effect": "Allow",
"Action": "iam:CreateServiceLinkedRole",
"Resource": "arn:aws:iam::*:role/aws-service-role/*"
},
{
"Sid": "Networking",
"Effect": "Allow",
"Action": ["ec2:Describe*", "ec2:*SecurityGroup*", "ec2:*Tags"],
"Resource": "*"
},
{
"Sid": "Logging",
"Effect": "Allow",
"Action": "logs:*",
"Resource": "arn:aws:logs:*:*:log-group:/aws/*/automl-lite-*"
}
]
}
CI/CD Flow
| 分支 | 操作 |
|---|---|
dev | Auto‑deploy to DEV |
main | plan → manual approval → deploy to PROD |
Deployment times (approx.)
| 目标 | 时间 |
|---|---|
| Lambda only | ~2 min |
| Training container | ~3 min |
| Frontend | ~3 min |
| Full infrastructure | ~10 min |
Monthly Cost Breakdown
| 服务 | 费用 |
|---|---|
| AWS Amplify | $5‑15 |
| Lambda + API Gateway | $1‑2 |
| Batch (Fargate Spot) | $2‑5 |
| S3 + DynamoDB | $1‑2 |
| Total | $10‑25 |
Comparison with SageMaker
| 特性 | SageMaker Autopilot | AWS AutoML Lite |
|---|---|---|
| 每月成本 | ~$150 + (real‑time endpoint) | $10‑25 |
| 设置时间 | 30 + min (Studio) | ~15 min |
| 可移植模型 | ❌ locked to SageMaker | ✅ downloadable .pkl |
| 机器学习专业程度 | Medium | None |
| 自动问题检测 | ✅ | ✅ |
| EDA 报告 | ❌ manual | ✅ automatic |
| 基础设施即代码 | ❌ console‑heavy | ✅ full Terraform |
| 冷启动 | N/A (always‑on) | ~200 ms (Lambda) |
| 最佳适用场景 | Production pipelines | Prototyping & side projects |
Using the Trained Model
# Build prediction container
docker build -f scripts/Dockerfile.predict -t automl-predict .
# Show model info
docker run --rm -v ${PWD}:/data automl-predict /data/model.pkl --info
# Predict from CSV
docker run --rm -v ${PWD}:/data automl-predict \
/data/model.pkl -i /data/test.csv -o /data/predictions.csv
Key observations
- 265 MB 的机器学习依赖体积迫使 Lambda ↔ Batch 分离。
- Fargate Spot 可节省约 70%,短作业中中断很少。
- FLAML 提供更小的体积和更快的训练速度,且结果可与 AutoGluon 相媲美。
Future Work (Roadmap)
- ☐ ONNX 导出 – 将模型部署到边缘设备
- ☐ 模型比较 UI – 并行训练多个模型
- ☐ 通过 WebSocket 实时更新(替代轮询)
- ☐ 使用 Cognito 进行多用户支持
- ☐ 超参数 UI – 从前端微调 FLAML 设置
- ☐ 训练完成时的邮件通知
Contributing
欢迎贡献!请查看 GitHub Issues 以获取适合新手的议题。
- Repository: (link omitted)