Skip to content

Instantly share code, notes, and snippets.

@ZeroDeth
Forked from davidlu1001/terraform_include_exclude
Created September 8, 2020 17:01
Show Gist options
  • Select an option

  • Save ZeroDeth/d3e48c4bcd509c82c7ad80e285c45dad to your computer and use it in GitHub Desktop.

Select an option

Save ZeroDeth/d3e48c4bcd509c82c7ad80e285c45dad to your computer and use it in GitHub Desktop.
Makefile for Terraform to support include/exclude
PLAN_OPTIONS ?=
APPLY_OPTIONS ?=
EXCLUDE ?=
INCLUDE ?=
define PLAN_OPTIONS_EXCLUDE
$(shell terraform show current.plan | perl -pe 's//\n/ if $$. == 1' | perl -pe 's/\x1b\[[0-9;]*[mG]//g' | perl -ne 'if ($$p) { print unless /^$$/; $$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 current.plan | perl -pe 's//\n/ if $$. == 1' | perl -pe 's/\x1b\[[0-9;]*[mG]//g' | perl -ne 'if ($$p) { print unless /^$$/; $$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
plan_exclude:
terraform plan -out current.plan $(call PLAN_OPTIONS_EXCLUDE,$(EXCLUDE))
plan_include:
terraform plan -out current.plan $(call PLAN_OPTIONS_INCLUDE,$(INCLUDE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment