Skip to content

Instantly share code, notes, and snippets.

@costa86
Last active May 29, 2023 12:21
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 costa86/f65897cde17b448cd94eb4254f58caaa to your computer and use it in GitHub Desktop.
Save costa86/f65897cde17b448cd94eb4254f58caaa to your computer and use it in GitHub Desktop.
Makefile for terraform to handle workspaces
valid_envs := dev uat bench prod
env := $(word 1,$(valid_envs))
cmd_plan := terraform plan -var-file=$(env).tfvars
cmd_apply := terraform apply -auto-approve -var-file=$(env).tfvars
cmd_destroy := terraform destroy -auto-approve -var-file=$(env).tfvars
ifeq (,$(filter $(env),$(valid_envs)))
$(error Invalid env specified. Available envs: $(valid_envs))
endif
help:
@echo "Usage:\nmake <command> env=<env>"
@echo "[INFO] Each env is mapped to a terraform workspace"
@echo "[INFO] Current env: $(env)"
@echo "[INFO] Available envs: $(valid_envs)"
@echo "Commands:"
@echo "plan env=$(env): $(cmd_plan)"
@echo "apply env=$(env): $(cmd_apply)"
@echo "destroy env=$(env): $(cmd_destroy)"
all: help
pre:
terraform fmt -recursive
terraform validate
change_workspace: pre
terraform workspace select $(env)
apply: change_workspace
$(cmd_apply)
destroy: change_workspace
$(cmd_destroy)
plan: change_workspace
$(cmd_plan)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment