Search code examples
amazon-web-servicesaws-cdkaws-fargate

Deploy ECS Image to existing Application Load Balancer with CDK


I am trying to deploy an ECS Image to an existing Application Load Balancer with CDK. We want to deploy this to an existing ALB and VPC.

// Reference existing ALB
const alb =
  elbv2.ApplicationLoadBalancer.fromApplicationLoadBalancerAttributes(
    this,
    "ImportedALB",
    {
      securityGroupId: "sg-XXXXXXXX",
      loadBalancerArn:
        "arn:aws:elasticloadbalancing:us-east-1:XXXXXXX:listener/app/alb-appliance/XXXXX/XXXXX",
    }
  );

// Create higher level construct containing the Fargate service with a load balancer
new ecspatterns.ApplicationLoadBalancedFargateService(
  this,
  "amazon-ecs-sample"
  {
    loadBalancer: alb,
    cluster: cluster,
    circuitBreaker: {
      rollback: true,
    },
    memoryLimitMiB: 512, 
    cpu: 256,
    assignPublicIp: false,
    desiredCount: 1,
    taskImageOptions: {
      image: image,
      containerPort: 80,
      logDriver: ecs.LogDrivers.awsLogs({
        streamPrefix: id,
        logRetention: logs.RetentionDays.ONE_YEAR,
      }),
    },
  }
);

When I run a CDK diff I see the following error message:

Error: Can only call addTargets() when using a constructed Load Balancer or an imported Load Balancer with specified vpc; construct a new TargetGroup and use addTargetGroup
    at ApplicationListener.addTargets (C:\Users\Documents\GitHub\cdk-workshop-ts\node_modules\aws-cdk-lib\aws-elasticloadbalancingv2\lib\alb\application-listener.js:1:5736)
    at new ApplicationLoadBalancedServiceBase (C:\Users\Documents\GitHub\cdk-workshop-ts\node_modules\aws-cdk-lib\aws-ecs-patterns\lib\base\application-load-balanced-service-base.js:1:3657)
    at new ApplicationLoadBalancedFargateService (C:\Users\Documents\GitHub\cdk-workshop-ts\node_modules\aws-cdk-lib\aws-ecs-patterns\lib\fargate\application-load-balanced-fargate-service.js:1:601)
    at new ECSServiceStack (C:\Users\Documents\GitHub\cdk-workshop-ts\lib\cdk-workshop-ts-stack.ts:43:5)
    at Object.<anonymous> (C:\Users\Documents\GitHub\cdk-workshop-ts\bin\cdk-workshop-ts.ts:7:1)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module.m._compile (C:\Users\Documents\GitHub\cdk-workshop-ts\node_modules\ts-node\src\index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users \Documents\GitHub\cdk-workshop-ts\node_modules\ts-node\src\index.ts:1621:12)

What settings do I need to modify to resolve this error message?

I am using an ApplicationLoadBalancedFargateService with this ECS image.


Solution

  • It's telling you it doesn't know which VPC the load balancer is on. So it can't create the cluster, service, and task, in the right VPC where the load balancer can access them.

    You should specify your VPC in fromApplicationLoadBalancerAttributes. For example:

    const alb =
      elbv2.ApplicationLoadBalancer.fromApplicationLoadBalancerAttributes(
        this,
        "ImportedALB",
        {
          securityGroupId: "sg-XXXXXXXX",
          loadBalancerArn:
            "arn:aws:elasticloadbalancing:us-east-1:XXXXXXX:listener/app/alb-appliance/XXXXX/XXXXX",
          vpc: ec2.Vpc.fromLookup(this, "ImportedVpc", { vpcName: "my-vpc" })
        }
      );