-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
82 lines (66 loc) · 3.07 KB
/
Copy pathMakefile
File metadata and controls
82 lines (66 loc) · 3.07 KB
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
# Make sure these match the inputs in Terraform
APP_NAME := tumpr
ENVIRONMENT := dev
TERRAFORM_DIR := ./terraform
LAMBDAS_DIR := ./lambdas
LAMBDAS = backend_api dummy_lambda
# =============================================================================
# Docker Container Registry
# =============================================================================
# Login to AWS ECR
docker_login:
@aws ecr get-login-password \
--region ${region} | docker login \
--username AWS \
--password-stdin ${account_id}.dkr.ecr.${region}.amazonaws.com
# =============================================================================
# Terraform & IaC
#
# When creating a container image-based lambda function for the first time,
# the ECR must first contain an image. The following targets are used to:
# - apply the ECR defined in the backend Terraform (without building any other
# infrastructure)
# - build and push a dummy container to the ECR
# This allows a later Terraform apply to create the lambda function
# =============================================================================
# Target to set up the ECR repository using Terraform
setup_ecr:
cd $(TERRAFORM_DIR) &&\
terraform apply -target="module.${lambda_name}.aws_ecr_repository.lambda_image"
# Target to get the image URI from Terraform outputs
get_ecr_url:
$(eval ecr_url := $(shell cd $(TERRAFORM_DIR) && terraform output -raw ${lambda_name}_ecr_url))
# Target to build and push an empty container (alpine) to the ECR repository
push_empty_container: get_ecr_url
docker pull alpine; \
docker tag alpine ${ecr_url}:latest; \
docker push ${ecr_url}:latest;
# Composite target to initialize the ECR repository and push an empty container
initialise_ecr: | setup_ecr docker_login push_empty_container
# Iterate over all defined lambda functions and initialise them
initialise_all_ecrs:
@for lambda in $(LAMBDAS); do \
$(MAKE) initialise_ecr lambda_name=$$lambda region=${region} account_id=${account_id}; \
done
outputs_to_env:
cd $(TERRAFORM_DIR) && \
terraform output >> ../lambdas/resources.env && \
terraform output >> ../frontend/.env
# =============================================================================
# Docker
# =============================================================================
build_image:
cd ${LAMBDAS_DIR}/${lambda_name} && \
docker build --platform linux/amd64 -t ${prefix}${lambda_name}:${build_no} .
tag_image:
docker tag ${prefix}${lambda_name}:${build_no} ${account_id}.dkr.ecr.${region}.amazonaws.com/${prefix}${lambda_name}:latest
push_image:
docker push ${account_id}.dkr.ecr.${region}.amazonaws.com/${prefix}${lambda_name}:latest
build_and_push_docker_image: docker_login build_image tag_image push_image
# =============================================================================
# AWS Lambda
# =============================================================================
update_lambda_with_latest_image:
aws lambda update-function-code \
--function-name ${prefix}${lambda_name} \
--image-uri ${account_id}.dkr.ecr.${region}.amazonaws.com/${prefix}${lambda_name}:latest