Created
November 28, 2017 19:40
-
-
Save aprice/12c7bc13a755785f36c9c3e293037d7d to your computer and use it in GitHub Desktop.
Terraform helper script for workspace-per-environment model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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