Skip to content

Instantly share code, notes, and snippets.

@seanorama
Last active March 2, 2021 17:21
Show Gist options
  • Save seanorama/c937e441be0d52651617141466013433 to your computer and use it in GitHub Desktop.
Save seanorama/c937e441be0d52651617141466013433 to your computer and use it in GitHub Desktop.
(notes from 2017) terraform makefile for running same codebase across separate envs
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
init: ## Initializes the terraform remote state backend and pulls the correct environments state.
@if [ -z ${env} ]; then echo "env was not set" ; exit 10 ; fi
@rm -rf .terraform/*.tf*
@terraform init \
-backend-config=environments/${env}/backend.conf
format: ## Fix terraform file content format
@terraform fmt
update: ## Gets any module updates
@terraform get -update=true &>/dev/null
validate: init update ## Validates a plan.
@terraform validate -input=false -var-file=environments/${env}/inputs.tfvars
plan: init update ## Runs a plan.
@terraform plan -input=false -refresh=true -module-depth=-1 -var-file=environments/${env}/inputs.tfvars
plan-destroy: init update ## Shows what a destroy would do.
@terraform plan -input=false -refresh=true -module-depth=-1 -destroy -var-file=environments/${env}/inputs.tfvars
show: init ## Shows a module
@terraform show -module-depth=-1
graph: ## Runs the terraform grapher
@rm -f graph.png
@terraform graph -draw-cycles -module-depth=-1 | dot -Tpng > graph.png
@open graph.png
apply: init update ## Applies a new state.
@terraform apply -input=true -refresh=true -var-file=environments/${env}/inputs.tfvars
output: update ## Show outputs of a module or the entire state.
@if [ -z $(MODULE) ]; then terraform output ; else terraform output -module=$(MODULE) ; fi
destroy: init update ## Destroys targets
@terraform destroy -var-file=environments/${env}/inputs.tfvars -force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment