whitebear
whitebear

Reputation: 12433

ALB listener requires at least one targetGroup

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

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

Upvotes: 2

Views: 3665

Answers (1)

Geoffrey Wiseman
Geoffrey Wiseman

Reputation: 5637

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.

Upvotes: 6

Related Questions