Skip to content

Instantly share code, notes, and snippets.

@distractdiverge
Last active March 12, 2020 20:22
Show Gist options
  • Save distractdiverge/5e588f3e6915bb6be3d354a0d16225ed to your computer and use it in GitHub Desktop.
Save distractdiverge/5e588f3e6915bb6be3d354a0d16225ed to your computer and use it in GitHub Desktop.
Terraform Makefile
########################################################################
# Variables #
########################################################################
PARAMS= # Dummy variables to pass commands to TF
PROJECT_DIR=$(shell pwd)
KUBE_CLUSTER_NAME=terraform-eks-demo
TERRAFORM_DIR=$(PROJECT_DIR)
BACKEND_CONF_PATH = $(TERRAFORM_DIR)/config/aws-personal-eks.conf
BOOTSTRAP_STACK_NAME = eks-knative
BOOTSTRAP_BUCKET_NAME = $(BOOTSTRAP_STACK_NAME)-state
BOOTSTRAP_DB_NAME = $(BOOTSTRAP_STACK_NAME)-lock
BOOTSTRAP_STACK_FILE = $(PROJECT_DIR)/bootstrap-cloudformation.yml
AWS_ACCOUNT_ID = 117243655954
########################################################################
# Kubernetes Config #
########################################################################
.PHONY: kubeconfig
kubeconfig: ## Update Local kubectl with new EKS cluster
@echo "Updating Kubectl with $(KUBE_CLUSTER_NAME) config from EKS"
@aws eks update-kubeconfig --name $(KUBE_CLUSTER_NAME)
@echo "DONE\n"
########################################################################
# Terrform Config Code #
########################################################################
.PHONY: format
format: ## Autoformat the Terraform files
@echo "Formatting Terraform Files"
@terraform fmt -recursive $(TERRAFORM_DIR)
@git add $(TERRAFORM_DIR)
@echo "DONE\n"
.PHONY: validate
validate: lint ## Validate the Terraform files
@echo "Validating Terraform Files"
@terraform validate $(TERRAFORM_DIR)
@echo "DONE\n"
.PHONY: lint
lint: format ## Lint the Terraform files (via tflint)
@echo "Linting Terraform Files"
@tflint -c $(TERRAFORM_DIR)/.tflint.hcl $(TERRAFORM_DIR)
@echo "DONE\n"
.PHONY: deploy
deploy: lint ## Provision the Terraform infrastructure
@echo "Deploying Terraform to AWS"
@cd $(TERRAFORM_DIR); terraform apply $(TERRAFORM_DIR) $(PARAMS); cd $(PROJECT_DIR)
@echo "DONE\n"
.PHONY: destroy
destroy: lint ## Destroy the Terraform provisioned infrastructure
@echo "Tearing Down Terraform in AWS"
@cd $(TERRAFORM_DIR); terraform destroy -auto-approve $(TERRAFORM_DIR); cd $(PROJECT_DIR)
@echo "DONE\n"
.PHONY: init
init: ## Initalize the Terraform Stack
@echo "Initalizing Terraform"
@cd $(TERRAFORM_DIR); terraform init -backend-config=$(BACKEND_CONF_PATH); cd $(PROJECT_DIR)
@echo "DONE\n"
.PHONY: bootstrap
bootstrap: ## Create infrastructure for Terraform remote state in AWS
@echo "Deploying Terraform Bootstrap CloudFormation Stack"
@aws cloudformation deploy \
--template-file ${BOOTSTRAP_STACK_FILE} \
--stack-name ${BOOTSTRAP_STACK_NAME} \
--parameter-overrides AccountId=${AWS_ACCOUNT_ID} \
BucketName=$(BOOTSTRAP_BUCKET_NAME) \
DynamoDbTableName=$(BOOTSTRAP_DB_NAME) \
--capabilities CAPABILITY_IAM
@aws cloudformation describe-stacks \
--stack-name ${BOOTSTRAP_STACK_NAME} | \
jq '.Stacks[0].Outputs[] | { (.OutputKey): .OutputValue }' | \
jq -n 'reduce inputs as $$in (null; . + $$in)'
@echo "DONE\n"
########################################################################
# Common #
########################################################################
.PHONY: prereq
prereq: ## Install Required Tools via Brew
@echo "Installing required dev tools"
@brew install terraform \
tflint \
kubectl \
jq
@echo "DONE\n"
# Colors -- https://www.linuxjournal.com/article/8603
BLACK = "\\033[30m"
RED = "\\033[31m"
GREEN = "\\033[32m"
YELLOW = "\\033[33m"
BLUE = "\\033[34m"
MAGENTA = "\\033[35m"
CYAN = "\\033[36m"
WHITE = "\\033[37m"
END = "\\033[0m"
help: Makefile ## Print this help
@echo ""
@printf "%-5s$(GREEN)%s-%s$(END) %s\n" "make" "PARAMS=" "\"-extra -opts\"" "<target>"
@echo ""
@printf "$(CYAN)%-10s: $(END) %s\n" "<target>" "<description>"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort |awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-10s: \033[0m %s\n", $$1, $$2}'
@echo ""
@echo ""
@echo "The above specificiation of '$(GREEN)PARAMS=<value>$(END)' will pass"
@echo "will pass additional params to the terraform tasks within a $(CYAN)<target>$(END)."
@echo ""
.DEFAULT_GOAL := help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment