-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelete_eks_cluster.py
More file actions
74 lines (55 loc) · 3 KB
/
Copy pathdelete_eks_cluster.py
File metadata and controls
74 lines (55 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import boto3
import os
import json
def lambda_handler(event, context):
# Set the AWS region
#region = os.environ['Region']
regions = [ "us-east-1", "us-west-1", "ap-south-1"]
# global variable
message = ""
for region in regions:
message = message + f'{region}: '
# Create clients for the EKS, EC2, and ELBv2 services
context.eks_client = boto3.client('eks', region_name=region)
# List all EKS clusters
clusters = context.eks_client.list_clusters()["clusters"]
print(f"Listed Clusters in {region}:\n{clusters}")
message = message + f"Listed Clusters in {region}:\n{clusters}"
# Iterate over the clusters
for cluster in clusters:
# Get the cluster name
cluster_name = cluster
try:
# Get the list of tags for the EKS cluster
response = context.eks_client.describe_cluster(name=cluster_name)
tags = response['cluster']['tags']
# Check if the cluster has any tags
if not tags:
# Get the list of nodes in the cluster
response = context.eks_client.list_nodegroups(clusterName=cluster_name)
nodegroups = response['nodegroups']
# Get the list of Fargate profiles for the cluster
response = context.eks_client.list_fargate_profiles(clusterName=cluster_name)
profiles = response['fargateProfileNames']
if (len(nodegroups) == 0) and (len(profiles) == 0 ):
try:
response = context.eks_client.delete_cluster(name=cluster_name)
print(response)
print(f"Cluster {cluster_name} has been terminated.")
message = message + f"\nCluster {cluster_name} has been terminated."
except Exception as e:
print(f'Error deleting Eks Cluster {cluster_name}: {e}')
message = message + f'Error deleting Eks Cluster {cluster_name}: {e}'
else:
print(f'Error deleting Eks Cluster {cluster_name}: {e}')
message = message + f'Error deleting Eks Cluster {cluster_name}: {e}'
else:
print(f'Cluster {cluster_name} has tags, So the cluster {cluster_name} is not being deleted...')
message = message + f'Cluster {cluster_name} has tags, So the cluster {cluster_name} is not being deleted...'
except Exception as e:
# Print the error message if there is a problem
print(f'Error deleting the cluster {cluster_name}: {e}')
message = message + f'Error deleting the cluster {cluster_name}: {e}'
return {
'message' :json.dumps(message, default=str)
}