Skip to content

Instantly share code, notes, and snippets.

@talentdeficit
Last active May 28, 2020 05:01
Show Gist options
  • Save talentdeficit/82bac9f8c998f3e7004494e94200e0bc to your computer and use it in GitHub Desktop.
Save talentdeficit/82bac9f8c998f3e7004494e94200e0bc to your computer and use it in GitHub Desktop.
look upon my works and despair
# find all files in vars/
vars = $(wildcard vars/*.tfvars)
# extract env name from vars
envs := $(vars:vars/%.tfvars=%)
# calculate datadir from env name
datadirs := $(envs:%=.terraform/%)
# calculate plan filename from env name
plans := $(envs:%=.terraform/%.plan)
# tf files in module, for recalculating plan on module changes
module := $(wildcard *.tf)
# generate a plan file in .terraform
plan: init $(plans)
# apply plans in .terraform
apply: plan $(envs)
# initialize terraform, setting up backend and foreign modules
init: $(datadirs)
# remove plans
clean:
@rm .terraform/*.plan
# remove plans and initialization data
clean-all:
@rm -rf .terraform
.PHONY: init plan apply clean clean-all
# generate a plan file, depends on env vars and module files
.terraform/%.plan: vars/%.tfvars $(module)
@TF_DATA_DIR=.terraform/$* terraform plan -input=false -var-file=vars/$*.tfvars -out=$@
# initialize terraform in data dir per env
.terraform/%: backends/%.tfvars
@TF_DATA_DIR=$@ terraform init -backend-config=$<
# apply plans
%: .terraform/%.plan
@TF_DATA_DIR=.terraform/$* terraform apply .terraform/$*.plan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment