Skip to content

Instantly share code, notes, and snippets.

@trinitronx
Forked from davidlu1001/terraform_include_exclude
Last active November 25, 2019 22:13
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 trinitronx/522d301c66c105d78c5fcfffb30219f5 to your computer and use it in GitHub Desktop.
Save trinitronx/522d301c66c105d78c5fcfffb30219f5 to your computer and use it in GitHub Desktop.
Makefile for Terraform to support include/exclude
# Plan exclude / include targets
# To use: export INCLUDE or EXCLUDE with Extended Grep pattern for targets to match
PLAN_OPTIONS ?=
APPLY_OPTIONS ?=
EXCLUDE ?=
INCLUDE ?=
TERRAFORM ?= terraform
PLAN_FILE ?= current.plan
REPO_NAME ?= terraform
TF_DEPENDENCIES ?= .terraform $(wildcard *.tf) $(wildcard */*/*.tf) $(wildcard */*/*/*.tf) $(wildcard */*/*.tpl) $(wildcard .terraform/*)
export TF_DEPENDENCIES
TERRAFORM_STATE = "us-east-1:$(TF_VAR_account)-terraform-state-$(TF_VAR_env)/$(REPO_NAME)/terraform.tfstate"
$(shell bash -c '([[ -e .terraform/STATE ]] && diff <(echo "$(TERRAFORM_STATE)") .terraform/STATE) || rm -rf .terraform' >/dev/null 2>&1 ) # The state is missing, or invalid, so make `.terraform` is clean (will be recreated by targets which need it)
.terraform: | $(TERRAFORM)
${TERRAFORM} init -backend-config "bucket=$(TF_VAR_account)-terraform-state-$(TF_VAR_env)" \
-backend-config "region=us-east-1" \
-backend-config "key=$(REPO_NAME)/terraform.tfstate"
echo "$(TERRAFORM_STATE)" > .terraform/STATE
$(PLAN_FILE): $(TF_DEPENDENCIES) | .terraform
$(TERRAFORM) plan $(TERRAFORM_FLAGS) -out $(PLAN_FILE)
$(PLAN_FILE): $(TF_DEPENDENCIES) | .terraform
$(TERRAFORM) plan $(TERRAFORM_FLAGS) -out $(PLAN_FILE)
plan: $(PLAN_FILE) ## run terraform plan with given parameters
apply: $(PLAN_FILE) | .terraform ## Deploys - run terraform plan and apply with given parameters
$(TERRAFORM) apply $(TERRAFORM_FLAGS) $(PLAN_FILE)
define PLAN_OPTIONS_EXCLUDE
$(shell $(TERRAFORM) show $(CLUSIVITY_PLAN_FILE) | perl -pe 's//\n/ if $$. == 1' | perl -pe 's/\x1b\[[0-9;]*[mG]//g' | perl -ne 'if ($$p) { print unless /^$$/ || /^This plan does nothing.$$/; $$p = 0 } $$p++ if /^$$/'| awk '{print $$2}' | grep -E -v '$(1)' | sed -e 's/^/-target="/g' -e 's/$$/"/g' | awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}')
endef
define PLAN_OPTIONS_INCLUDE
$(shell $(TERRAFORM) show $(CLUSIVITY_PLAN_FILE) | perl -pe 's//\n/ if $$. == 1' | perl -pe 's/\x1b\[[0-9;]*[mG]//g' | perl -ne 'if ($$p) { print unless /^$$/ || /^This plan does nothing.$$/; $$p = 0 } $$p++ if /^$$/'| awk '{print $$2}' | grep -E '$(1)' | sed -e 's/^/-target="/g' -e 's/$$/"/g' | awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}')
endef
CLUSIVITY_PLAN_FILE := intermediate-$(PLAN_FILE)
plan_clusivity_prep: PLAN_FILE=$(CLUSIVITY_PLAN_FILE) ## Prepare intermediate terraform plan for clusivity pattern targets
plan_clusivity_prep: $(PLAN_FILE)
plan_exclude: plan_clusivity_prep ## run terraform plan excluding resources matching EXCLUDE pattern
@echo '$@: called with EXCLUDE="$(EXCLUDE)"'
@if [ -n "$(call PLAN_OPTIONS_EXCLUDE,$(EXCLUDE))" ]; then \
echo 'Running plan excluding targets: $(call PLAN_OPTIONS_EXCLUDE,$(EXCLUDE))'; \
$(TERRAFORM) plan -out $(PLAN_FILE) $(call PLAN_OPTIONS_EXCLUDE,$(EXCLUDE)) ; \
else \
echo -e 'Plan EXCLUDE pattern resulted in zero targets!\nNothing to be done for $@'; \
exit 0; \
fi
plan_include: plan_clusivity_prep ## run terraform plan including resources matching INCLUDE pattern
@echo '$@: called with INCLUDE="$(INCLUDE)"'
@if [ -n "$(call PLAN_OPTIONS_INCLUDE,$(INCLUDE))" ]; then \
echo 'Running plan including targets: $(call PLAN_OPTIONS_INCLUDE,$(INCLUDE))'; \
$(TERRAFORM) plan -out $(PLAN_FILE) $(call PLAN_OPTIONS_INCLUDE,$(INCLUDE)) ; \
else \
echo -e 'Plan INCLUDE pattern resulted in zero targets!\nNothing to be done for $@'; \
exit 0; \
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment