Last active
August 10, 2017 18:10
-
-
Save chriswgerber/fc6b7cfaa4fd079a1b2fbcb0d5237cdb to your computer and use it in GitHub Desktop.
Terraform Makefile
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
.ONESHELL: | |
SHELL = /bin/bash | |
.SHELLFLAGS = -e | |
CLEAN_TARGETS = | |
TF = terraform | |
TF_DIR = .terraform | |
STATE_FILE = ${TF_DIR}/terraform.tfstate | |
STATE_PLAN = ${TF_DIR}/terraform.tfplan | |
STATE_FILE_BAK = ${TF_DIR}/terraform.tfbackup | |
SOURCEDIR := ./ | |
SRCS := $(shell find $(SOURCEDIR) -name '*.tf') | |
SRCS += $(shell find $(SOURCEDIR) -name '*.tfvars') | |
SRCS += $(shell find $(SOURCEDIR) -name '*.json') | |
TF_MODULE_DIR = $(TF_DIR)/modules | |
CLEAN_TARGETS += $(TF_DIR)/modules/ $(STATE_PLAN) $(STATE_FILE_BAK) | |
define terraform | |
$(strip $(TF) $(1) $(TF_FLAGS) $(2)) | |
endef | |
$(STATE_PLAN): TF_FLAGS = -out=$(STATE_PLAN) | |
$(STATE_PLAN): $(SRCS) | |
@$(MAKE) lint | |
$(call terraform,plan) | |
$(STATE_FILE) : TF_FLAGS = -backend=true \ | |
-get=true \ | |
-force-copy \ | |
-input=false | |
$(STATE_FILE) : $(SRCS) | $(TF_DIR) | |
$(call terraform,init) | |
$(SRCS) : ; | |
$(TF_DIR) : | |
mkdir -p $@ | |
$(TF_MODULE_DIR): | |
$(call terraform,get) | |
plan: | |
rm -f $(STATE_PLAN) | |
$(MAKE) $(STATE_PLAN) | |
apply: TF_FLAGS = -backup=$(STATE_FILE_BAK) | |
apply: | |
$(call terraform,$@,$(STATE_PLAN)) | |
rm $(STATE_PLAN) | |
refresh: | |
$(call terraform,$@) | |
lint: TF_FLAGS = | |
lint: | |
$(call terraform,fmt) | |
$(call terraform,validate) | |
clean: $(CLEAN_TARGETS) | |
rm -rf $^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment