Sign in

Python AWS Lambda

Follow our generic Python to setup your codebase. Next replace the Makefile with the one below, and adapt as needed. This Makefile will generate an AWS Lambda compliant zip file containing all the dependencies needed at runtime.

Linting and unit tests:

make lint
make test

To build the zip file:

make package

This zip file must be uploaded into an existing AWS Lambda function (for production workload this should be done by CI/CD).

To try the AWS Lambda function in AWS:

make upload
make invoke

Display the result:

cat out

Makefile

This Makefile example has been adapted to our needs based on the article How to build python code bundles for AWS Lambda quickly and easily.

APP_VERSION = 0.0.1
BUILD_DIR=$(SRC_DIR)/build
COV_MIN = 100
DIST_DIR=$(SRC_DIR)/dist
OUTPUT_ZIP=$(DIST_DIR)/lambda-$(APP_VERSION).zip
SRC_DIR=$(shell pwd)
STAGING_WHEELHOUSE=$(BUILD_DIR)/staging-wheelhouse
CACHE_WHEELHOUSE=$(BUILD_DIR)/cache-wheelhouse
STAGING_DIRECTORY=$(BUILD_DIR)/staging
STAGING_DIRECTORY_STAMP=$(BUILD_DIR)/staging-directory-stamp
STAGING_WHEELHOUSE_STAMP=$(BUILD_DIR)/staging-wheelhouse-stamp
CACHE_WHEELHOUSE_STAMP=$(BUILD_DIR)/cache-wheelhouse-stamp
TESTS = tests/
UPLOAD_CODE_STAMP=$(BUILD_DIR)/upload-stamp

build: clean configure lint test
	APP_VERSION=$(APP_VERSION) python setup.py bdist_wheel

clean:
	find . -name '__pycache__' -exec rm -rf {} +
	find . -name '*.pyc' -exec rm -f {} +
	find . -name '*.pyo' -exec rm -f {} +
	find . -name '*~' -exec rm -f {} +
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info
	rm -rf htmlcov/
	rm -rf .pytest_cache/
	rm -rf .coverage*

configure:
	APP_VERSION=$(APP_VERSION) pip install -e ".[dev]"

lint:
	flake8

test:
	pytest \
		--cov=lambda_test \
		--cov-report html \
		--cov-report term-missing \
		--cov-fail-under $(COV_MIN) \
		$(TESTS)

$(CACHE_WHEELHOUSE_STAMP):
	APP_VERSION=$(APP_VERSION) \
		pip wheel . \
		--wheel-dir=$(CACHE_WHEELHOUSE) \
		--find-links=$(CACHE_WHEELHOUSE)
	touch $@

$(STAGING_WHEELHOUSE_STAMP): $(CACHE_WHEELHOUSE_STAMP)
	rm -rf $(STAGING_WHEELHOUSE)
	APP_VERSION=$(APP_VERSION) \
		pip wheel . \
		--wheel-dir=$(STAGING_WHEELHOUSE) \
		--find-links=$(CACHE_WHEELHOUSE)
	touch $@

$(STAGING_DIRECTORY_STAMP): $(STAGING_WHEELHOUSE_STAMP)
	rm -rf $(STAGING_DIRECTORY)
	unzip -q "$(STAGING_WHEELHOUSE)/*.whl" -d $(STAGING_DIRECTORY)
	touch $@

$(OUTPUT_ZIP): build $(STAGING_DIRECTORY_STAMP)
	rm -f $(OUTPUT_ZIP)
	cd $(STAGING_DIRECTORY) && zip -q -X -9 -r $(OUTPUT_ZIP) *

package: $(OUTPUT_ZIP)

$(UPLOAD_CODE_STAMP): $(OUTPUT_ZIP)
	aws lambda update-function-code \
		--region eu-west-1 \
		--function-name LambdaTest \
		--zip-file fileb://$(OUTPUT_ZIP)
	touch $@

upload: $(UPLOAD_CODE_STAMP)

invoke: $(UPLOAD_CODE_STAMP)
	aws lambda invoke \
		--region eu-west-1 \
		--function-name LambdaTest \
		--invocation-type RequestResponse \
		--payload '{"key": "value"}' \
		out

.PHONY: all build clean configure lint test upload invoke