【问题标题】:Why isn't the Load Balancer attached to the target group when deploying inside of a custom Amplify resource?在自定义 Amplify 资源中部署时,为什么负载均衡器没有附加到目标组?
【发布时间】: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


    【解决方案1】:

    我最终没有找到解决此问题的方法。在 Amplify 项目之外构建基础架构时,负载均衡器会按预期启动。

    These 两个pages 概述配置 Amplify 客户端库以使用未与 Amplify CLI/Env 一起部署的基础架构。走这条路还允许您使用 CDK V2,而不是不再开发的 V1,这是 Amplify 官方接受的唯一版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-26
      • 2019-10-05
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 2018-12-27
      相关资源
      最近更新 更多