-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcleanup.sh
executable file
·89 lines (77 loc) · 2.65 KB
/
cleanup.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
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
PROFILE=default
REGION=ca-central-1
STACK_NAME=SEC405
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "Cleans up the workshop CloudFormation stacks by deleting the resources and infrastructure that were created."
echo " "
echo "options:"
echo "-h, --help shows this help"
echo "-p, --profile PROFILE specify an AWS CLI profile to use"
echo "-r, --region REGION AWS region to use for the specified profile"
echo "-s, --stack-name STACK_NAME CloudFormation stack name to use"
exit 0
;;
-p|--profile)
shift
if test $# -gt 0; then
export PROFILE=$1
else
echo "No profile specified"
exit 1
fi
shift
;;
-r|--region)
shift
if test $# -gt 0; then
export REGION=$1
else
echo "No region specified"
exit 1
fi
shift
;;
-s|--stack-name)
shift
if test $# -gt 0; then
export STACK_NAME=$1
else
echo "No stack name specified"
exit 1
fi
shift
;;
*)
break
;;
esac
done
# Delete S3 buckets first, otherwise stack deletion will fail
BUCKETS_TO_DELETE=("TuplesBucket")
for bucket in "${BUCKETS_TO_DELETE[@]}"
do
bucket_desc=$(aws --profile ${PROFILE} --region ${REGION} cloudformation describe-stack-resource --stack-name ${STACK_NAME} --logical-resource-id ${bucket})
if [[ -n ${bucket_desc} ]]; then
actual_bucket_name=$(echo ${bucket_desc} | python -c "import sys, json; print json.load(sys.stdin)['StackResourceDetail']['PhysicalResourceId']")
echo ${acutal_bucket_name}
if [[ -n ${actual_bucket_name} ]]; then
echo "Deleting the S3 bucket ${bucket} (${actual_bucket_name})"
aws --profile ${PROFILE} --region ${REGION} s3 rb s3://${actual_bucket_name} --force
fi
fi
done
STACKS_TO_DELETE=(
${STACK_NAME}
)
for stack in "${STACKS_TO_DELETE[@]}"
do
echo "Deleting the CloudFormation stack ${stack}"
# Delete the stack
aws --profile ${PROFILE} --region ${REGION} cloudformation delete-stack --stack-name ${stack}
# Wait for completion
# (this command didn't exist in older versions of the AWS CLI, so ignore any errors)
aws --profile ${PROFILE} --region ${REGION} cloudformation wait stack-delete-complete --stack-name ${stack} || true
done