Skip to content

Commit

Permalink
Detach load balancer from ECS services before deletion; bump version …
Browse files Browse the repository at this point in the history
…to 0.5.50
  • Loading branch information
sinanartun committed Jan 3, 2025
1 parent 1f7eec2 commit 31c5b9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,9 @@ def delete_load_balancer(self, region_name):
LoadBalancerNames=[x['LoadBalancerName']]
)

# Detach from other services
self.detach_load_balancer_from_services(x['LoadBalancerArn'], region_name)

# Delete the load balancer
try:
res1 = client.delete_load_balancer(
Expand All @@ -1602,7 +1605,19 @@ def delete_load_balancer(self, region_name):
except client.exceptions.ResourceInUseException as e:
logger.error(f"Load balancer cannot be deleted: {e}")

def detach_load_balancer_from_services(self, load_balancer_arn, region_name):
client = boto3.client('elbv2', region_name=region_name)
# Detach from ECS services
ecs_client = boto3.client('ecs', region_name=region_name)
clusters = ecs_client.list_clusters()['clusterArns']
for cluster in clusters:
services = ecs_client.list_services(cluster=cluster)['serviceArns']
for service in services:
ecs_client.update_service(cluster=cluster, service=service, loadBalancers=[])
logger.info(f"Detached load balancer {load_balancer_arn} from ECS service {service} in cluster {cluster}")

# Detach from other services if needed
# Add additional detachment logic here if necessary


def delete_target_groups(self, region_name):
Expand Down Expand Up @@ -2108,8 +2123,7 @@ def delete_all_notebook_instances(self, region_name):

print(f"Method do not exists {region_name} and was called successfully.")
return False
else:
print(f"Method exists {region_name} and was called successfully.")


# Get list of notebook instances
response = sagemaker_client.list_notebook_instances()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='aws-delete-all',
version='0.5.49',
version='0.5.50',
author='Sinan Artun',
author_email='[email protected]',
description='A script that concurrently deletes common AWS resources like S3 buckets, RDS instances, and EC2 instances across all AWS regions.',
Expand Down

0 comments on commit 31c5b9b

Please sign in to comment.