使用 AWS CDK 在 AWS 上部署 Dify - 一个开源的 LLM 应用开发平台。
# 安装项目依赖
npm install
# 进入 CDK 目录安装依赖
cd dify-cdk
npm install
cd ..
# 运行交互式配置工具
cd dify-cdk
npm run config
配置工具将引导您设置:
配置将保存在 dify-cdk/config.json 文件中。
npx cdk bootstrap
# 构建并部署所有堆栈
npm run deploy
# 或者分步执行
npm run build
npx cdk deploy --all
部署完成后,您将获得:
并行部署可以显著减少部署时间,特别是在部署多个独立堆栈时:
# 使用并行部署(最多同时部署 4 个堆栈)
npx cdk deploy --all --concurrency 4
# 新加坡区域测试部署示例
export AWS_REGION=ap-southeast-1
npx cdk deploy --all --concurrency 4 --require-approval never
并行部署优势:
部署完成后,系统会自动:
验证命令:
# 检查 TargetGroupBinding 状态
kubectl get targetgroupbindings -n dify
# 检查 Pod 状态
kubectl get pods -n dify
# 检查 Target Groups 健康状态
aws elbv2 describe-target-health \
--target-group-arn <api-target-group-arn> \
--query "TargetHealthDescriptions[*].[Target.Id,TargetHealth.State]" \
--output table
dify-cdk/
├── bin/ # CDK 应用入口
│ ├── dify.ts # 主入口文件
│ └── dify-helm-stack.ts # TargetGroupBinding 部署堆栈
├── lib/ # CDK 堆栈定义
│ ├── VPC/ # VPC 网络堆栈
│ ├── EKS/ # EKS 集群堆栈
│ ├── RDS/ # RDS 数据库堆栈
│ ├── redis/ # Redis 缓存堆栈
│ ├── AOS/ # OpenSearch 堆栈
│ ├── S3/ # S3 存储堆栈
│ ├── alb/ # ALB 构造器(TargetGroupBinding)
│ │ └── dify-alb-construct.ts # ALB 和 Target Groups 创建
│ ├── cloudfront/ # CloudFront CDN 堆栈
│ ├── database/ # 数据库迁移构造器
│ └── helm/ # Helm Chart 部署
│ └── dify-helm.ts # 支持双模式的 Helm 部署
├── src/ # 配置管理
│ ├── config/ # 配置类型和加载器
│ └── cli/ # 配置向导工具
├── config.json # 部署配置文件
└── config-cloudfront-example.json # CloudFront 示例配置
{
"dify": {
"version": "1.1.0",
"pluginDaemon": {
"enabled": true,
"storageSize": "20Gi"
},
"dbMigration": {
"enabled": true // 推荐启用自动数据库迁移
}
},
"network": {
"useExistingVpc": false,
"vpcCidr": "10.0.0.0/16"
},
"cluster": {
"useExistingCluster": false,
"clusterName": "dify-eks",
"version": "1.33",
"managedNodeGroups": {
"app": {
"instanceType": "m8g.large",
"desiredSize": 3,
"minSize": 1,
"maxSize": 6
}
}
},
"database": {
"instanceType": "db.m6g.large"
},
"redis": {
"nodeType": "cache.t4g.micro"
},
"openSearch": {
"enabled": true,
"dataNodeInstanceType": "r6g.xlarge.search"
}
}
CDK 创建 ALB 资源:
Helm 部署 Dify:
TargetGroupBinding 自动绑定:
# TargetGroupBinding 示例
apiVersion: elbv2.k8s.aws/v1beta1
kind: TargetGroupBinding
metadata:
name: dify-api-tgb
namespace: dify
spec:
serviceRef:
name: dify-api-svc
port: 80
targetGroupARN: arn:aws:elasticloadbalancing:...
networking:
ingress:
- from:
- ipBlock:
cidr: 10.0.0.0/16
CloudFront 现在可以在初始部署时直接配置,因为 ALB DNS 是预先知道的:
{
"domain": {
"useCloudfront": true,
"domainName": "dify.example.com",
"hostedZoneId": "Z1234567890ABC",
"cloudfront": {
"enabled": true,
"domainName": "dify.example.com",
"aliases": ["www.dify.example.com"],
"priceClass": "PriceClass_200",
"waf": {
"enabled": false
}
}
}
}
如果您已经使用传统 Ingress 模式部署,可以平滑迁移:
dify-helm-stack.ts如果部署在中国区域(cn-north-1 或 cn-northwest-1),需要注意:
| 组件 | 实例类型/规格 | 预估月成本 |
|---|---|---|
| EKS 集群 | 管理费用 | $72 |
| EC2 节点 | m8g.large x3 | ~$240 |
| Aurora PostgreSQL | Serverless v2 (0.5-4 ACU) | ~$100-400 |
| ElastiCache Redis | cache.t4g.small | ~$25 |
| OpenSearch | r6g.xlarge x2 | ~$400 |
| ALB | 标准 | ~$25 |
| CloudFront | 请求 + 数据传输 | ~$20-50 |
| S3 | 按使用量 | 变动 |
总计: 约 $900-1250/月(含 CloudFront)
| 组件 | 实例类型/规格 | 预估月成本 |
|---|---|---|
| EKS 集群 | 管理费用 | $72 |
| EC2 节点 | m8g.large x2 | ~$160 |
| Aurora PostgreSQL | db.t4g.medium | ~$60 |
| ElastiCache Redis | cache.t4g.micro | ~$15 |
| OpenSearch | t3.small.search x1 | ~$35 |
| ALB | 标准 | ~$25 |
| CloudFront | 最小使用 | ~$5-10 |
| S3 | 最小存储 | ~$5 |
测试环境总计: 约 $380-400/月
dbMigration 可自动执行数据库 schema 迁移config.json 中的 dify.versionnpm run deploy 重新部署# 通过 AWS 控制台或 CLI 调整节点组大小
aws eks update-nodegroup-config \
--cluster-name dify-eks \
--nodegroup-name NodeGroup \
--scaling-config minSize=2,maxSize=10,desiredSize=5
# 查看堆栈依赖关系
npx cdk list
# 并行部署独立堆栈
npx cdk deploy DifyVPCStack DifyS3Stack --concurrency 2
# 部署所有堆栈(自动处理依赖)
npx cdk deploy --all --concurrency 4
# 仅部署特定堆栈
npx cdk deploy DifyStack
# 排除某些堆栈
npx cdk deploy --all --exclusively DifyCloudFrontStack
# 检查 TargetGroupBinding 状态
kubectl get targetgroupbindings -n dify
# 查看 TargetGroupBinding 详情
kubectl describe targetgroupbinding dify-api-tgb -n dify
# 检查 AWS Load Balancer Controller 日志
kubectl logs -n kube-system deployment/aws-load-balancer-controller
# 检查 Pod 状态
kubectl get pods -n dify
# 查看 Pod 日志
kubectl logs -n dify <pod-name>
# 检查事件
kubectl get events -n dify
# 检查 RDS 安全组
# 确保允许来自 EKS 节点的连接
# 验证 Secret
kubectl get secret -n dify dify-db-secret -o yaml
欢迎提交 Issue 和 Pull Request!
TypeScript
87.4%
JavaScript
12.6%
使用 AWS CDK 在 AWS 上部署 Dify - 一个开源的 LLM 应用开发平台。
# 安装项目依赖
npm install
# 进入 CDK 目录安装依赖
cd dify-cdk
npm install
cd ..
# 运行交互式配置工具
cd dify-cdk
npm run config
配置工具将引导您设置:
配置将保存在 dify-cdk/config.json 文件中。
npx cdk bootstrap
# 构建并部署所有堆栈
npm run deploy
# 或者分步执行
npm run build
npx cdk deploy --all
部署完成后,您将获得:
并行部署可以显著减少部署时间,特别是在部署多个独立堆栈时:
# 使用并行部署(最多同时部署 4 个堆栈)
npx cdk deploy --all --concurrency 4
# 新加坡区域测试部署示例
export AWS_REGION=ap-southeast-1
npx cdk deploy --all --concurrency 4 --require-approval never
并行部署优势:
部署完成后,系统会自动:
验证命令:
# 检查 TargetGroupBinding 状态
kubectl get targetgroupbindings -n dify
# 检查 Pod 状态
kubectl get pods -n dify
# 检查 Target Groups 健康状态
aws elbv2 describe-target-health \
--target-group-arn <api-target-group-arn> \
--query "TargetHealthDescriptions[*].[Target.Id,TargetHealth.State]" \
--output table
dify-cdk/
├── bin/ # CDK 应用入口
│ ├── dify.ts # 主入口文件
│ └── dify-helm-stack.ts # TargetGroupBinding 部署堆栈
├── lib/ # CDK 堆栈定义
│ ├── VPC/ # VPC 网络堆栈
│ ├── EKS/ # EKS 集群堆栈
│ ├── RDS/ # RDS 数据库堆栈
│ ├── redis/ # Redis 缓存堆栈
│ ├── AOS/ # OpenSearch 堆栈
│ ├── S3/ # S3 存储堆栈
│ ├── alb/ # ALB 构造器(TargetGroupBinding)
│ │ └── dify-alb-construct.ts # ALB 和 Target Groups 创建
│ ├── cloudfront/ # CloudFront CDN 堆栈
│ ├── database/ # 数据库迁移构造器
│ └── helm/ # Helm Chart 部署
│ └── dify-helm.ts # 支持双模式的 Helm 部署
├── src/ # 配置管理
│ ├── config/ # 配置类型和加载器
│ └── cli/ # 配置向导工具
├── config.json # 部署配置文件
└── config-cloudfront-example.json # CloudFront 示例配置
{
"dify": {
"version": "1.1.0",
"pluginDaemon": {
"enabled": true,
"storageSize": "20Gi"
},
"dbMigration": {
"enabled": true // 推荐启用自动数据库迁移
}
},
"network": {
"useExistingVpc": false,
"vpcCidr": "10.0.0.0/16"
},
"cluster": {
"useExistingCluster": false,
"clusterName": "dify-eks",
"version": "1.33",
"managedNodeGroups": {
"app": {
"instanceType": "m8g.large",
"desiredSize": 3,
"minSize": 1,
"maxSize": 6
}
}
},
"database": {
"instanceType": "db.m6g.large"
},
"redis": {
"nodeType": "cache.t4g.micro"
},
"openSearch": {
"enabled": true,
"dataNodeInstanceType": "r6g.xlarge.search"
}
}
CDK 创建 ALB 资源:
Helm 部署 Dify:
TargetGroupBinding 自动绑定:
# TargetGroupBinding 示例
apiVersion: elbv2.k8s.aws/v1beta1
kind: TargetGroupBinding
metadata:
name: dify-api-tgb
namespace: dify
spec:
serviceRef:
name: dify-api-svc
port: 80
targetGroupARN: arn:aws:elasticloadbalancing:...
networking:
ingress:
- from:
- ipBlock:
cidr: 10.0.0.0/16
CloudFront 现在可以在初始部署时直接配置,因为 ALB DNS 是预先知道的:
{
"domain": {
"useCloudfront": true,
"domainName": "dify.example.com",
"hostedZoneId": "Z1234567890ABC",
"cloudfront": {
"enabled": true,
"domainName": "dify.example.com",
"aliases": ["www.dify.example.com"],
"priceClass": "PriceClass_200",
"waf": {
"enabled": false
}
}
}
}
如果您已经使用传统 Ingress 模式部署,可以平滑迁移:
dify-helm-stack.ts如果部署在中国区域(cn-north-1 或 cn-northwest-1),需要注意:
| 组件 | 实例类型/规格 | 预估月成本 |
|---|---|---|
| EKS 集群 | 管理费用 | $72 |
| EC2 节点 | m8g.large x3 | ~$240 |
| Aurora PostgreSQL | Serverless v2 (0.5-4 ACU) | ~$100-400 |
| ElastiCache Redis | cache.t4g.small | ~$25 |
| OpenSearch | r6g.xlarge x2 | ~$400 |
| ALB | 标准 | ~$25 |
| CloudFront | 请求 + 数据传输 | ~$20-50 |
| S3 | 按使用量 | 变动 |
总计: 约 $900-1250/月(含 CloudFront)
| 组件 | 实例类型/规格 | 预估月成本 |
|---|---|---|
| EKS 集群 | 管理费用 | $72 |
| EC2 节点 | m8g.large x2 | ~$160 |
| Aurora PostgreSQL | db.t4g.medium | ~$60 |
| ElastiCache Redis | cache.t4g.micro | ~$15 |
| OpenSearch | t3.small.search x1 | ~$35 |
| ALB | 标准 | ~$25 |
| CloudFront | 最小使用 | ~$5-10 |
| S3 | 最小存储 | ~$5 |
测试环境总计: 约 $380-400/月
dbMigration 可自动执行数据库 schema 迁移config.json 中的 dify.versionnpm run deploy 重新部署# 通过 AWS 控制台或 CLI 调整节点组大小
aws eks update-nodegroup-config \
--cluster-name dify-eks \
--nodegroup-name NodeGroup \
--scaling-config minSize=2,maxSize=10,desiredSize=5
# 查看堆栈依赖关系
npx cdk list
# 并行部署独立堆栈
npx cdk deploy DifyVPCStack DifyS3Stack --concurrency 2
# 部署所有堆栈(自动处理依赖)
npx cdk deploy --all --concurrency 4
# 仅部署特定堆栈
npx cdk deploy DifyStack
# 排除某些堆栈
npx cdk deploy --all --exclusively DifyCloudFrontStack
# 检查 TargetGroupBinding 状态
kubectl get targetgroupbindings -n dify
# 查看 TargetGroupBinding 详情
kubectl describe targetgroupbinding dify-api-tgb -n dify
# 检查 AWS Load Balancer Controller 日志
kubectl logs -n kube-system deployment/aws-load-balancer-controller
# 检查 Pod 状态
kubectl get pods -n dify
# 查看 Pod 日志
kubectl logs -n dify <pod-name>
# 检查事件
kubectl get events -n dify
# 检查 RDS 安全组
# 确保允许来自 EKS 节点的连接
# 验证 Secret
kubectl get secret -n dify dify-db-secret -o yaml
欢迎提交 Issue 和 Pull Request!
TypeScript
87.4%
JavaScript
12.6%