Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(ElasticLoadBalancingV2): CDK to automatically calculate priority for host-based load balancer rules #12067

Open
mimozell opened this issue Dec 14, 2020 · 16 comments
Labels
@aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 effort/medium Medium work item – several days of effort feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. feature-request A feature should be added or improved. p1

Comments

@mimozell
Copy link

Use Case

When creating a host-based load balancer rule, I really don't care about the rule priority because I rely on exact matches, so I don't want to have to keep track of the priority values and make sure I don't try to set the same value twice or else I'll get an error on deployment. I realize this is a problem with AWS itself (it should really be fixed there so no one would have to do anything custom like today), but other tools like Terraform have solved this problem internally so that the end user doesn't have to care about it. It would be great if CDK did this by default as well, and the user would provide the value if there's an exception to the rule.

Desired behavior:

When creating a load balancer rule like this:

        ApplicationListenerRule.Builder.create(this, "Listener Rule")
            .action(ListenerAction.forward(listOf(defaultTargetGroup)))
            .conditions(listOf(ListenerCondition.hostHeaders(listOf(hostName))))
            .listener(httpsListener)
            .priority(1)
            .build()

I shouldn't have to add the priority myself as I don't rely on priority matches when using host-based rules.


This is a 🚀 Feature Request

@mimozell mimozell added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Dec 14, 2020
@github-actions github-actions bot added the @aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 label Dec 14, 2020
@njlynch
Copy link
Contributor

njlynch commented Dec 15, 2020

Thanks for the feature request.

This seems reasonable; we could keep track of existing rules, and if no priority is specified, set the priority based on the count (and/or priority) of existing rules. One "gotcha" will be that we currently use the presence of priority in ApplicationListener.addAction to determine if we create a rule or not. That can likely be handled, however.

@njlynch njlynch added effort/medium Medium work item – several days of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels Dec 15, 2020
@mimozell
Copy link
Author

Sounds good. I would just increment by one the highest priority used by any existing rule. There will be issues with concurrency - if multiple stacks are trying to create a rule with the same generated priority at the exact same time, then that will fail, but perhaps a good error message is good enough? Ideally there would also be some automatic retries with the next available priority, but that's not absolutely necessary to have as long as the error message is there.

@ericzbeard ericzbeard added the feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. label Apr 2, 2021
@efrodriguez
Copy link

efrodriguez commented Mar 29, 2022

Any update on this? @njlynch have you found a workaround for this?

@metametadata
Copy link

Our current workaround is to assign a priority based on hashing the rule's unique ID. In Clojure:

rule-id (str domain "-rule")
priority (mod (hash rule-id) 50001) ; Range: 0-50000

...

(-> (ApplicationListenerRule$Builder/create stack rule-id)
     ...
     (.conditions [(ListenerCondition/hostHeaders [domain])])
     (.priority priority)
     .build)

It's prone to hash collisions but we were lucky so far.

@Kazpers
Copy link

Kazpers commented Sep 26, 2022

Is there anything new on this one @ericzbeard @rix0rrr or others? It's a serious deficiency (although I know it's the fault of underlying AWS, not cdk), that makes it extremely hard to use CDK for deployments involving multiple listener rules. We really don't want to micro-manage priority for hundreds of services that each have a forward rule on the ALBs...

The workaround with hashes is... fragile. I appreciate it as a temporary measure, but I really don't want fragility in my ops deployments...

@github-actions github-actions bot added p1 and removed p2 labels Mar 5, 2023
@github-actions
Copy link

github-actions bot commented Mar 5, 2023

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

@juinquok
Copy link
Contributor

Just wondering if anyone has any idea if using AwsCustomResource will be able to solve this problem? If so, it might be possible to use a aws-sdk call via the custom resource to derive the rule priority. Else, if anyone can point me in the right direction of where the aws-sdk can be added in the aws-cdk codebase, I would be happy to give this a go.

@luxaritas
Copy link
Contributor

I want to add my support for this as well. I wound up implementing this by having a global counter my stacks could reference, but I wound up realizing that if I reordered or inserted a stack between stacks that used it, deployments would fail since all of the stacks would change their priorities (and the first stack would try to take the ID already assigned to the second stack, etc). Would be great to have the CDK figure it out!

@adamghowiba
Copy link

adamghowiba commented Jul 5, 2023

I'd also like to add my support for this. It's very difficult to handle ALB priorities at CDK level. I also implemented a solution similar to @luxaritas, which works well until I reorder my ALB rules.

I can't seem to come up with a temporary solution, aside from removing ALB rules beforehand, utilizing hashing, or making AWS API calls before running deployments. All solutions are problematic.

Would love to see a feature like this implemented.

@MayconFrr
Copy link

MayconFrr commented Jul 24, 2023

Bump

Edit: Also would be nice to have a parameter to set if I want to get the first available priority using a first-to-last or last-to-first strategy

@marwan-alloreview
Copy link

marwan-alloreview commented Sep 27, 2023

Would also be very interested by this

@timothy-cloudopsguy
Copy link

timothy-cloudopsguy commented Oct 3, 2023

Attempted this with 146 domains, and had 9 collisions.

def hash_modulus(i: str, n: int):
    snum = 0
    for c in i:
        snum += ord(c)
    m = snum % n
    return m

where i == domain name, and n == 50001

Also, FWIW... when deploying the same stack that creates iam roles/policies to us-east-2 and us-west-2 i get collisions with the internal naming function in CDK. So whatever they use to unique/hash also is collision prone.

@metametadata
Copy link

This is a poor hashing algorithm as it seems to lack uniform distribution: https://web.stanford.edu/class/archive/cs/cs106b/cs106b.1172/handouts/8-Hashing.pdf.

@rogerchi
Copy link
Contributor

FYI, this is how I've solved it for our use cases: https://github.com/rogerchi/cdk-listener-next-available-priority

@n-miles
Copy link

n-miles commented Apr 25, 2024

Adding another bump to the pile

@Kazpers
Copy link

Kazpers commented Apr 27, 2024

The lack of action (or even an update) on this after over 3 years is very disappointing. How can we take CDK seriously as an ops tool this way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 effort/medium Medium work item – several days of effort feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. feature-request A feature should be added or improved. p1
Projects
None yet
Development

No branches or pull requests