Skip to content

Instantly share code, notes, and snippets.

@immanuelpotter
Created December 6, 2023 09:33
Show Gist options
  • Save immanuelpotter/9812f2ea56057f0324ac5110bfb11022 to your computer and use it in GitHub Desktop.
Save immanuelpotter/9812f2ea56057f0324ac5110bfb11022 to your computer and use it in GitHub Desktop.
Remove dangling resources that can affect terraform after nuking an AWS account.
#!/bin/bash
set -e
workspace="$1"
environment="$2"
state_bucket="${3:-terraform-state-bucket}"
usage(){
echo "Usage: $0 [workspace_name] [environment_name] (state_bucket_optional)"
exit 3
}
delete_things(){
aws s3 rm --recursive s3://${state_bucket}/state_subdirectory/${workspace}/
aws dynamodb delete-item --table-name terraform-state-lock --key '{"LockID": {"S": "'${state_bucket}/state_subdirectory/${workspace}/state_file.tfstate'"}}'
aws dynamodb delete-item --table-name terraform-state-lock --key '{"LockID": {"S": "'${state_bucket}/state_subdirectory/${workspace}/state_file.tfstate-md5'"}}'
# KMS goes here when I know what to delete
}
main(){
if [[ -z $workspace || -z $environment || -z $state_bucket ]] ; then
usage
fi
echo "AWS Profile: ${AWS_PROFILE}"
echo "Workspace: ${workspace}"
echo "Environment: ${environment}"
read -p "Continue? " yn
case $yn in
[Yy]* ) delete_things;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment