Created
November 16, 2017 01:44
-
-
Save b-b3rn4rd/27d8b47d48f761a248082f4ca662c4e3 to your computer and use it in GitHub Desktop.
auto-increment ALB ListenerRule priority
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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