Last active
August 7, 2024 10:20
-
-
Save huevos-y-bacon/510b3212fef89f800e1977800d9202d8 to your computer and use it in GitHub Desktop.
Terraform - Based on given strings, destroy Terraform remote backend config (DDB table, S3 buckets, SSM Parameters)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# shellcheck disable=1091,2068,2162 | |
SSMSTRING=backend | |
BUCKSTRING=state | |
DDBSTRING=locks | |
if command -v colours &> /dev/null; then source colours; fi | |
unset COUNT | |
[[ $* == *"--force"* ]] && FORCE=yes | |
[[ $* == *"--check"* ]] && CHECK=yes | |
if [[ -z $CHECK ]]; then | |
echo -e "\n${BOLD}${RED}WARNING: THIS CHECKS FOR AND DESTROYS TERRAFORM BACKEND RESOURCES!${NORM}\n" | |
else echo -e "\nCHECKING FOR TERRAFORM BACKEND RESOURCES\n" | |
fi | |
echo "${CYAN}Checking for strings:${NORM} | |
- ${CYAN}SSMSTRING : ${YELLOW}${SSMSTRING}${NORM} | |
- ${CYAN}BUCKSTRING : ${YELLOW}${BUCKSTRING}${NORM} | |
- ${CYAN}DDBSTRING : ${YELLOW}${DDBSTRING}${NORM}" | |
SSMPATHS=$(aws ssm get-parameters-by-path --path "/" --recursive --out text --query "Parameters[?contains(Name,'${SSMSTRING}')].[Name]") | |
BUCKETS=$(s3-list-buckets ${BUCKSTRING} --quiet) | |
TABLES=$(aws dynamodb list-tables --query 'TableNames[][]' --out text | grep ${DDBSTRING}) | |
echo -e "\n${BOLD}${RED}SSM Params:${NORM}" | |
if (( ${#SSMPATHS[0]} )); then | |
COUNT=1 | |
for s in ${SSMPATHS[@]}; do echo "- ${YELLOW}${s}${NORM}"; done | |
else echo "No ${SSMSTRING} params found" | |
fi | |
echo -e "\n${BOLD}${RED}S3 ${BUCKSTRING} buckets:${NORM}" | |
if (( ${#BUCKETS[0]} )); then | |
COUNT=1 | |
for b in ${BUCKETS[@]}; do echo "- ${YELLOW}${b}${NORM}"; done | |
else echo "No ${BUCKSTRING} buckets found" | |
fi | |
echo -e "\n${BOLD}${RED}Ddb state tables:${NORM}" | |
if (( ${#TABLES[0]} )); then | |
COUNT=1 | |
for t in ${TABLES[@]}; do echo "- ${YELLOW}${t}${NORM}"; done | |
else echo "No ${DDBSTRING} tables found" | |
fi | |
echo | |
if [[ -z $CHECK ]]; then | |
if [[ -n $COUNT ]];then | |
if [[ -z $FORCE ]]; then | |
echo -e "${RED}THIS WILL DELETE ALL THESE RESOURCES" | |
read -p "${YELLOW}Are you sure you want to proceed? (y/n) ${NORM}" choice | |
case "$choice" in | |
y|Y ) ;; | |
* ) echo -e "Aborting\n" && exit 0;; | |
esac | |
echo | |
fi | |
s3_delete_bucket(){ | |
[[ -n $DEBUG ]] && set -x | |
[[ $1 ]] || { echo "specify bucket"; exit 0; } | |
BUCKET=$1 | |
[[ $2 == "--force" ]] && FORCE=true | |
if [[ ! $FORCE ]]; then | |
echo -e "\n${BOLD}This will empty and destroy bucket: ${RED}${BUCKET}${NORM}" | |
echo -e " (This may take a while)\n" | |
read -p "Do you want to proceed? " -n 1 -r | |
echo | |
if [[ ! $REPLY =~ ^[Yy]$ ]] | |
then | |
echo -e "Aborting\n"; exit 0 | |
fi | |
echo -e "${BOLD}${BLUE}Are you 100% sure you intend to destroy bucket: ${RED}${BUCKET} ?${NORM}\n" | |
read -p "Do you want to proceed?" -n 1 -r | |
echo | |
if [[ ! $REPLY =~ ^[Yy]$ ]] | |
then | |
echo -e "Aborting\n"; exit 0 | |
fi | |
fi | |
echo "Emptying bucket ${BOLD}${YELLOW}${BUCKET}${NORM}..." | |
python3 "$(which s3-empty-bucket.py)" "${BUCKET}" || exit | |
echo "Destroying bucket ${BOLD}${YELLOW}${BUCKET}${NORM}..." | |
set -eEo pipefail | |
shopt -s inherit_errexit >/dev/null 2>&1 || true | |
# $@ := bucket_name | |
aws s3 rb "s3://${BUCKET}" --force > /dev/null || exit | |
echo "${BOLD}${BLUE}Bucket: ${BUCKET} destroyed${NORM}" | |
} | |
delete_ssm_param(){ echo "- ${1}"; aws ssm delete-parameter --name "${1}"; } | |
delete_s3_bucket(){ echo "- ${1}"; s3_delete_bucket "${1}" --force; } | |
delete_ddb_table(){ echo "- ${1}"; aws dynamodb delete-table --table-name "${1}" --query 'TableDescription.TableStatus' > /dev/null; } | |
echo -e "${BOLD}${RED}Deleting SSM Params:${NORM}" | |
for s in ${SSMPATHS[@]}; do delete_ssm_param "${s}"; done; echo | |
echo -e "${BOLD}${RED}Deleting S3 ${BUCKSTRING} buckets:${NORM}" | |
for b in ${BUCKETS[@]}; do delete_s3_bucket "${b}"; done; echo | |
echo -e "${BOLD}${RED}Deleting Ddb state tables:${NORM}" | |
for t in ${TABLES[@]}; do delete_ddb_table "${t}"; done; echo | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment