This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMakefile
145 lines (120 loc) · 5.24 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
144
145
# We assume you have an AWS profile with the the PROJECT-ENVIRONMENT i.e. cti-dev
# This is the same profile that should exist in the env-vars file
.ONESHELL:
.SHELL := /bin/bash
# #########################################
# Shell swag
# #########################################
GREEN = \033[1;32m
RESET = \033[0m
WHITE = \033[1;38;5;231m
# #########################################
# Environment variables defaults
# #########################################
ENVIRONMENT ?=
PROJECT_KEY ?=
# #########################################
# Make target aliases
# #########################################
MAKE_TF_INIT = $(MAKE) --no-print-directory internal-init
MAKE_TF_PLAN = $(MAKE) --no-print-directory internal-plan
MAKE_TF_APPLY = $(MAKE) --no-print-directory internal-apply
# #########################################
# Commands args
# #########################################
PROJECT_ENVIRONMENT_KEY = $(PROJECT_KEY)-$(ENVIRONMENT)
PROJECT_TFVAR_FILE = env-vars/$(PROJECT_KEY).tfvars
PROJECT_ENVIRONMENT_TFVAR_FILE = env-vars/$(PROJECT_ENVIRONMENT_KEY).tfvars
TERRAFORM_PLAN_FILE = terraform-$(PROJECT_ENVIRONMENT_KEY).tfplan
# Lets use profiles that use this convention - could have got from the project-env tf var file like the AWS region below
AWS_PROFILE = $(PROJECT_ENVIRONMENT_KEY)
# Determine from tf vars file(s) - will use the first match
AWS_REGION = $(shell grep '^aws_region' $(PROJECT_ENVIRONMENT_TFVAR_FILE) $(PROJECT_TFVAR_FILE) | head -n 1 | cut -d '"' -f 2)
# NOTE: We cannot always derive these as we do not have a consistent scheme currently, see the cti-ENV-init targets
TERRAFORM_BACKEND_BUCKET ?= $(PROJECT_ENVIRONMENT_KEY)-terraform-store
TERRAFORM_BACKEND_KEY ?= $(PROJECT_KEY)
TERRAFORM_BACKEND_TABLE ?= $(PROJECT_ENVIRONMENT_KEY)-terraform-lock
# #########################################
# cti targets - HSE
# This is the one that is non conformant re backends
# #########################################
# DEV
.PHONY: cti-dev-init cti-dev-plan cti-dev-apply
cti-dev-init:
@$(MAKE_TF_INIT) ENVIRONMENT=dev PROJECT_KEY=cti \
TERRAFORM_BACKEND_BUCKET=fight-together-terraform-store-dev \
TERRAFORM_BACKEND_TABLE=fight-together-terraform-lock \
TERRAFORM_BACKEND_KEY=fight-together-dev-eu-west-1
cti-dev-plan:
@$(MAKE_TF_PLAN) ENVIRONMENT=dev PROJECT_KEY=cti
cti-dev-apply:
@$(MAKE_TF_APPLY) ENVIRONMENT=dev PROJECT_KEY=cti
# QA
.PHONY: cti-qa-init cti-qa-plan cti-qa-apply
cti-qa-init:
@$(MAKE_TF_INIT) ENVIRONMENT=qa PROJECT_KEY=cti \
TERRAFORM_BACKEND_BUCKET=fight-together-terraform-store-qa \
TERRAFORM_BACKEND_TABLE=fight-together-terraform-lock \
TERRAFORM_BACKEND_KEY=fight-together-qa-eu-west-1
cti-qa-plan:
@$(MAKE_TF_PLAN) ENVIRONMENT=qa PROJECT_KEY=cti
cti-qa-apply:
@$(MAKE_TF_APPLY) ENVIRONMENT=qa PROJECT_KEY=cti
# TRIAL
.PHONY: cti-trial-init cti-trial-plan cti-trial-apply
cti-trial-init:
@$(MAKE_TF_INIT) ENVIRONMENT=trial PROJECT_KEY=cti \
TERRAFORM_BACKEND_BUCKET=cti-terraform-store-trial \
TERRAFORM_BACKEND_TABLE=cti-terraform-lock-trial \
TERRAFORM_BACKEND_KEY=cti
cti-trial-plan:
@$(MAKE_TF_PLAN) ENVIRONMENT=trial PROJECT_KEY=cti
cti-trial-apply:
@$(MAKE_TF_APPLY) ENVIRONMENT=trial PROJECT_KEY=cti
# PROD
.PHONY: cti-prod-init cti-prod-plan cti-prod-apply
cti-prod-init:
@$(MAKE_TF_INIT) ENVIRONMENT=prod PROJECT_KEY=cti \
TERRAFORM_BACKEND_BUCKET=cti-terraform-store \
TERRAFORM_BACKEND_TABLE=cti-terraform-lock \
TERRAFORM_BACKEND_KEY=cti
cti-prod-plan:
@$(MAKE_TF_PLAN) ENVIRONMENT=prod PROJECT_KEY=cti
cti-prod-apply:
@$(MAKE_TF_APPLY) ENVIRONMENT=prod PROJECT_KEY=cti
# #########################################
# Generic targets - not dependant on a project
# #########################################
.PHONY: update validate format
update:
@echo "$(WHITE)==> Updating modules - $(GREEN)terraform get$(RESET)"
terraform get -update
validate:
@echo "$(WHITE)==> Validate terraform code - $(GREEN)terraform validate$(RESET)"
terraform validate
format:
@echo "$(WHITE)==> Format terraform code - $(GREEN)terraform fmt$(RESET)"
terraform fmt -recursive
# #########################################
# internal targets - don't expect these to be invoked directly
# #########################################
.PHONY: internal-init internal-plan internal-apply
internal-init:
@echo "$(WHITE)==> Setting up environment - $(GREEN)$(PROJECT_ENVIRONMENT_KEY)$(WHITE) in $(GREEN)$(AWS_REGION)$(RESET) using AWS profile $(GREEN)$(AWS_PROFILE)$(RESET), S3 bucket $(GREEN)$(TERRAFORM_BACKEND_BUCKET)$(RESET) and DynamoDB table $(GREEN)$(TERRAFORM_BACKEND_TABLE)$(RESET)"
terraform init -get=true \
-upgrade=true \
-input=false \
-lock=true \
-reconfigure \
-backend=true \
-backend-config="region=$(AWS_REGION)" \
-backend-config="dynamodb_table=$(TERRAFORM_BACKEND_TABLE)" \
-backend-config="bucket=$(TERRAFORM_BACKEND_BUCKET)" \
-backend-config="key=$(TERRAFORM_BACKEND_KEY)" \
-backend-config="profile=$(AWS_PROFILE)"
internal-plan:
@echo "$(WHITE)==> Planning changes - $(GREEN)$(PROJECT_ENVIRONMENT_KEY) - terraform plan$(RESET)"
terraform plan -out $(TERRAFORM_PLAN_FILE) -var-file $(PROJECT_TFVAR_FILE) -var-file $(PROJECT_ENVIRONMENT_TFVAR_FILE)
internal-apply:
@echo "$(WHITE)==> Applying changes - $(GREEN)$(PROJECT_ENVIRONMENT_KEY) - terraform apply$(RESET)"
terraform apply -parallelism=10 $(TERRAFORM_PLAN_FILE)