Home > Software engineering >  ALB listener requires at least one targetGroup
ALB listener requires at least one targetGroup

Time:05-15

I made an ApplicationLoadBalancer in a CDK project and want to use this ApplicationLoadBalancer from multiple projects.

For example, I have three cdk project maincdk,app1cdk,app2cdk

  • making an ALB and a listener(main-listener, port 80) in maincdk

  • in app1cdk, add targetGroup(like ECS) to main-listener

  • in app2cdk add targetGroup(like ECS) to main-listener

  • each access is port 80, but I can switch by domain name.

However, in maincdk:

    const lb = new elb.ApplicationLoadBalancer(this, "lb", {
      vpc: vpc,
      loadBalancerName : "main-lb",
      internetFacing: true,
      vpcSubnets: vpc.selectSubnets({ subnetType: ec2.SubnetType.PUBLIC }),
      securityGroup: adminLbSg
    });

    const listener = lb.addListener("main-listener", { port: 80 });

this error occurs:

    [CommonLbStack/lb/main-listener] Listener needs at least one default action or target group (call addTargetGroups or addAction)

It says that the listener should have at least one TargetGroup.

I added a dummy action like this, but the same error occurs:

    listener.addAction('Fixed', {
      priority: 1,
      conditions: [
        elb.ListenerCondition.pathPatterns(['/ok']),
      ],
      action: elb.ListenerAction.fixedResponse(200, {
        messageBody: 'OK',
      })
    });

CodePudding user response:

It says you need at least one:

  • default action
  • or target group

Since you don't want to add a TargetGroup from maincdk, why not add a default action? The fixed action you have above isn't a default action. Try something like:

listener.addAction('DefaultAction', {
  action: elbv2.ListenerAction.fixedResponse(404, {
    contentType: elbv2.ContentType.TEXT_PLAIN,
    messageBody: 'Cannot route your request; no matching project found.',
  }),
});

Then you can layer conditional rules on top of that from the app1cdk and app2cdk projects to route to those projects based on whatever criteria you need -- path, hostname, etc.

See: "Default Rules" on the Listener Configuration docs.

  • Related