mirror of
https://github.com/RogerSik/aws-target-group-cleanup.git
synced 2024-11-23 16:20:27 +01:00
Pretty well done
This commit is contained in:
parent
ad1a002d1c
commit
d792ca8df5
78
src/aws-target-group-cleanup.py
Normal file
78
src/aws-target-group-cleanup.py
Normal file
@ -0,0 +1,78 @@
|
||||
import sys
|
||||
import boto3
|
||||
import argparse
|
||||
import pprint
|
||||
|
||||
|
||||
def remove_target_group(arn, elb_client):
|
||||
request_id = None
|
||||
|
||||
try:
|
||||
response = elb_client.delete_target_group(
|
||||
TargetGroupArn=arn,
|
||||
)
|
||||
except Exception as e:
|
||||
print("Error removing target group " + arn + " (" + str(e) + ")")
|
||||
|
||||
if response:
|
||||
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
|
||||
request_id = response['ResponseMetadata']['RequestId']
|
||||
|
||||
return request_id
|
||||
|
||||
|
||||
def main(argv):
|
||||
plugin_results = dict()
|
||||
|
||||
parser = argparse.ArgumentParser(description='Remove ALB target groups not assigned to load balancers')
|
||||
parser.add_argument('-f','--force', help='Perform the actual deletes (will run in dryrun mode by default)', action='store_true')
|
||||
parser.add_argument('-r','--region', help='AWS region to process', required=True)
|
||||
parser.add_argument('-p','--prefix', help='Only process target groups with this prefix in their name', required=False)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.prefix:
|
||||
print("Prefix specified - only evaluating target groups beginning with " + args.prefix)
|
||||
|
||||
if args.force:
|
||||
print("Force flag specified - doing deletion")
|
||||
|
||||
session = boto3.session.Session(region_name=args.region)
|
||||
elb_client = session.client("elbv2")
|
||||
|
||||
tg_paginator = elb_client.get_paginator('describe_target_groups')
|
||||
tg_iterator = tg_paginator.paginate()
|
||||
for response in tg_iterator:
|
||||
# pprint.pprint(response)
|
||||
|
||||
for target_group in response['TargetGroups']:
|
||||
punt = False
|
||||
|
||||
target_group_name = target_group['TargetGroupName']
|
||||
target_group_load_balancers = target_group['LoadBalancerArns']
|
||||
target_group_arn = target_group['TargetGroupArn']
|
||||
|
||||
if args.prefix:
|
||||
if target_group_name.startswith(args.prefix):
|
||||
if not target_group_load_balancers:
|
||||
punt = True
|
||||
else:
|
||||
if not target_group_load_balancers:
|
||||
punt = True
|
||||
|
||||
if punt:
|
||||
sys.stdout.write("Target group " + target_group_name + " has no load balancers - removing....")
|
||||
|
||||
if args.force:
|
||||
request_id = remove_target_group(target_group_arn, elb_client)
|
||||
if request_id:
|
||||
print("REMOVED (request id " + request_id + ")")
|
||||
else:
|
||||
print("ERROR")
|
||||
else:
|
||||
print("DRYRUN,no deletion took place")
|
||||
|
||||
print "Complete"
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
1
src/requirements.txt
Normal file
1
src/requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
boto3
|
Loading…
Reference in New Issue
Block a user