|
| 1 | +SHELL=/bin/bash -o pipefail |
| 2 | +PROHOME := $(realpath ../../..) |
| 3 | + |
| 4 | +REGION := $(notdir $(CURDIR)) |
| 5 | +ACCOUNT := $(notdir $(patsubst %/,%,$(dir $(CURDIR)))) |
| 6 | + |
| 7 | +ifneq ($(GITHUB_ACTIONS),true) |
| 8 | +export AWS_PROFILE := $(ACCOUNT) |
| 9 | +endif |
| 10 | + |
| 11 | +.PHONY: all |
| 12 | +all: |
| 13 | + echo "Please specify a target" |
| 14 | + exit 1 |
| 15 | + |
| 16 | +.terraform/modules/modules.json: backend.tf |
| 17 | + tofu init |
| 18 | + |
| 19 | +.PHONY: init |
| 20 | +init: .terraform/modules/modules.json |
| 21 | + |
| 22 | +.PHONY: clean |
| 23 | +clean: |
| 24 | + $(RM) -rf .terraform |
| 25 | + $(RM) -rf inventory |
| 26 | + $(RM) .terraform.lock.hcl |
| 27 | + $(RM) backend-state.tf |
| 28 | + $(RM) backend.plan |
| 29 | + $(RM) backend.tf |
| 30 | + $(RM) dyn_locals.tf |
| 31 | + $(RM) terraform.tfstate |
| 32 | + cd $(PROHOME)/ && make clean |
| 33 | + |
| 34 | +.PHONY: backend-state |
| 35 | +backend-state: backend.tf |
| 36 | + |
| 37 | +dyn_locals.tf: |
| 38 | + echo -e "locals {\n # tflint-ignore: terraform_unused_declarations\n aws_region = \"$(REGION)\"\n # tflint-ignore: terraform_unused_declarations\n aws_account_id = \"$(ACCOUNT)\"\n}\n" >dyn_locals.tf |
| 39 | + |
| 40 | +backend.tf: backend-state.tf |
| 41 | + sed "s/#AWS_REGION/$(REGION)/g" <$(PROHOME)/modules/backend-file/backend.tf >backend.tf |
| 42 | + $(RM) terraform.tfstate |
| 43 | + |
| 44 | +backend-state.tf: dyn_locals.tf |
| 45 | + sed "s/#AWS_REGION/$(REGION)/g" <$(PROHOME)/modules/backend-file/backend-state.tf >backend-state.tf |
| 46 | + tofu get -update |
| 47 | + tofu init -backend=false |
| 48 | + tofu plan -input=false -out=backend.plan -detailed-exitcode -target=module.backend-state -lock-timeout=15m ; \ |
| 49 | + ext_code=$$? ; \ |
| 50 | + if [ $$ext_code -eq 2 ] ; then \ |
| 51 | + if [ "$$GITHUB_ACTIONS" != "true" ] ; then \ |
| 52 | + echo "Backend state does not exist, creating" ; \ |
| 53 | + tofu apply ${TERRAFORM_EXTRAS} backend.plan ; \ |
| 54 | + else \ |
| 55 | + echo "Backend state does not exist, should not do it on a github action!" ; \ |
| 56 | + exit 1 ; \ |
| 57 | + fi ; \ |
| 58 | + elif [ $$ext_code -eq 0 ] ; then \ |
| 59 | + echo "Backend state already exists" ; \ |
| 60 | + else \ |
| 61 | + echo "Unexpected exit code $$ext_code" ; \ |
| 62 | + exit 1 ; \ |
| 63 | + fi |
| 64 | + |
| 65 | +.PHONY: plan |
| 66 | +plan: .terraform/modules/modules.json |
| 67 | + tofu plan $(TERRAFORM_EXTRAS) |
| 68 | + |
| 69 | +.PHONY: apply |
| 70 | +apply: .terraform/modules/modules.json |
| 71 | + tofu apply ${TERRAFORM_EXTRAS} |
| 72 | + |
| 73 | +.PHONY: destroy |
| 74 | +destroy: .terraform/modules/modules.json |
| 75 | + echo "To make sure you want to run this, go to the Makefile and comment out the destroy target" ; exit 1 |
| 76 | + # terraform destroy ${TERRAFORM_EXTRAS} |
| 77 | + |
| 78 | +.PHONY: tflint |
| 79 | +tflint: .terraform/modules/modules.json |
| 80 | + tflint --init |
| 81 | + tflint --call-module-type=all --color --minimum-failure-severity=warning --recursive |
0 commit comments