Skip to content

Instantly share code, notes, and snippets.

@aprice
Created November 28, 2017 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aprice/12c7bc13a755785f36c9c3e293037d7d to your computer and use it in GitHub Desktop.
Save aprice/12c7bc13a755785f36c9c3e293037d7d to your computer and use it in GitHub Desktop.
Terraform helper script for workspace-per-environment model
#!/bin/bash
cmd=$1
shift
env=$1
shift
terraform workspace select $env || exit_error "failed to select workspace $env"
case $cmd in
# Custom command: plan -> outfile, confirm, apply outfile
planapply)
pfile=/tmp/${env}_$(date +%s).tfplan
terraform plan -var-file=$env.tfvars -out="$pfile" $@
echo -n "OK? [y/N] "
read choice
if [[ "$choice" == "y" ]]; then
terraform apply "$pfile"
else
echo "Aborted."
fi
rm "$pfile"
;;
# Only plan/apply/console/destroy/import/refresh/validate use -var-file, others will error if its passed
plan|apply|console|destroy|import|refresh|validate) terraform $cmd -var-file=$env.tfvars $@;;
*) terraform $cmd $@;;
esac
terraform workspace select default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment