'Why isn't the Load Balancer attached to the target group when deploying inside of a custom Amplify resource?

I'm attempting to add a custom AWS Amplify resource using the CloudFormation CDK: a Grafana container running on Fargate, fronted by a load balancer. This should be simple enough using the L3 construct ecs_patterns.applicationLoadBalancedFargateService() but no matter what I try I keep getting the error "The target group with targetGroupArn [arn] does not have an associated load balancer". Even when attempting to create the load balancer and Fargate service separately before connecting the two it seems that the target group just cannot be assigned the load balancer using addListener or anything else... the same error is also returned when using service.registerLoadBalancerTargets() as well.

I've gotten other custom resources to deploy but with this one I'm lost at this point; Is there more to the example than stated in the docs? Is there something Amplify specific I'm missing here?

Here's a snippet of my code if that helps:

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)
})

I'm new to the CDK and IaC but to my understanding everything after defining the scalable target is redundant but I'm still told the target group does not have an associated load balancer. I also tried attaching the service to a LB manually:

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',
});

But unfortunately this yielded the same result.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source