Skip to content

Instantly share code, notes, and snippets.

@a3linux
Created January 5, 2021 09:14
Show Gist options
  • Save a3linux/3a83a9b6a62cf927838121c2c90eb0f4 to your computer and use it in GitHub Desktop.
Save a3linux/3a83a9b6a62cf927838121c2c90eb0f4 to your computer and use it in GitHub Desktop.
#!/bin/bash
function usage()
{
echo "Usage: $0 [options] [--]
Options:
-h|help Display this message
-r|region AWS Region
-n|loadbalancername Load balancer name
-p|pathpattern Path pattern"
}
while getopts ":hr:n:p:" opt;
do
case ${opt} in
h|help ) usage; exit 0 ;;
r|region ) REGION=$OPTARG ;;
n|loadbalancername ) LBNAME=$OPTARG ;;
p|pathpattern ) PATHPATTERN=$OPTARG ;;
* ) echo -e "\n Option does not exist : $OPTARG\n"
usage; exit 1 ;;
esac
done
shift $(($OPTIND-1))
if [ -z $REGION ] || [ -z $LBNAME ] || [ -z $PATHPATTERN ]; then
usage
exit 1
fi
LBARN=`aws elbv2 describe-load-balancers --region $REGION --name $LBNAME --output json | jq -r .LoadBalancers[0].LoadBalancerArn`
echo "Process Load balancer $LBARN"
LBLISTENERs=`aws elbv2 describe-listeners --load-balancer-arn $LBARN --output json | jq -r .Listeners[].ListenerArn`
for listener in $LBLISTENERs
do
echo "Go through listener: $listener"
RULEs=`aws elbv2 describe-rules --listener-arn $listener --output json | jq -r .Rules[].RuleArn`
for rule in $RULEs
do
PATH_PATTERN_VAL=`aws elbv2 describe-rules --rule-arns $rule --output json | jq -r .Rules[0].Conditions[0].Values[0]`
if [ $PATH_PATTERN_VAL == $PATHPATTERN ]; then
echo "Found a rule matched the pattern"
echo "ARN, Priority and Pattern as follow,"
aws elbv2 describe-rules --rule-arns $rule --output json | jq -r ".Rules[0] | .RuleArn, .Priority, .Conditions "
FIRST_RULE_ARN=`aws elbv2 describe-rules --listener-arn $listener --output json | jq -r ".Rules | sort_by(.Priority | tonumber? // .) | .[0].RuleArn "`
FIRST_RULE_PRIORITY=`aws elbv2 describe-rules --rule-arns $FIRST_RULE_ARN --output json | jq -r ".Rules[0].Priority"`
echo "The current listenr highest prority rule is $FIRST_RULE_ARN with the priority $FIRST_RULE_PRIORITY"
echo "You can change/set the Priority of $rule by, "
echo ""
echo " aws elbv2 set-rule-priorities --rule-priorites RuleArn=$rule,Priority=<number_less_than_$FIRST_RULE_PRIORITY>"
echo ""
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment