Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Accept sub-path in listener rules #13

Open
wichert opened this issue Mar 12, 2017 · 2 comments
Open

Accept sub-path in listener rules #13

wichert opened this issue Mar 12, 2017 · 2 comments

Comments

@wichert
Copy link
Contributor

wichert commented Mar 12, 2017

I spent way too much time debugging something that ended up being completely trivial: I had missed the fact that the example service only listen for an exact path:

    ListenerRule:
        Type: AWS::ElasticLoadBalancingV2::ListenerRule
        Properties:
            ListenerArn: !Ref Listener
            Priority: 2
            Conditions:
                - Field: path-pattern
                  Values: 
                    - !Ref Path
            Actions:
                - TargetGroupArn: !Ref TargetGroup
                  Type: forward

My services, and most I suspect unless your micro services are really tiny, handle a fair number of subpaths as well, so I needed two listeners rules:

    RootListenerRule:
        Type: AWS::ElasticLoadBalancingV2::ListenerRule
        Properties:
            ListenerArn: !Ref Listener
            Priority: 100
            Conditions:
                - Field: path-pattern
                  Values:
                    - !Ref Path
            Actions:
                - TargetGroupArn: !Ref TargetGroup
                  Type: forward

    SubListenerRule:
        Type: AWS::ElasticLoadBalancingV2::ListenerRule
        Properties:
            ListenerArn: !Ref Listener
            Priority: 101
            Conditions:
                - Field: path-pattern
                  Values:
                    - !Sub ${Path}/*
            Actions:
                - TargetGroupArn: !Ref TargetGroup
                  Type: forward

Would it make sense to do that in the reference templates as well? If so I'll happily submit a PR.

@NightKhaos
Copy link
Contributor

So, this rule matches ${Path} and ${Path}/*, however it takes two rules to do it. If you want to save on rules and reduce this down to only one rule, you could use ${Path}*. ${Path}* will match any string prefixed with with ${Path} which may be undesirable in some circumstances so you need to be careful.

You can work around this somewhat by use of prioritises however. Take for example:

Rule 1
Priority: 201
Pattern:/foo*

Rule 2
Priority: 101
Pattern: /food*

Rule 3
Priority: 301
Pattern: /foobar*

In this example, /foo/page will go to Rule 1, whereas /food/page will go to Rule 2 because Rule 2 will be evaluated first in priority. However, /foobar will be taken by Rule 1, not Rule 3, due to Rule 3 having a lower priority than Rule 1.

As there is a limit of 10 rules per load balancer, it would be a good idea to be frugal on rule usage.

@wichert
Copy link
Contributor Author

wichert commented Mar 24, 2017

@NightKhaos That is true, however I would argue that people with enough rules to run into that limitations are more likely to be capable to come up with this workaround. The risk for unexpected matches for less experienced people is something to think about.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants