-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
167 lines (130 loc) · 5.67 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
########################
# Localstack Variables #
########################
# aws services that will be created in the localstack container
AWS_SERVICES := cloudtrail,cloudwatch,dynamodb,iam,logs,s3
# the default region to be used in localstack
REGION := us-west-2
# the s3 bucket name where terraform state will be stored
TF_BUCKET_NAME := tf-state-bucket
# the dynamodb table that will handle terraform state locking
TF_DYNAMO_NAME := terraform-backend-lock
####################
# System Variables #
####################
export AWS_ACCESS_KEY_ID="test"
export AWS_SECRET_ACCESS_KEY="test"
export AWS_DEFAULT_REGION=($REGION)
##################
# Path Variables #
##################
# locate the top-level directory of the project
project_dir := $(shell cd $(dir $(lastword $(MAKEFILE_LIST)))/.. && pwd)
# the current working directory where make was invoked from
CWD := $(shell basename $$PWD)
####################
# Docker Variables #
####################
# the name of the dockerfile
DOCKER_FILE_NAME ?= Dockerfile.test
# determine the location of the Dockerfile
DOCKER_FILE ?= $(shell while [[ "$$(pwd)" != "$(abspath $(project_dir)/..)" ]]; do \
test -f "$(DOCKER_FILE_NAME)" && echo "$$(pwd)/$(DOCKER_FILE_NAME)" && break; \
pushd $$(pwd)/.. >> /dev/null; \
done)
# set the docker image name
DOCKER_IMAGE ?= $(shell echo $(CWD) | tr A-Z a-z | tr -cd '[:alnum:]' )
#######################
# Terraform Variables #
#######################
# the backend state configuration file for development
DEV_BACKEND_CONFIG := $(abspath tfvars/development-backend)
# the terraform variable file for development
DEV_VARS_FILE := $(abspath tfvars/development.tfvars)
##################
# Docker Targets #
##################
# build the localstack container
.PHONY: build-container
build-container: internal-check-docker-file
@docker build \
-t $(DOCKER_IMAGE) \
-f $(DOCKER_FILE) .
# stop the localstack container
.PHONY: stop-container
stop-container:
@docker ps -a --filter=status=running | awk '$$1 != "CONTAINER" {print $$1}' | xargs -I {} docker stop {}
# remove all created localstack images
.PHONY: clean-docker
clean-docker:
@docker ps -a --filter=status=exited | awk '$$1 != "CONTAINER" {print $$1}' | xargs -I {} docker rm {}
@docker images | awk '$$1 == "<none>" || $$1 ~ /^$(DOCKER_IMAGE).*$$/ {print $$3}' | xargs -I {} docker rmi -f {}
# check that the dockerfile exists in the project
.PHONY: internal-check-docker-file
internal-check-docker-file:
ifeq ($(DOCKER_FILE),)
@echo "ERROR: No Dockerfile with name '$(DOCKER_FILE_NAME)' was found while recursively searching '$(project_dir)'"
@exit 1
endif
# run the localstack container
.PHONY: run-container
run-container:
@docker run \
-e LOCALSTACK_SERVICES="$(AWS_SERVICES)" \
-p 4566:4566 \
-p 4510-4559:4510-4559 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack" \
--rm -d $(DOCKER_IMAGE)
# internal-check calls internal-check-docker-file
.PHONY: internal-check
internal-check: internal-check-docker-file
# wait for the localstack container to have a healthy status
.PHONY: wait-container-healthy
wait-container-healthy:
@while [ "`docker ps -a --filter=status=running | awk '$$1 != "CONTAINER" {print $$1}' | xargs -I {} docker container inspect -f {{.State.Health.Status}} {}`" != "healthy" ]; do \
echo "Waiting for container to come up healthy..." && sleep 2; \
done;
#####################
# Terraform Targets #
#####################
# create the s3 bucket in the localstack container for terraform remote state
.PHONY: tf-create-s3-bucket
tf-create-s3-bucket:
@AWS_PAGER="" aws --endpoint-url http://localhost:4566 s3api create-bucket --bucket $(TF_BUCKET_NAME) --region $(REGION) --create-bucket-configuration LocationConstraint=$(REGION)
# create the dynamodb table in the localstack container for terraform remote state locking
.PHONY: tf-create-dynamodb
tf-create-dynamodb:
@AWS_PAGER="" aws --endpoint-url http://localhost:4566 dynamodb create-table --table-name $(TF_DYNAMO_NAME) --region $(REGION) --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
# run a terraform init with the backend using the localstack container
.PHONY: tf-init
tf-init:
@terraform init -backend-config=$(DEV_BACKEND_CONFIG)
# run a terraform apply against the localstack container using the development tfvars file
.PHONY: tf-apply
tf-plan:
@terraform apply -var-file=$(DEV_VARS_FILE) -auto-approve
#######################
# Development Targets #
#######################
# creates and deploys localstack development environment, applies terraform resources against it, then tears it all down (intended for CI)
.PHONY: integration-test-ci
integration-test-ci: internal-check deploy-localstack bootstrap-tf-backend deploy-terraform clean
# creates and deploys a localstack development environment, then applies terraform resources against it
.PHONY: local-development
local-development: internal-check deploy-localstack bootstrap-tf-backend deploy-terraform
##################
# Helper Targets #
##################
# builds and runs a localstack container
.PHONY: deploy-localstack
deploy-localstack: build-container run-container
# creates the resources needed for terraform to use a remote s3 backend in localstack
.PHONY: bootstrap-tf-backend
bootstrap-tf-backend: wait-container-healthy tf-create-s3-bucket tf-create-dynamodb
# applies terraform resources against the localstack container
.PHONY: deploy-terraform
deploy-terraform: tf-init tf-plan
# stop all localstack containers and remove the images
.PHONY: clean
clean: stop-container clean-docker