Skip to content

Instantly share code, notes, and snippets.

@b-b3rn4rd
Created November 16, 2017 01:44
Show Gist options
  • Save b-b3rn4rd/27d8b47d48f761a248082f4ca662c4e3 to your computer and use it in GitHub Desktop.
Save b-b3rn4rd/27d8b47d48f761a248082f4ca662c4e3 to your computer and use it in GitHub Desktop.
auto-increment ALB ListenerRule priority
#!/usr/bin/python
import boto3
import sys
import os
from operator import itemgetter
def main(listener_arn):
client = boto3.client('elbv2')
rules = client.describe_rules(
ListenerArn=listener_arn,
)['Rules']
rules = [rule for rule in rules if rule['Priority'].isdigit()]
if not rules:
return 1
sorted_rules = sorted(rules, key=lambda x: int(x['Priority']), reverse=True)
priority = int(sorted_rules[0]['Priority'])+1
return priority
if __name__ == '__main__':
try:
if len(sys.argv) < 2:
raise Exception('usage "{} arn:aws:elasticloadbalancing:us-west-2:123456789123:listener/app/alb/xxxxxxxxxx/yyyyyyyyyyy"'.format(os.path.basename(__file__)))
print main(sys.argv[1])
exit(0)
except Exception as e:
print "Error: {}".format(str(e))
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment