-
Notifications
You must be signed in to change notification settings - Fork 11
/
Makefile
55 lines (48 loc) · 1.83 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
ENVS := aws-dev aws-test aws-prod
env := aws-dev
PREFIX := $(shell echo $(env) |shasum -a 256|cut -c 1-12)
KEY := $(shell pwd | xargs basename)
TF_ENV_VARS :=
AWS_ENV :=
S3_PATH :=
.PHONY: help
help:
@echo "make (plan|apply|autoapply) [env=]"
@echo " e.g. make plan ENV=(`echo $(ENVS) | tr ' ' \|`) [default: $(env)]"
@echo " "
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
tf_init:
ifeq ($(filter $(env),$(ENVS)),)
$(error $(env) is not supported)
endif
ifdef CI
$(eval S3_PATH = services/$(CI_PROJECT_NAME))
else
$(if $(findstring deploy,$(KEY)), $(eval KEY := services/$(shell cd .. && pwd | xargs basename)))
$(eval AWS_ENV = $(env))
$(eval S3_PATH = $(KEY))
endif
@echo "# PREFIX: $(PREFIX)"
@echo "# AWS_ENV: $(AWS_ENV)"
@if [ -e .terraform/terraform.tfstate ]; then rm .terraform/terraform.tfstate; fi;
$(eval TF_ENV_VARS := $(TF_ENV_VARS) AWS_PROFILE=$(AWS_ENV) )
@echo "# TF_ENV_VARS: $(TF_ENV_VARS)"
$(TF_ENV_VARS) terraform init -backend-config="bucket=$(PREFIX)-state" -backend-config="dynamodb_table=$(PREFIX)-state-lock" -backend-config="key=$(S3_PATH)/terraform.tfstate"
tf_vars:
$(eval TF_OPTIONS := $(TF_OPTIONS) -var aws-account=$(AWS_ENV))
$(eval TF_OPTIONS := $(TF_OPTIONS) -var tf-statefile-prefix=$(PREFIX))
$(if $(wildcard .env.$(env)), $(eval TF_OPTIONS := $(TF_OPTIONS) -var-file ".env.$(env)"))
$(if $(wildcard .env), $(eval TF_OPTIONS := $(TF_OPTIONS) -var-file ".env"))
@echo "# TF_OPTIONS= "$(TF_OPTIONS)
.PHONY: plan
plan: tf_init tf_vars
$(TF_ENV_VARS) terraform plan $(TF_OPTIONS)
.PHONY: apply
apply: tf_init tf_vars
$(TF_ENV_VARS) terraform apply $(TF_OPTIONS)
.PHONY: destroy
destroy: tf_init tf_vars
$(TF_ENV_VARS) terraform destroy $(TF_OPTIONS)
.PHONY: autoapply
autoapply: tf_init tf_vars
$(TF_ENV_VARS) terraform apply -auto-approve $(TF_OPTIONS)