-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclean-up.sh
executable file
·76 lines (71 loc) · 2.82 KB
/
clean-up.sh
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
75
76
#!/bin/bash
# SPDX-FileCopyrightText: Copyright 2021 Amazon.com, Inc. or its affiliates.
# SPDX-License-Identifier: MIT-0
echo "delete EMR on EKS IAM execution role "
export ROLE_NAME=${EMRCLUSTER_NAME}-execution-role
export POLICY_ARN=arn:aws:iam::$ACCOUNTID:policy/${ROLE_NAME}-policy
aws iam detach-role-policy --role-name $ROLE_NAME --policy-arn $POLICY_ARN
aws iam delete-role --role-name $ROLE_NAME
aws iam delete-policy --policy-arn $POLICY_ARN
echo "delete Karpenter role"
export K_ROLE_NAME=${EKSCLUSTER_NAME}-karpenter
export K_POLICY_ARN=arn:aws:iam::$ACCOUNTID:policy/KarpenterControllerPolicy-${EKSCLUSTER_NAME}
aws iam detach-role-policy --role-name $K_ROLE_NAME --policy-arn $K_POLICY_ARN
aws iam delete-role --role-name $K_ROLE_NAME
aws iam delete-policy --policy-arn $K_POLICY_ARN
echo "delete S3"
aws s3 rm s3://$S3BUCKET --recursive
aws s3api delete-bucket --bucket $S3BUCKET
echo "delete Grafana workspace"
WID=$(aws grafana list-workspaces --query "workspaces[?name=='$EMRCLUSTER_NAME'].id" --output text)
if ! [ -z "$WID" ]; then
for id in $WID; do
sleep 2
echo "Delete $id"
aws grafana delete-workspace --workspace-id $id
done
fi
echo "delete Prometheus worksapce"
PID=$(aws amp list-workspaces --alias $EKSCLUSTER_NAME --query workspaces[].workspaceId --output text)
if ! [ -z "$PID" ]; then
for id in $PID; do
sleep 2
echo "Delete $id"
aws amp delete-workspace --workspace-id $id
done
fi
echo "delete ALB"
vpcId=$(aws ec2 describe-vpcs --filters Name=tag:"karpenter.sh/discovery",Values=$EKSCLUSTER_NAME --query "Vpcs[*].VpcId" --output text)
ALB=$(aws elbv2 describe-load-balancers --query "LoadBalancers[?VpcId=='$vpcId'].LoadBalancerArn" --output text)
if ! [ -z "$ALB" ]; then
for alb in $ALB; do
sleep 2
echo "Delete $alb"
aws elbv2 delete-load-balancer --load-balancer-arn $alb
done
fi
TG=$(aws elbv2 describe-target-groups --query "TargetGroups[?VpcId=='$vpcId'].TargetGroupArn" --output text)
if ! [ -z "$TG" ]; then
for tg in $TG; do
sleep 2
echo "Delete Target groups $tg"
aws elbv2 delete-target-group --target-group-arn $tg
done
fi
# delete cloud9
env_ls=$(aws cloud9 list-environments --query environmentIds --output text)
if ! [ -z "$env_ls" ]; then
for l in $env_ls; do
ts=$(aws cloud9 describe-environments --environment-ids $l --query "environments[?name=='workshop-env']")
if ! [ -z "$ts" ]; then
aws cloud9 delete-environment --environment-id $l
fi
done
fi
echo "delete karpenter"
aws cloudformation delete-stack --stack-name Karpenter-$EKSCLUSTER_NAME
echo "delete EKS cluster"
eksctl delete cluster --name $EKSCLUSTER_NAME
echo "delete EMR virtual cluster"
export VIRTUAL_CLUSTER_ID=$(aws emr-containers list-virtual-clusters --query "virtualClusters[?name == '${EMRCLUSTER_NAME}' && state == 'RUNNING'].id" --output text)
aws emr-containers delete-virtual-cluster --id $VIRTUAL_CLUSTER_ID