Created
October 6, 2021 00:58
-
-
Save samloh84/94b415d07094afb28c03c1f0b34f381f to your computer and use it in GitHub Desktop.
AWS ECR Terraform + Docker Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SHELL := /bin/bash | |
.SHELLFLAGS := -ec | |
.PHONY: apply destroy refresh output | |
TERRAFORM_TFVARS_FILE_PATTERNS := *.tfvars | |
define _LOAD_AWSRC := | |
if [[ -f "~/.awsrc" ]]; then \ | |
source "~/.awsrc"; \ | |
fi; \ | |
if [[ -f "$(CURDIR)/.awsrc" ]]; then \ | |
source "$(CURDIR)/.awsrc"; \ | |
fi | |
endef | |
define _SET_TERRAFORM_VAR_FILE_ARGS := | |
if [[ ! -z "$(TERRAFORM_TFVARS_FILE_PATTERNS)" ]]; then \ | |
TERRAFORM_TFVARS_FILE_PATTERNS=($(TERRAFORM_TFVARS_FILE_PATTERNS)); \ | |
else \ | |
TERRAFORM_TFVARS_FILE_PATTERNS=(); \ | |
fi; \ | |
TERRAFORM_VAR_FILE_ARGS=(); \ | |
for TERRAFORM_TFVARS_FILE_PATTERN in $${TERRAFORM_TFVARS_FILE_PATTERNS[@]}; do \ | |
for TERRAFORM_TFVARS_FILE in $$(find $(CURDIR) -name "$${TERRAFORM_TFVARS_FILE_PATTERN}"); do \ | |
TERRAFORM_VAR_FILE_ARGS+=("-var-file" "$${TERRAFORM_TFVARS_FILE}"); \ | |
done; \ | |
done | |
endef | |
all: apply | |
apply: | |
set -euxo pipefail; \ | |
$(call _LOAD_AWSRC); \ | |
$(call _SET_TERRAFORM_VAR_FILE_ARGS); \ | |
TERRAFORM_APPLY_ARGS=("-auto-approve" "$${TERRAFORM_VAR_FILE_ARGS[@]}"); \ | |
terraform init; \ | |
terraform apply "$${TERRAFORM_APPLY_ARGS[@]}" | |
destroy: | |
set -euxo pipefail; \ | |
$(call _LOAD_AWSRC); \ | |
$(call _SET_TERRAFORM_VAR_FILE_ARGS); \ | |
TERRAFORM_DESTROY_ARGS=("-auto-approve" "$${TERRAFORM_VAR_FILE_ARGS[@]}"); \ | |
terraform init; \ | |
terraform destroy "$${TERRAFORM_DESTROY_ARGS[@]}" | |
refresh: | |
set -euxo pipefail; \ | |
$(call _LOAD_AWSRC); \ | |
$(call _SET_TERRAFORM_VAR_FILE_ARGS); \ | |
TERRAFORM_REFRESH_ARGS=("$${TERRAFORM_VAR_FILE_ARGS[@]}"); \ | |
terraform init; \ | |
terraform refresh "$${TERRAFORM_REFRESH_ARGS[@]}" | |
output: | |
set -euxo pipefail; \ | |
$(call _LOAD_AWSRC); \ | |
terraform init; \ | |
terraform output | |
docker_login: | |
set -euxo pipefail; \ | |
$(call _LOAD_AWSRC); \ | |
terraform init; \ | |
DOCKER_HOSTNAME=("$$(terraform output -json | jq -r '.ecr_registry_repository_url.value' | cut -d/ -f1)"); \ | |
aws ecr get-login-password | docker login --username AWS --password-stdin "$${DOCKER_HOSTNAME}" | |
docker_build: | |
set -euxo pipefail; \ | |
$(call _LOAD_AWSRC); \ | |
terraform init; \ | |
DOCKER_REGISTRY=("$$(terraform output -json | jq -r '.ecr_registry_repository_url.value'):latest"); \ | |
docker build --tag "$${DOCKER_REGISTRY}" $$(pwd)/../../../docker/cloudflared | |
docker_push: docker_login docker_build | |
set -euxo pipefail; \ | |
DOCKER_REGISTRY=("$$(terraform output -json | jq -r '.ecr_registry_repository_url.value'):latest"); \ | |
docker push "$${DOCKER_REGISTRY}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment