Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Value for Listener Priority in AWS ELB Rules

I'm trying to create multiple Listener Rules in AWS ELB through the JavaScript SDK. The required priority field requires you to add a priority. And I don't want to have to check what the existing rule priorities are every time I want to add a new rule.

I was wondering if anyone knew a way to set a default value. Let's say I have Rules with priorities 1 2 3, is there a way that I can set it so that the next rule that I add will have a priority of 4?

I've tried one work around where I set the priority to be a really high number, which in AWS, it will push it back to Rules 1 2 3. But in the system itself, the priority is still stored as that big number i gave it.

The createRule() function I'm using can be found here

like image 310
Serey Avatar asked Dec 23 '22 14:12

Serey


1 Answers

I ran into this question on my own and thought I would share my solution since this question hasn't been answered in almost a year. As far as I can tell, there is no way to do this just using AWS, but with a little extra logic we can automate it.

RULE_COUNT=$(aws elbv2 describe-rules --listener-arn "${LISTENER_ARN}" | jq ".Rules | length")
aws elbv2 create-rule --listener-arn "${LISTENER_ARN}" --cli-input-json "${RULE_TEMPLATE}" --priority "${RULE_COUNT}"

jq is the json parser that's part of nearly all *nix based systems, and we can use it to determine the number of rules that awscli returns. There should be n + 1 rules for the n rules that you have defined along with one default rule that AWS always keeps as the lowest, default priority. So the next priority according to your question will simply be the total number of rules in AWS.


EDIT: After a little more time working with this I found out that Amazon doesn't necessarily keep the priorities in any predictable manner. The first answer only works if the priorities are contiguous and occupy the minimum possible priorities. For example, if you have n rules then the code assumed that the range of priorities will always be 1..n. It turns out though that Amazon's priorities can seemingly be anything and sometimes AWS will change all of the priority numbers (but keep everything in the same order) when editing, adding, or deleting a rule.

I had a case where I had 8 rules and Amazon originally gave them priorities of 1-8 as expected, but then after editing the order of the rules through the console Amazon decided to change the priorities of the rules to 2-9. Even worse after playing around with it and trying to learn the system I eventually got to a point where Amazon gave me priorities 11-16 and 19-20 which is where I gave up and tried to figure out a better system.

RULE_COUNT=$(aws elbv2 describe-rules --listener-arn "${LISTENER_ARN}" | jq -r '[.Rules[].Priority][0:-1] | map(.|tonumber) | max + 1')
aws elbv2 create-rule --listener-arn "${LISTENER_ARN}" --cli-input-json "${RULE_TEMPLATE}" --priority "${RULE_COUNT}"

This code basically finds the priorities of all of the different rules, puts them in a list, finds the largest priority in the list and adds one to it. This should work if your priority list gets thrown out of wack like mine was, though really I wish that AWS would just add a simple way of adding a rule without having to care about shifting around the priorities of everything else or having to parse their JSON to figure out where the next available priority is in a noncontiguous array.

like image 97
Brandon Harrison Avatar answered Feb 05 '23 04:02

Brandon Harrison