-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
143 lines (122 loc) · 4.09 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
SHELL = /bin/bash -e
DIRECTORY = "./"
MODULES = `find modules -maxdepth 1 -mindepth 1 | tr '\n' ' '`
define run_command
$(info >>> Exec ($(1)))
$(1)
endef
.PHONY: help
help: ## This help output
help:
@echo ""
@echo "Usage: make <target> [parameters]"
@echo ""
@echo "Targets:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-38s\033[0m %s\n", $$1, $$2}'
@echo ""
@echo "Parameters:"
@echo -e " \033[36mDIRECTORY=[environment-root-folder]\033[0m Select the root folder to use for terragrunt execution"
@echo ""
@echo "Examples:"
@echo -e " \033[36mmake format\033[0m Format all code"
@echo -e " \033[36mmake lint\033[0m Lint all code"
@echo -e " \033[36mmake plan-all DIRECTORY=./aws\033[0m Apply all modules in the \`dev/\` folder"
@echo -e " \033[36mmake plan DIRECTORY=./aws/modules/debug\033[0m Apply specific module"
@echo -e " \033[36mmake apply-all DIRECTORY=./aws/modules/debug\033[0m Apply all modules in the \`dev/australiaeast/\` folder"
@echo ""
.PHONY: debug
debug:
for module in $(MODULES); do \
echo $$module && make tflint-module MODULE=$$module; \
done
.PHONY: format
format: ## Format Terraform code
format: terraform-format
.PHONY: terraform-format
terraform-format:
$(info --- terraform-format)
@$(call run_command,terraform fmt -recursive -diff)
.PHONY: lint
lint: ## Lint all Terraform code
lint: terraform-lint tflint yamllint
.PHONY: terraform-lint
terraform-lint:
$(info --- terraform-lint)
@$(call run_command,terraform fmt -check -recursive -diff)
.PHONY: yamllint
yamllint:
$(info --- yamllint)
@$(call run_command,yamllint -f auto .)
.PHONY: tflint
tflint:
$(info --- tflint)
@tflint --init
@for module in $(MODULES); do \
$(MAKE) --no-print-directory tflint-module MODULE=$$module; \
done
.PHONY: tflint-module
tflint-module:
tflint-module: module-vars
$(info --- tflint-module ($(MODULE)))
@$(call run_command,tflint $(MODULE))
.PHONY: validate
validate: ## Validate all terraform modules
$(info --- validate)
@for module in $(MODULES); do \
echo Running terraform validate on $$module; \
cd $$module; \
terraform validate; \
cd ../../; \
done
.PHONY: tfsec
tfsec: ## TFSec all terraform modules
$(info --- tfsec)
@for module in $(MODULES); do \
echo Running tfsec on $$module; \
cd $$module; \
tfsec ./; \
cd ../../; \
done
.PHONY: clean
clean: ## Remove the .terragrunt-cache directories
$(info --- clean)
@$(call run_command,find . -type d -name ".terragrunt-cache" -prune -exec rm -rf {} \;)
@$(call run_command,find . -type f -name ".terraform.lock.hcl" -prune -exec rm -rf {} \;)
@$(call run_command,rm -fr /opt/terragrunt-cache/*)
.PHONY: docs
docs: ## Generate teraform-docs for all modules
$(info --- docs)
@for module in $(MODULES); do \
$(MAKE) --no-print-directory docs-module MODULE=$$module; \
done
.PHONY: docs-module
docs-module: module-vars
$(info --- docs-module ($(MODULE)))
@$(call run_command,terraform-docs markdown document --hide requirements --escape=false --sort-by required "$(MODULE)" > "$(MODULE)/README.md")
.PHONY: init-all
init-all: ## Run `terraform init` for specifc [DIRECTORY]
$(info --- init-all ($(DIRECTORY)))
@for module in $(MODULES); do \
cd $$module; \
terraform init -upgrade; \
cd ../../; \
done
.PHONY: init
init: ## Run `terraform init` for specifc [MODULE]
$(info --- init ($(MODULE)))
@$(call run_command,cd $(MODULE))
@$(call run_command,terraform init)
.PHONY: plan-all
plan-all: ## Run `terragrunt run-all plan` for specifc [DIRECTORY]
$(info --- plan-all ($(DIRECTORY)))
@$(call run_command,terragrunt run-all plan --terragrunt-non-interactive --terragrunt-include-external-dependencies --terragrunt-working-dir $(DIRECTORY))
.PHONY: plan
plan: ## Run `terragrunt plan` for specifc [DIRECTORY]
$(info --- plan ($(DIRECTORY)))
@$(call run_command,terragrunt plan --terragrunt-non-interactive --terragrunt-working-dir $(DIRECTORY))
# Required environment variables when targeting an environment
.PHONY: module-vars
module-vars:
ifndef MODULE
$(error MODULE is undefined)
endif