Skip to content

Instantly share code, notes, and snippets.

@mmingorance-dh
Created October 31, 2019 16:56
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 mmingorance-dh/be1702d791c9ef91d6aca074c70fadcb to your computer and use it in GitHub Desktop.
Save mmingorance-dh/be1702d791c9ef91d6aca074c70fadcb to your computer and use it in GitHub Desktop.
Makefile to run Terraform
# ------------------------------------------------------------------------------
# Runs Terraform inside a Docker container.
#
# The primary purpose it to ensure everyone uses the same Terraform version.
# A typical workflow would be this:
# $ make clean
# $ make init
# $ make plan
# $ make apply
# ------------------------------------------------------------------------------
REPOS_DIR := $(shell dirname $(shell dirname $(shell dirname $(shell dirname $(shell pwd)))))
PROJECT_NAME:= $(shell basename $(shell dirname $(shell dirname $(shell pwd))))
PROJECT_DIR:= $(shell dirname $(shell dirname $(shell pwd)))
RESOURCE_NAME:= $(shell basename $(shell dirname $(shell dirname $(shell pwd))))
ENV:= $(shell basename $(shell pwd))
ENV_SHORT:= $(shell basename $(shell pwd) | cut -d '-' -f 1)
# Docker image to use to run terraform, bootstrap and shutdown scripts
TERRAFORM_VERSION="0.11.11"
TERRAFORM_BINARY=docker run --rm -ti \
-e TF_LOG \
-v $(PROJECT_DIR):/go/ \
--workdir /go/envs/$(ENV)/ hashicorp/terraform:${TERRAFORM_VERSION}
TERRAFORM_RM=docker run --rm -ti \
-e TF_LOG \
-v $(PROJECT_DIR):/go/ \
--entrypoint="/bin/rm" \
--workdir /go/envs/$(ENV)/ hashicorp/terraform:${TERRAFORM_VERSION}
test:
@echo ${PROJECT_NAME}
@echo ${PROJECT_DIR}
@echo ${RESOURCE_NAME}
@echo ${ENV}
@echo $(TERRAFORM_BINARY)
@echo $(ENV_SHORT)
@echo key=$(PROJECT_NAME)/$(ENV)/$(PROJECT_NAME)-$(ENV).tfstate
@echo $(shell ls)
help:
@echo "Available commands:"
@echo "apply ; destroy ; init ; output ; plan ; refresh ; show ; output ; validate ; remove-plan ; clean"
@echo
@echo "A typical workflow would be this:"
@echo "make init"
@echo "make plan"
@echo "make apply"
# Remove the Terraform plan (if it exists).
remove-plan:
$(TERRAFORM_RM) -fv terraform.tfplan
# Remove the <.terraform/> folder and plan, should they exist.
clean: remove-plan
$(TERRAFORM_RM) -rfv .terraform/*
# Initialise Terraform (it is idempotent - run at any time).
init: clean
$(TERRAFORM_BINARY) init \
-backend-config="bucket=<BUCKET_NAME_HERE>" \
-backend-config="key=<KEY_STATE_FILE>" \
-backend-config=terraform.remote
# Create a plan and save it.
plan: remove-plan
$(TERRAFORM_BINARY) validate
$(TERRAFORM_BINARY) plan -out terraform.tfplan -parallelism=100
# Apply the most recent plan created during the "plan" stage (see above).
apply:
$(TERRAFORM_BINARY) apply terraform.tfplan
rm -rf terraform.tfplan
output:
$(TERRAFORM_BINARY) output
refresh:
$(TERRAFORM_BINARY) refresh
validate:
$(TERRAFORM_BINARY) validate
show:
$(TERRAFORM_BINARY) show
destroy:
$(TERRAFORM_BINARY) destroy
.PHONY: apply destroy init output plan refresh show output validate remove-plan clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment