Skip to content

Instantly share code, notes, and snippets.

@AlienHoboken
Created August 19, 2020 21:03
Show Gist options
  • Save AlienHoboken/60db4572f087f82446a5c64e617386d6 to your computer and use it in GitHub Desktop.
Save AlienHoboken/60db4572f087f82446a5c64e617386d6 to your computer and use it in GitHub Desktop.
Bash script to automatically remove attributes from terraform state file
set -e
if [ $# -eq 0 ]; then echo "No command specified\nAvailable commands:\n* remove [attribute_name]\n* commit\n"; exit; fi
if [ $1 = "remove" ]; then
if [ $# -eq 1 ]; then
echo "No attribute_name specified to remove\n"
exit
fi
terraform state pull > state.json
jq "(.resources[].instances[] |= del(.attributes.${2})) | (.serial |= .+1)" state.json > state.new.json
echo "Please review diff and run \"tf_remove_attrs.sh commit\" to continue\n"
diff state.json state.new.json
fi
if [ $1 = "commit" ]; then
echo "Commiting state.new.json to workspace\n"
terraform state push state.new.json
rm state.new.json
rm state.json
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment