-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.sh
More file actions
executable file
·107 lines (96 loc) · 4.05 KB
/
cleanup.sh
File metadata and controls
executable file
·107 lines (96 loc) · 4.05 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# =============================================================================
# Cleanup script for SageMaker MLflow Portal
# Destroys all CDK stacks and deletes the serverless MLflow App
# Run this to tear down the entire setup
# =============================================================================
set -e
STACK_PREFIX="sagemaker-infra"
REGION="${CDK_DEFAULT_REGION:-us-east-1}"
echo "============================================"
echo " SageMaker MLflow Portal - Full Cleanup"
echo "============================================"
echo ""
echo "WARNING: This will destroy ALL resources including:"
echo " - EC2 instance and ALB (Flask App stack)"
echo " - Serverless MLflow App"
echo " - SageMaker Domain and user profiles"
echo " - VPC and networking resources"
echo " - S3 helper scripts bucket (auto-deleted)"
echo ""
echo " NOTE: The MLflow artifacts S3 bucket has RETAIN policy"
echo " and will NOT be deleted. Delete it manually if needed."
echo ""
read -p "Are you sure you want to proceed? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
echo "Cleanup cancelled."
exit 0
fi
echo ""
# Derive MLflow App name from SageMaker Domain ID
echo "Resolving MLflow App name from SageMaker Domain stack..."
DOMAIN_ID=$(aws cloudformation describe-stacks \
--stack-name "${STACK_PREFIX}-domain" \
--region "$REGION" \
--query "Stacks[0].Outputs[?OutputKey=='DomainId'].OutputValue" \
--output text 2>/dev/null || true)
if [ -n "$DOMAIN_ID" ] && [ "$DOMAIN_ID" != "None" ]; then
MLFLOW_APP_NAME="mlflow-app-${DOMAIN_ID}"
echo " Domain ID: $DOMAIN_ID"
echo " MLflow App Name: $MLFLOW_APP_NAME"
else
echo " WARNING: Could not resolve Domain ID from stack outputs."
echo " Falling back to legacy app name 'sagemaker-mlflow-app'."
MLFLOW_APP_NAME="sagemaker-mlflow-app"
fi
echo ""
# Step 1: Destroy Flask App stack first (depends on others)
echo "[Step 1/5] Destroying Flask App stack..."
cdk destroy ${STACK_PREFIX}-flaskapp --force 2>/dev/null || echo " Flask App stack not found or already deleted. Skipping."
echo ""
# Step 2: Delete the serverless MLflow App via AWS CLI
echo "[Step 2/5] Deleting serverless MLflow App..."
MLFLOW_APP_ARN=$(aws sagemaker list-mlflow-apps \
--region "$REGION" \
--no-paginate 2>/dev/null | python3 -c "
import json, sys
data = json.load(sys.stdin)
for app in data.get('Summaries', []):
if app.get('Name') == '$MLFLOW_APP_NAME':
print(app.get('Arn', ''))
break
" 2>/dev/null || true)
if [ -n "$MLFLOW_APP_ARN" ]; then
echo " Found MLflow App: $MLFLOW_APP_ARN"
aws sagemaker delete-mlflow-app \
--arn "$MLFLOW_APP_ARN" \
--region "$REGION" 2>/dev/null || echo " Failed to delete MLflow App. You may need to delete it manually."
echo " MLflow App deletion initiated."
echo " Waiting 60 seconds for MLflow App to be fully deleted..."
sleep 60
else
echo " No MLflow App found with name '$MLFLOW_APP_NAME'. Skipping."
fi
echo ""
# Step 3: Destroy MLflow Resources stack (IAM role + S3 bucket)
echo "[Step 3/5] Destroying MLflow Resources stack..."
cdk destroy ${STACK_PREFIX}-mlflow --force 2>/dev/null || echo " MLflow stack not found or already deleted. Skipping."
echo ""
# Step 4: Destroy SageMaker Domain stack
echo "[Step 4/5] Destroying SageMaker Domain stack..."
cdk destroy ${STACK_PREFIX}-domain --force 2>/dev/null || echo " Domain stack not found or already deleted. Skipping."
echo ""
# Step 5: Destroy Networking stack (VPC, subnets, NAT gateway)
echo "[Step 5/5] Destroying Networking stack..."
cdk destroy ${STACK_PREFIX}-networking --force 2>/dev/null || echo " Networking stack not found or already deleted. Skipping."
echo ""
echo "============================================"
echo " Cleanup Complete!"
echo "============================================"
echo ""
echo "All stacks have been destroyed."
echo ""
echo "Remaining manual cleanup (if needed):"
echo " - MLflow artifacts S3 bucket (RETAIN policy — check S3 console)"
echo " - Any CloudWatch log groups created by Lambda functions"
echo " - CDK bootstrap stack (if you no longer need CDK in this account)"