Skip to content

Commit 5cf64a3

Browse files
committed
Refactor to multiple python versions & test integration
1 parent 671f511 commit 5cf64a3

File tree

10 files changed

+88
-11
lines changed

10 files changed

+88
-11
lines changed

Dockerfile

Lines changed: 0 additions & 6 deletions
This file was deleted.

Dockerfile-2.7

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:2.7-alpine
2+
LABEL maintainer="Kilna kilna@kilna.com"
3+
RUN pip install python-lambda &&\
4+
mkdir /lambda
5+
COPY bin/* /usr/local/bin/
6+
WORKDIR /lambda

Dockerfile-3.3

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.3-alpine
2+
LABEL maintainer="Kilna kilna@kilna.com"
3+
RUN pip install python-lambda &&\
4+
mkdir /lambda
5+
WORKDIR /lambda

Dockerfile-3.4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.4-alpine
2+
LABEL maintainer="Kilna kilna@kilna.com"
3+
RUN pip install python-lambda &&\
4+
mkdir /lambda
5+
WORKDIR /lambda

Dockerfile-3.5

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.5-alpine
2+
LABEL maintainer="Kilna kilna@kilna.com"
3+
RUN pip install python-lambda &&\
4+
mkdir /lambda
5+
WORKDIR /lambda

Dockerfile-3.6

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.6-alpine
2+
LABEL maintainer="Kilna kilna@kilna.com"
3+
RUN pip install python-lambda &&\
4+
mkdir /lambda
5+
WORKDIR /lambda

README.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ Lightweight docker image for running and packaging python-based AWS lambda code.
44
* [alpine-aws-python-lambda on docker hub](https://hub.docker.com/r/kilna/alpine-aws-python-lambda/)
55
* [alpine-aws-python-lambda on github](https://github.com/kilna/alpine-aws-python-lambda)
66

7+
# Purpose
8+
9+
I needed a Docker-based environment in which to host AWS python lambda functions for the purpose of testing and building them... python-lambda works well under virtualenv for development, but build and deployment automation require a clean and reproducible environment to operate in. Our CI system already supported Docker as a containerization sytem, so it was the obvious choice.
10+
11+
In order to use this, you will have your project derive its own Dockerfile based on a base alpine-aws-python-lambda image corresponding to which version of Python you wish to run.
12+
713
# Usage
814

9-
An example of a usable project can be found in the [example/](./example/) directory. This lambda function takes a JSON input file like the provided [event.json](./example/event.json) and returns an ASCII-art version of the text described in it. The provided [Dockerfile](./example/Dockerfile) derives from this image and loads the current workspace into the image, then installs dependencies from the [requirements.txt](./example/requirements.txt) file.
15+
An example of a usable project can be found in the [example/](./example/) directory. This lambda function takes a JSON input file like the provided [event.json](./example/event.json) and returns an ASCII-art version of the text described in it. The provided [Dockerfile](./example/Dockerfile) derives from this image and loads the current workspace into the image, then installs dependencies from the [requirements.txt](./example/requirements.txt) file.
1016

1117
To build a docker image called _example-lambda-image_ with the example lambda function in it, run:
1218

@@ -33,11 +39,39 @@ If you would like to see if your lambda function builds properly, run:
3339
$ docker run example-lambda-image lambda build
3440
```
3541

36-
If you would like to get a ZIP file of the lambda function suitable for uploading to Amazon, and the build log in one command:
42+
## Build and Test
43+
44+
If you would like to build and test the lambda function and gather up the results, you can run:
3745

3846
```
39-
$ docker run example-lambda-image sh -c 'rm -rf build.log dist || true && lambda build &>build.log && tar -c build.log dist' | tar -x -v
47+
$ docker run example-lambda-image lambda_build_tar | tar -x -v
4048
build.log
49+
test.log
4150
dist/
4251
dist/2017-09-01-003647-example-lambda.zip
4352
```
53+
54+
Behind the scenes, what this script does is:
55+
56+
* Removes any log and dist files from prior runs
57+
* Runs 'lambda build' and store the log in _/lambda/build.log_ in the container
58+
* If present and executable, run _/lambda/run_tests_ and store the log in /lambda/test.log in the container
59+
* Tars the log files, and the contents of the dist directory in /lambda on the container and pipes it to standard output
60+
* Untars the contents bundled up within the container, and extracts them into your current directory
61+
62+
## Python Version
63+
64+
The example Dockerfile uses _:latest_ in the FROM line, which is currently the same as _:python-3.6_, but if you wish to use different python versions you can change this.
65+
66+
For instance, if you want to use Python version 2.7 change the first line of your Dockerfile from:
67+
68+
```
69+
FROM kilna/alpine-aws-python-lambda:latest
70+
```
71+
72+
To:
73+
74+
```
75+
FROM kilna/alpine-aws-python-lambda:python-2.7
76+
```
77+

bin/lambda_build_tar

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
# This file runs 'lambda build', saving the log to build.log, then tars both
4+
# the dist/ directory and the build.log file to STDOUT
5+
6+
set -e
7+
cd /lambda
8+
rm -rf *.log dist &>/dev/null || true
9+
lambda build &>build.log
10+
if [ -x 'run_tests' ]; then
11+
./run_tests &>test.log
12+
fi
13+
tar -c *.log dist
14+

example/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM kilna/alpine-aws-python-lambda
2-
COPY . /workspace
1+
FROM kilna/alpine-aws-python-lambda:latest
2+
COPY . /lambda
33
RUN pip install -r requirements.txt

example/run_tests

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -o pipefail
5+
cd /lambda
6+
if [ `lambda invoke | md5sum | cut -d ' ' -f 1` != '5cd3139eadee0a85b2bb9f2164f3b6f6' ]; then
7+
echo 'ERROR: md5sum of output returned by lambda invoke did not match expected results'
8+
fi
9+

0 commit comments

Comments
 (0)