user15013406
user15013406

Reputation: 168

AWS CDK - Existing Application Load Balancer Listener - How To Insert Rule

I have an application load balancer with a HTTPS Listener that was created before I started using CDK.

The listener already has 13 rules which route requests based on hostname to different fargate instances. The last rule is a fallback rule.

Now I want to insert new rules with CDK before the fallback rules. As there will be new rules when adding new fargate services, this must be dynamic. How can I add new rules to an existing listener and how can I get the priority of a specific rule (the fallback rule in this case) to serve as a starting point for the priority calculation

Or could I just add rule after rule all with priority 1 and the priority of the already existing rules would be increased by 1 each?

I retrieved the existing listener this way but I have no idea how to access its rules

const httpsListener = elbv2.ApplicationListener.fromLookup(
  this,
  "ALBListenerHTTPS", {
    listenerArn: "my:aws:alb:listener:arn",
  }
);

Upvotes: 6

Views: 4179

Answers (1)

miensol
miensol

Reputation: 41638

Each ApplicationListener maps to a AWS::ElasticLoadBalancingV2::ListenerRule CloudFormation resource.

Once you have an ApplicationListener you can do define new ApplicationListenerRules:

new ApplicationListenerRule(this, 'Rule1', {
    listener: httpsListener,
    priority: 1
    ...
})

However, for aws-cdk to manage load balancer rules, they all need to be created by it. There are 2 solutions.

Solution A

Import all existing listener rules into CloudFormation stack. The load balancer rule resource supports importing into CloudFormation stack.

Solution B

Change priority of existing listener rules via command line. The aim is to have existing rules priority start with N + 1. Where N is the current count of rules. Doing so creates free space for new rules defined in aws-cdk. With the space in place, you can define copies of existing rules via aws-cdk and deploy them. After they are deployed, you can safely remove manually created rules.

Upvotes: 1

Related Questions