-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.sh
executable file
·140 lines (115 loc) · 4.32 KB
/
deploy.sh
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
#!/usr/bin/env bash
set -e
source .env
if [ -z $PROJECT_NAME ]; then
echo "PROJECT_NAME environment variable is not set."
exit 1
fi
if [ -z $AWS_ACCOUNT_ID ]; then
echo "AWS_ACCOUNT_ID environment variable is not set."
exit 1
fi
if [ -z $AWS_DEFAULT_REGION ]; then
echo "AWS_DEFAULT_REGION environment variable is not set."
exit 1
fi
if [ -z $ENVOY_IMAGE ]; then
echo "ENVOY_IMAGE environment variable is not set to App Mesh Envoy, see https://docs.aws.amazon.com/app-mesh/latest/userguide/envoy.html"
exit 1
fi
if [ -z $KEY_PAIR ]; then
echo "KEY_PAIR environment variable is not set. This must be the name of an SSH key pair, see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html"
exit 1
fi
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
ECR_IMAGE_PREFIX=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${PROJECT_NAME}
deploy_images() {
echo "Deploying Client and Server images to ECR..."
for app in client server; do
aws ecr describe-repositories --repository-name ${PROJECT_NAME}/${app} >/dev/null 2>&1 || aws ecr create-repository --repository-name ${PROJECT_NAME}/${app}
docker build -t ${ECR_IMAGE_PREFIX}/${app} ${DIR}/${app} --build-arg GO_PROXY=${GO_PROXY:-"https://proxy.golang.org"}
aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 373656256964.dkr.ecr.ap-northeast-1.amazonaws.com
# $(aws ecr get-login --no-include-email)
docker push ${ECR_IMAGE_PREFIX}/${app}
done
}
deploy_infra() {
echo "Deploying Cloud Formation stack: \"${PROJECT_NAME}-infra\" containing VPC and Cloud Map namespace..."
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--stack-name "${PROJECT_NAME}-infra"\
--template-file "${DIR}/infra.yaml" \
--capabilities CAPABILITY_IAM \
--parameter-overrides "ProjectName=${PROJECT_NAME}" "KeyPair=${KEY_PAIR}"
}
deploy_app() {
echo "Deploying Cloud Formation stack: \"${PROJECT_NAME}-app\" containing ALB, ECS Tasks, and Cloud Map Services..."
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--stack-name "${PROJECT_NAME}-app" \
--template-file "${DIR}/app.yaml" \
--capabilities CAPABILITY_IAM \
--parameter-overrides "ProjectName=${PROJECT_NAME}" "EnvoyImage=${ENVOY_IMAGE}" "EchoClientImage=${ECR_IMAGE_PREFIX}/client" "EchoServerImage=${ECR_IMAGE_PREFIX}/server"
}
deploy_mesh() {
echo "Deploying Cloud Formation stack: \"${PROJECT_NAME}-mesh\"..."
aws cloudformation deploy \
--no-fail-on-empty-changeset \
--stack-name "${PROJECT_NAME}-mesh" \
--template-file "${DIR}/mesh.yaml" \
--capabilities CAPABILITY_IAM \
--parameter-overrides "ProjectName=${PROJECT_NAME}"
}
print_bastion() {
echo "Bastion endpoint:"
ip=$(aws cloudformation describe-stacks \
--stack-name="${PROJECT_NAME}-infra" \
--query="Stacks[0].Outputs[?OutputKey=='BastionIp'].OutputValue" \
--output=text)
echo "${ip}"
}
print_endpoint() {
echo "Public endpoint:"
prefix=$(aws cloudformation describe-stacks \
--stack-name="${PROJECT_NAME}-app" \
--query="Stacks[0].Outputs[?OutputKey=='PublicEndpoint'].OutputValue" \
--output=text)
echo "${prefix}"
}
deploy_stacks() {
deploy_images
deploy_infra
deploy_mesh
deploy_app
print_bastion
print_endpoint
}
delete_cfn_stack() {
stack_name=$1
echo "Deleting Cloud Formation stack: \"${stack_name}\"..."
aws cloudformation delete-stack --stack-name $stack_name
echo 'Waiting for the stack to be deleted, this may take a few minutes...'
aws cloudformation wait stack-delete-complete --stack-name $stack_name
echo 'Done'
}
delete_images() {
for app in client server; do
echo "deleting repository \"${app}\"..."
aws ecr delete-repository \
--repository-name $PROJECT_NAME/$app \
--force
done
}
delete_stacks() {
# delete_cfn_stack "${PROJECT_NAME}-app"
delete_cfn_stack "${PROJECT_NAME}-infra"
delete_cfn_stack "${PROJECT_NAME}-mesh"
delete_images
echo "all resources from this tutorial have been removed"
}
action=${1:-"deploy"}
if [ "$action" == "delete" ]; then
delete_stacks
exit 0
fi
deploy_stacks