Skip to content

Instantly share code, notes, and snippets.

@Ameausoone
Last active October 9, 2019 07:02
Show Gist options
  • Save Ameausoone/addec3d6190292bc571dfdbe7e15937b to your computer and use it in GitHub Desktop.
Save Ameausoone/addec3d6190292bc571dfdbe7e15937b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Check if there is some delete actions in plan, and apply it if not
# In Docker image :
# FROM hashicorp/terraform:0.12.9
# RUN apk add jq
set -eo pipefail
# Output tfplan
terraform plan -out=tfplan -input=false
# Convert to json and prettify it
terraform show -json tfplan | jq > tfplan.json
# Check if there is some delete actions
if jq --exit-status --raw-output -c '.resource_changes[] | select( (.change.actions[] | contains ("delete")))' tfplan.json >/dev/null; then
echo "There is some delete actions, terraform apply has to be apply manually"
else
echo "Could apply terraform plan"
terraform apply -input=false -auto-approve tfplan
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment