Last active
February 12, 2017 19:40
-
-
Save seppestas/6b13b65dd04e4c3c800e to your computer and use it in GitHub Desktop.
AWS Lambda deployment package maker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) Productize | |
# This is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License version 3 | |
# as published by the Free Software Foundation. | |
# You can get the license at <http://www.gnu.org/licenses/gpl.html> | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# AWS Lambda deployment package maker | |
# | |
# This makefile allows to easily create and upload AWS Lambda deployment packages. | |
# | |
# To use it, just create a `Makefile` and include this file. | |
# e.g: echo "include awslambda.mk" > Makefile | |
# | |
# It will automatically search for Python modules installed by pip and add them | |
# to the zip file uploaded to AWS Lambda based on the requirements.txt file. | |
# | |
# The Sha256 checksum of the zip file is calculated so up-to-date zip files | |
# are not sent unnecessarily. | |
# | |
# Notes: | |
# - Using requirements with a required version is not (yet) supported. | |
# - Removing a source file or requirement will not cause it to be removed | |
# from the zip file. Use `make clean` if this is a problem. | |
# | |
# Dependencies: | |
# - Zip (Duh): e.g http://www.info-zip.org/Zip.html | |
# - GNU make (obviously): https://www.gnu.org/software/make/ | |
# - AWS cli: https://aws.amazon.com/cli/ | |
# - jq (to parse results from aws cli): https://stedolan.github.io/jq/ | |
# - pip (to find the location of packages and install missing ones): | |
# https://pypi.python.org/pypi/pip | |
# - OpenSSL toolkit (for calculating checksums): https://www.openssl.org/ | |
FN=$(shell cat configuration.json | jq -r '.["FunctionName"]') | |
WD=$(shell pwd) | |
SRC=$(shell find src -type f) | |
REQUIREMENTS=$(shell if [ -a requirements.txt ]; then echo requirements.zip; fi;) | |
define get_package_location | |
$(shell pip show $1 | while read -r line; do \ | |
if [ "$${line% *}" = "Location:" ]; then \ | |
echo $${line#* }; \ | |
fi; \ | |
done;) | |
endef | |
all: $(FN).zip | |
clean: | |
rm -f requirements.zip | |
rm -f $(FN).zip | |
# Put required packages in zip file | |
requirements.zip: requirements.txt | |
@echo "Adding required modules" | |
@rm -f requirements.zip | |
@while read -r req; do \ | |
pkg_info=`pip show $$req`; \ | |
if [ "$$pkg_info" = "" ]; then \ | |
echo "Requirement $$req not found"; \ | |
read -p 'Do you want to install it (pip install '$$req')? [Y/n]: ' continue </dev/tty; \ | |
if [ "$$continue" = "N" ] || [ "$$continue" = "n" ]; then \ | |
continue; \ | |
else \ | |
pip install $$req; \ | |
pkg_info=`pip show $$req`; \ | |
fi; \ | |
fi; \ | |
echo "$$pkg_info" | while read -r line; do \ | |
if [ "$${line% *}" = "Location:" ]; then \ | |
cd $${line#* }; \ | |
zip -r $(WD)/requirements.zip $$req; \ | |
fi; \ | |
done; \ | |
done < requirements.txt | |
$(FN).zip: $(REQUIREMENTS) $(SRC) | |
@echo "Adding sources" | |
@if [ -a requirements.zip ]; then \ | |
cp requirements.zip $(FN).zip; \ | |
fi; | |
@cd src; zip -r -x="*.pyc" $(WD)/$(FN).zip * | |
@echo "Created $(FN).zip" | |
upload: $(FN).zip | |
@ \ | |
local_dgst=`cat $(FN).zip | \ | |
openssl dgst -sha256 -binary | \ | |
openssl base64`; \ | |
remote_dgst=`aws lambda get-function --output json --function-name $(FN) | \ | |
jq -r '.["Configuration"]["CodeSha256"]'`; \ | |
if [ "$$local_dgst" != "$$remote_dgst" ]; then \ | |
echo "Updating Lambda function"; \ | |
aws lambda update-function-code \ | |
--function-name $(FN) \ | |
--zip-file fileb://$(FN).zip; \ | |
else \ | |
echo "Lambda function is up-to-date"; \ | |
fi; | |
.PHONY: upload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment