-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-sam.sh
157 lines (124 loc) · 3.79 KB
/
aws-sam.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
set -e
export AWS_PAGER=""
STACK_PREFIX="${STACK_PREFIX:-$USER}"
DEFAULT_STACK_NAME="$STACK_PREFIX-$(jq < package.json -r '.name')"
STACK_NAME="${STACK_NAME:-$DEFAULT_STACK_NAME}"
if [ -z "$AWS_REGION" ]; then
echo "AWS_REGION is not set. Exiting."
exit 1
fi
if [ -z "$AWS_ACCOUNT_ID" ]; then
echo "AWS_ACCOUNT_ID is not set. Exiting."
exit 2
fi
DOCKER_REPO_URL=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
DOCKER_IMAGE_NAME=$(jq < package.json -r '.name')
DOCKER_IMAGE_TAG=$(jq < package.json -r '.version')
DOCKER_IMAGE=$DOCKER_REPO_URL/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
sam.deploy() {
if ! aws ecr describe-repositories --repository-names "$DOCKER_IMAGE_NAME" 2>/dev/null; then
echo "Creating ECR repository: $DOCKER_REPO_URL/$DOCKER_IMAGE_NAME"
aws ecr create-repository --repository-name "$DOCKER_IMAGE_NAME"
else
echo "ECR repository already exists: $DOCKER_REPO_URL/$DOCKER_IMAGE_NAME"
fi
echo "Building Docker image: $DOCKER_IMAGE"
docker build --quiet . -t "$DOCKER_IMAGE"
echo "Logging in to ECR"
aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$DOCKER_REPO_URL"
echo "Pushing Docker image to ECR: $DOCKER_IMAGE"
docker push "$DOCKER_IMAGE"
# The application relies on the existence of the Docker image in ECR
# Otherwise, the deployment will never reach the created state
# We pushed the image to ECR to ensure it exists before deploying the application
rain merge $(find templates -iname '*.yml') > template.yml
echo "Deploying the application"
echo " - ImageName: $DOCKER_IMAGE_NAME"
echo " - ImageTag: $DOCKER_IMAGE_TAG"
echo
sam deploy \
--stack-name "$STACK_NAME" \
--parameter-overrides "ImageName=$DOCKER_IMAGE_NAME ImageTag=$DOCKER_IMAGE_TAG"
sam list stack-outputs --stack-name "$STACK_NAME"
}
sam.destroy.help() {
echo "Usage: $0 destroy [options]"
echo
echo "Options:"
echo " -e, --no-ecr Delete the ECR repository and its contents"
}
sam.destroy.opts() {
no_ecr=0
while [ $# -gt 0 ] ; do
case $1 in
-e | --no-ecr)
no_ecr=1
;;
*)
echo "Invalid option: $1"
echo
sam.destroy.help
exit 1
;;
esac
shift
done
}
sam.destroy() {
sam.destroy.opts "$@"
# Delete the SAM application
echo "Deleting the application: $STACK_NAME"
sam delete --stack-name "$STACK_NAME" --no-prompts
# Delete the ECR repository and its contents
if [ "$no_ecr" -eq 0 ]; then
echo "Deleting the ECR repository: $DOCKER_REPO_URL/$DOCKER_IMAGE_NAME"
aws ecr delete-repository --repository-name "$DOCKER_IMAGE_NAME" --force
fi
}
sam.outputs() {
sam list stack-outputs --stack-name "$STACK_NAME"
}
ecs.redeploy() {
# Update the ECS service with the new task definition
outputs=$(sam list stack-outputs --stack-name "$STACK_NAME" --output json)
cluster=$(echo "$outputs" | jq -r '.[] | select(.OutputKey == "ClusterName") | .OutputValue')
service=$(echo "$outputs" | jq -r '.[] | select(.OutputKey == "ServiceName") | .OutputValue')
echo "Updating ECS service: $service in cluster: $cluster"
aws ecs update-service \
--cluster "$cluster" \
--service "$service" \
--force-new-deployment
aws ecs wait services-stable --cluster "$cluster" --services "$service"
}
help() {
echo "Usage: $0 {deploy|destroy|redeploy}"
echo
echo " deploy - Deploy the application"
echo " destroy - Destroy the application"
echo " redeploy - Recreate the existing ECS task"
}
main() {
case "$1" in
deploy)
sam.deploy "${@:2}"
;;
destroy)
sam.destroy "${@:2}"
;;
redeploy)
ecs.redeploy "${@:2}"
;;
outputs)
sam.outputs
;;
help)
help
;;
*)
help
exit 1
;;
esac
}
main "$@"