-
Notifications
You must be signed in to change notification settings - Fork 11
/
build-upload-aws-lambda-function
executable file
·80 lines (69 loc) · 2.53 KB
/
build-upload-aws-lambda-function
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
#!/bin/bash -ex
#
# Build AWS Lambda function ZIP file and upload to S3
#
# Usage: ./build-upload-aws-lambda-function S3BUCKET S3KEY [HUGOVERSION]
#
# For example:
# ./build-upload-aws-lambda-function run.alestic.com lambda/aws-lambda-site-generator-hugo-0.17.zip 0.17
#
function ver_compare {
printf "%03d%03d%03d%03d" $(echo "$1" | tr '.' ' ')
}
s3bucket=${1:?Specify target S3 bucket name}
s3key=${2:?Specify target S3 key}
target=s3://$s3bucket/$s3key
hugo_version="${3:-0.80.0}"
tmpdir=$(mktemp -d /tmp/lambda-XXXXXX)
zipfile=$tmpdir/lambda.zip
# Add AWS Lambda function file to ZIP file
zip -r9 $zipfile index.py
download_url="https://github.com/gohugoio/hugo/releases/download/v${hugo_version}/hugo_extended_${hugo_version}_Linux-64bit.tar.gz"
# Hugo extended was not available before 0.43
# Hugo zip format changes on 0.21 0.16, and change download URL on 0.17 0.16 and before, so we have to treat them differently:
if [ $(ver_compare $hugo_version) -ge $(ver_compare 0.43) ] ; then
(
cd $tmpdir
wget -qO hugo.tar.gz $download_url
tar xvzf hugo.tar.gz
zip -r9 $zipfile hugo
)
elif [ $(ver_compare $hugo_version) -ge $(ver_compare 0.21) ] ; then
download_url="https://github.com/gohugoio/hugo/releases/download/v${hugo_version}/hugo_${hugo_version}_Linux-64bit.tar.gz"
(
cd $tmpdir
wget -qO hugo.tar.gz $download_url
tar xvzf hugo.tar.gz
zip -r9 $zipfile hugo
)
elif [ $(ver_compare $hugo_version) -ge $(ver_compare 0.17) ] ; then
download_url="https://github.com/gohugoio/hugo/releases/download/v${hugo_version}/hugo_${hugo_version}_Linux-64bit.tar.gz"
(
cd $tmpdir
wget -qO hugo.tar.gz $download_url
tar xvzf hugo.tar.gz
mv hugo_${hugo_version}*/hugo_${version}* ./hugo
zip -r9 $zipfile hugo
)
elif [ "$hugo_version" == "0.16" ] ; then
download_url="https://github.com/gohugoio/hugo/releases/download/v${hugo_version}/hugo_${hugo_version}_linux-64bit.tgz"
(
cd $tmpdir
wget -qO hugo.tar.gz $download_url
tar xvzf hugo.tar.gz
zip -r9 $zipfile hugo
)
elif [ $(ver_compare $hugo_version) -lt $(ver_compare 0.16) ] ; then
download_url="https://github.com/gohugoio/hugo/releases/download/v${hugo_version}/hugo_${hugo_version}_linux_amd64.tar.gz"
(
cd $tmpdir
wget -qO hugo.tar.gz $download_url
tar xvzf hugo.tar.gz
mv hugo_${hugo_version}*/hugo_${version}* ./hugo
zip -r9 $zipfile hugo
)
fi
# Upload AWS Lambda function ZIP file to S3
aws s3 cp --acl public-read $zipfile $target
# Clean up
rm -r $tmpdir