Skip to content

Instantly share code, notes, and snippets.

@jpluscplusm
Last active September 17, 2016 00:03
Show Gist options
  • Save jpluscplusm/7b345e3853dcefaefc82fb2d9c734422 to your computer and use it in GitHub Desktop.
Save jpluscplusm/7b345e3853dcefaefc82fb2d9c734422 to your computer and use it in GitHub Desktop.
Terraform Makefile wrapper with remote state mgmt/bootstrap
/.terraform/terraform.tfstate*
/plan.out
/.plan.out.*
.PHONY: infra plan apply tf-apply clean mrproper help
.DEFAULT_GOAL := help
AWS_PROFILE ?= Default
NOW := $(shell date +"%Y%m%d-%H%M%S")
RESOURCES := resources
STATE := .terraform/terraform.tfstate
TF_FILES := $(wildcard $(RESOURCES)/*.tf $(STATE))
PLAN := plan.out
export AWS_PROFILE
infra: plan apply ## Do whatever's needed to bring the infra up to date
plan: $(PLAN) ## Plan, display and store that which would be needed to bring the infra up to date
$(PLAN): $(STATE) $(TF_FILES)
terraform plan -out $(PLAN) $(RESOURCES)/
apply: $(STATE) tf-apply clean ## Apply the current plan of operations to the infra, without replanning
tf-apply:
terraform apply $(PLAN)
show: ## Print what the current plan of operations would do, without replanning
terraform show $(PLAN)
clean: ## Archive the last plan
mv $(PLAN) .$(PLAN).$(NOW)
mrproper: ## Remove all non-version-controlled files
rm -f $(STATE) $(PLAN) .$(PLAN).*
.terraform/terraform.tfstate:
terraform remote config \
-backend=S3 \
-backend-config="bucket=my-tfstate-bucket" \
-backend-config="key=terraform/live/terraform.tfstate" \
-backend-config="region=eu-west-1" \
-backend-config="encrypt=true" \
-backend-config="acl=private"
help: ## Display this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
# this directory holds the .tf resource definitions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment