Skip to content

Instantly share code, notes, and snippets.

@tcarreira
Created February 22, 2022 14:19
Show Gist options
  • Save tcarreira/4334f81e698adf82ca664badc4700c69 to your computer and use it in GitHub Desktop.
Save tcarreira/4334f81e698adf82ca664badc4700c69 to your computer and use it in GitHub Desktop.
Terraform wrapper for temporarily disable 'prevent_destroy = true'
#!/bin/sh
BASE_DIR=$(dirname "$0")
_cleanup() {
find "${BASE_DIR}" -type f -name "*.tf" -exec sh -c 'sed -i "s@prevent_destroy = false #REPLACED@prevent_destroy = true@g" $1' shell {} \;
}
_replace_prevent_destroy() {
find "${BASE_DIR}" -type f -name "*.tf" -exec sh -c 'sed -i "s@prevent_destroy = true@prevent_destroy = false #REPLACED@g" $1' shell {} \;
}
_main() {
trap _cleanup EXIT # always cleanup, even when crashing
_replace_prevent_destroy
terraform "$@"
# sleep 300 # Test the cleanup by uncommenting this out and then ctrl+C the process
}
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: this is just a terraform wrapper."
echo " Consider '$0' as an alias for 'terraform'"
echo " The only difference is that it will remove temporarily all 'prevent_destroy = true'"
echo " and restore it after the command has finished"
else
_main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment