Skip to content

Commit 2752282

Browse files
committed
fix: improve extensibility of the Makefile for re-use
1 parent 974a62e commit 2752282

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Makefile.terraform

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
## Override any of the below ?= variables in .config.mk
66
-include .config.mk
7+
## If you have any local to your repository modifications or extensions
8+
## for this makefile load them into local.mk
9+
## A good usecase would be creating another .check-env:: that looks for
10+
## specific TF_VAR environment variables
11+
-include local.mk
712

8-
AWS_CONFIG ?= ${HOME}/.aws
13+
USER_AWS_CONFIG ?= ${HOME}/.aws
914
TERRAFORM_DIR ?= .terraform
1015
TERRAFORM_IMAGE ?= docker.io/hashicorp/terraform
1116
TERRAFORM_VERSION ?= 1.0.7 ## Terraform is very version specific, so know what you need
@@ -15,22 +20,25 @@ CONTAINER_ENV ?= .env
1520
CONTAINER_WORK_DIR ?= /data
1621

1722
# Helper switches for the BASE_COMMAND
18-
ifeq ("$(AWS_CONFIG)", "$(wildcard $(AWS_CONFIG))")
19-
BASE_ENV := -v $(AWS_CONFIG):/root/.aws:Z --env-file=$(CONTAINER_ENV)
23+
ifeq ("$(USER_AWS_CONFIG)", "$(wildcard $(USER_AWS_CONFIG))")
24+
BASE_ENV := -v $(USER_AWS_CONFIG):/.aws:Z
2025
endif
2126
BASE_ENV := $(BASE_ENV) --env-file=$(CONTAINER_ENV)
2227
BASE_USER := -u $(shell id -u ${USER}):$(shell id -g ${USER})
2328
BASE_WORKDIR := -w $(CONTAINER_WORK_DIR) -v "$(CURDIR)":$(CONTAINER_WORK_DIR):Z
2429

2530
# Container based commands to for use handling target steps
26-
BASE_COMMAND := $(CONTAINER_ENGINE) run --rm -it $(BASE_ENV) $(BASE_WORKDIR)
31+
BASE_COMMAND := $(CONTAINER_ENGINE) run --rm -it $(BASE_USER) $(BASE_ENV) $(BASE_WORKDIR)
2732
TERRAFORM_COMMAND := $(BASE_COMMAND) $(TERRAFORM_IMAGE):$(TERRAFORM_VERSION)
2833

2934
# Determine some runtime values
3035
TERRAFORM_ENVIRONMENT := $(TERRAFORM_DIR)/environment
3136
ifeq ("$(TERRAFORM_ENVIRONMENT)", "$(wildcard $(TERRAFORM_ENVIRONMENT))")
32-
# if file exists on disk, get the workspace from it
33-
TERRAFORM_VAR_FILE := -var-file=$(shell cat $(TERRAFORM_ENVIRONMENT)).tfvars
37+
# If file exists on disk, get the workspace from it.
38+
# Depending on your file and directory structure you may need to do some adjustments
39+
# such as this sed that takes a region.tier workspace name and changes it to region/tier
40+
# for the directory structure.
41+
TERRAFORM_VAR_FILE := -var-file=$(shell cat $(TERRAFORM_ENVIRONMENT) | sed 's!\.!/!').tfvars
3442
endif
3543

3644
all: help
@@ -46,7 +54,7 @@ debug-%: ## Debug a variable by calling `make debug-VARIABLE`
4654

4755
.PHONY: .check-env
4856
.SILENT: .check-env
49-
.check-env:
57+
.check-env:: # This is ::'d so that you can add a custom version to local.mk for repo specific checks
5058
ifeq ($(origin TERRAFORM_STATE_S3), "yes")
5159
if [ "${AWS_PROFILE}" == "" ]; then \
5260
if [ "${AWS_SECRET_ACCESS_KEY}" == "" ] || [ "${AWS_ACCESS_KEY_ID}" == "" ]; then \
@@ -64,11 +72,11 @@ help: ## Show this help, includes list of all actions.
6472
.PHONY: clean
6573
clean: ## Cleanup the local checkout
6674
-rm -f *.zip *.backup
67-
-rf -rf $(TERRAFORM_DIR)
75+
-rm -rf $(TERRAFORM_DIR)
6876

6977
.SILENT: $(CONTAINER_ENV)
7078
$(CONTAINER_ENV):
71-
env | awk '!/TOKEN/ && /^(AWS)/ { print }' | sort > $(@)
79+
env | awk '!/TOKEN/ && /^(AWS|TF_VAR)/ { print }' | sort > $(@)
7280

7381
.SILENT: $(TERRAFORM_DIR)
7482
$(TERRAFORM_DIR): $(CONTAINER_ENV)

0 commit comments

Comments
 (0)