forked from aws-samples/step-up-auth
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeploy-awscli.sh
executable file
·112 lines (97 loc) · 3.19 KB
/
deploy-awscli.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
#!/bin/sh
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
## initialize and do some pre-flight checks
if [ -z "$AWS_PROFILE" -o -z "$AWS_REGION" -o -z "$AWS_LAMBDA_ROLE_ARN" -o -z "$ENV_PREFIX" ]; then
echo "Missing environment variables. Ensure following environment variables are set"
echo " AWS_PROFILE"
echo " AWS_REGION"
echo " AWS_LAMBDA_ROLE_ARN"
echo " ENV_PREFIX"
exit 1
fi
## Change variables as necessary
module_name="step-up-auth-sample-api-lambda"
## DO NOT MODIFY
NODE_ENV="${DEPLOYMENT_ENV}"
if [ -z "${NODE_ENV}" ]; then
NODE_ENV="production"
fi
module_build="build"
module_dist="${module_build}/dist"
module_zip="${module_build}/compressed"
module_export="src/index.handler"
WORKING_DIR="`pwd`"
# show some stats
echo "AWS_PROFILE: ${AWS_PROFILE}"
echo "AWS_REGION: ${AWS_REGION}"
echo "AWS_LAMBDA_ROLE_ARN: ${AWS_LAMBDA_ROLE_ARN}"
echo "ENV_PREFIX: ${ENV_PREFIX}"
echo "WORKING_DIR: ${WORKING_DIR}"
echo "NODE_ENV: ${NODE_ENV}"
echo
## creating distribution zip
echo "creating distribution zip"
# check for dist folder
if [ ! -d "${module_dist}" ]; then
echo "dist directory is empty. forgot to run build.sh?"
exit 1
fi
# compress dist
dist_version=`grep '.*"version".*:.*' package.json | sed 's/,//g' | sed 's/.*:.*"\(.*\)"$/\1/g'`
dist_name_prefix=`grep '.*"name".*:.*' package.json | sed 's/,//g' | sed 's/\@//g' | sed 's/\//-/g' | sed 's/.*:.*"\(.*\)"$/\1/g'`
dist_name_zip="${dist_name_prefix}-${dist_version}.zip"
(
cd "${module_dist}"
rm -rf "${WORKING_DIR}/${module_zip}/${dist_name_zip}"
zip -9r "${WORKING_DIR}/${module_zip}/${dist_name_zip}" . > /tmp/$$.zip.log 2>&1
if [ $? -ne 0 ]; then
echo "zip command failed ${WORKING_DIR}/${module_zip}/${dist_name_zip}: $?"
echo "zip logs:"
cat /tmp/$$.zip.log
rm -rf /tmp/$$.*
exit 2
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
## deploy lambda
echo "create/update lambda"
# get module fully qualified path and file name
module_zip_path=`ls ${module_zip}/${dist_name_zip}`
# check if lambda exits:
# if yes, run "aws update-function-code"
# if no, run "aws create-function"
aws lambda get-function --function-name ${module_name} --profile ${AWS_PROFILE} --region ${AWS_REGION} >& /tmp/$$.get.lambda
if [ $? -ne 0 ]; then
aws lambda create-function \
--region ${AWS_REGION} \
--function-name ${module_name} \
--zip-file fileb://"${module_zip_path}" \
--runtime nodejs18.x \
--tracing-config Mode=PassThrough \
--timeout 30 \
--memory-size 128 \
--environment Variables="{NODE_ENV=${NODE_ENV},ENV_PREFIX=${ENV_PREFIX}}" \
--role ${AWS_LAMBDA_ROLE_ARN} \
--handler ${module_export} \
--profile ${AWS_PROFILE} >& /tmp/$$.lambda.create
cat /tmp/$$.lambda.create
else
aws lambda update-function-code \
--region ${AWS_REGION} \
--function-name ${module_name} \
--zip-file fileb://"${module_zip_path}" \
--profile ${AWS_PROFILE} >& /tmp/$$.lambda.update
cat /tmp/$$.lambda.update
fi
# check for error
if [ $? -ne 0 ]; then
echo "unable to create/update lambda"
rm -rf /tmp/$$.*
exit 4
fi
## clean up
rm -rf /tmp/$$.*
echo "done"