-
Notifications
You must be signed in to change notification settings - Fork 1
/
sam_build.sh
executable file
·51 lines (41 loc) · 1.32 KB
/
sam_build.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
#!/bin/bash
set -ex
# run this on the docker container; works on host too if environment variables
# set and sam is installed
# env setup
RESOURCE_NAME=$( < "${PROJECT_DIR}"/template.yaml grep -A1 -E '^Resources:$' | tail -1 | cut -d ':' -f 1 | awk '{print $1}')
BUILD_DIR=${PROJECT_DIR}/build
OUT_FILE=${PROJECT_DIR}/lambda.zip
# env verification
echo PROJECT_DIR="${PROJECT_DIR}"
echo RESOURCE_NAME="${RESOURCE_NAME}"
echo BUILD_DIR="${BUILD_DIR}"
echo OUT_FILE="${OUT_FILE}"
# make sure all required env vars have a value
for VAR in PROJECT_DIR RESOURCE_NAME BUILD_DIR OUT_FILE; do
if [ -z "${!VAR}" ]; then
echo "ERROR: no value found for ${!VAR}"
exit 1
fi
done
# clean up lambda.zip
rm -vf "${OUT_FILE}"
# opt out of amazon data collection
export SAM_CLI_TELEMETRY=0
REQUIREMENTS_FILE="${PROJECT_DIR}/src/requirements.txt"
if [ ! -f "${REQUIREMENTS_FILE}" ]; then
echo "No requirements file found, generating one..."
cd "${PROJECT_DIR}"
poetry self add poetry-plugin-export
poetry export -f requirements.txt --output "${REQUIREMENTS_FILE}"
cd -
fi
# build it
sam build \
--use-container \
--template-file "${PROJECT_DIR}"/template.yaml \
--build-dir "${BUILD_DIR}"
# zip it
(cd "${BUILD_DIR}"/"${RESOURCE_NAME}" && zip "${OUT_FILE}" -X -r ./*)
# clean up
rm -rf "${BUILD_DIR}"