【发布时间】:2022-06-26 02:59:58
【问题描述】:
我正在尝试使用 CloudFormation CDK 添加自定义 AWS Amplify 资源:在 Fargate 上运行的 Grafana 容器,前端有负载均衡器。这应该很简单,使用 L3 构造 ecs_patterns.applicationLoadBalancedFargateService() 但无论我尝试什么,我都会不断收到错误消息“具有 targetGroupArn [arn] 的目标组没有关联的负载均衡器”。即使在连接两者之前尝试分别创建负载均衡器和 Fargate 服务,似乎也无法使用 addListener 或其他任何东西为目标组分配负载均衡器......使用 service.registerLoadBalancerTargets() 时也会返回相同的错误也是。
我已经部署了其他自定义资源,但现在我迷路了;该示例是否比the docs 中所述的更多?我在这里缺少什么 Amplify 特定的东西吗?
如果有帮助,这是我的代码的 sn-p:
const vpc = new ec2.Vpc(this, 'MarketVpc', { maxAzs: 2 }); //LB requires two Azs
const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
const grafana = new ecs_patterns.ApplicationLoadBalancedFargateService(this, "Grafana", {
cluster,
cpu: 1024,
memoryLimitMiB: 2048,
loadBalancerName: "GrafanaLB",
circuitBreaker: {rollback: true},
taskImageOptions: {
containerName: 'grafana',
containerPort: 3000,
image: ecs.ContainerImage.fromAsset("./Amplify/backend/custom/timestream/grafana")
},
publicLoadBalancer: true,
targetProtocol: elbv2.ApplicationProtocol.HTTP,
protocol: elbv2.ApplicationProtocol.HTTP,
});
const scalableTarget = grafana.service.autoScaleTaskCount({
minCapacity: 1,
maxCapacity: 1
});
const listener = grafana.loadBalancer.addListener("grafanaListener",{
protocol: elbv2.ApplicationProtocol.HTTP,
defaultTargetGroups: [grafana.targetGroup]
})
grafana.service.registerLoadBalancerTargets({
containerName: 'grafana',
containerPort: 3000,
newTargetGroupId: 'ECSTargetGroup',
listener: ecs.ListenerConfig.applicationListener(listener)
})
我是 CDK 和 IaC 的新手,但据我了解,定义可扩展目标后的一切都是多余的,但我仍然被告知目标组没有关联的负载均衡器。我还尝试手动将服务附加到 LB:
const grafanaTask = new ecs.TaskDefinition(this, 'WorkerTask', {
compatibility: ecs.Compatibility.FARGATE,
cpu: '1024',
memoryMiB: '2048'
});
const container = grafanaTask.addContainer('grafana', {
image: ecs.ContainerImage.fromAsset('./Amplify/backend/custom/timestream/grafana'),
environment: {
dbARN: db.attrArn, //convert to timestream plugin stuff
database: table.databaseName,
table: table.tableName
},
containerName: 'grafana'
})
container.addPortMappings({containerPort: 3000})
const grafana = new ecs.FargateService(this, "Grafana", { cluster, taskDefinition:grafanaTask });
const loadBalancer = new elbv2.ApplicationLoadBalancer(this, "GrafanaLB",{
vpc,
deletionProtection: false,
internetFacing: true
})
const listener = loadBalancer.addListener("grafanaListener",{ protocol: elbv2.ApplicationProtocol.HTTP })
grafana.registerLoadBalancerTargets({
containerName: 'grafana',
containerPort: 3000,
newTargetGroupId: 'ECSTargetGroup',
listener: ecs.ListenerConfig.applicationListener(listener)
})
new CfnOutput(this, 'GrafanaLink', {
value: grafana.loadBalancer.loadBalancerDnsName,
description: 'Grafana DNS Name',
});
但不幸的是,这产生了相同的结果。
【问题讨论】:
标签: aws-amplify aws-cdk aws-fargate aws-application-load-balancer