Skip to content

Commit 95538f8

Browse files
committed
Add python support
1 parent cdd3960 commit 95538f8

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

Dockerfile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ENV NODE_VERSION_4 4.3.2
77
ENV NODE_VERSION_6 6.10.2
88

99
# Install gcc add utilities to manage users and permissions
10-
RUN yum install -y gcc-c++ util-linux shadow-utils
10+
RUN yum install -y gcc-c++ util-linux shadow-utils zlib-devel openssl-devel
1111

1212
# Install node v4 and node v6 as commands "node4" and "node6"
1313
# Command "node" defaults to v6
@@ -27,6 +27,19 @@ RUN cd /opt &&\
2727
ln -s /opt/node-v${NODE_VERSION_6}-linux-x64/bin/npm /usr/local/bin/npm &&\
2828
/opt/node-v${NODE_VERSION_6}-linux-x64/bin/npm install -g npm@4
2929

30+
# Install python 3.6 and pip, python 2.7 is already available
31+
RUN curl -O https://bootstrap.pypa.io/get-pip.py &&\
32+
python get-pip.py &&\
33+
curl -O https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz &&\
34+
tar zxvf Python-3.6.1.tgz &&\
35+
cd Python-3.6.1 &&\
36+
./configure --prefix=/opt/python3 &&\
37+
make &&\
38+
make install &&\
39+
ln -s /opt/python3/bin/python3 /usr/bin/python3 &&\
40+
ln -s /opt/python3/bin/pip3 /usr/bin/pip3
41+
42+
3043
# Add a script to modify the UID / GID for the default user if needed
3144
COPY /usr/local/bin/change-uid /usr/local/bin/change-uid
3245
RUN chmod +x /usr/local/bin/change-uid

README.md

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
## Why?
44

5-
Some node modules require a build system because they contain c++ binding. Once deployed, the compiled module may not be
6-
complatible with the Amazon Lambda execution environment.
5+
Some node and python modules require a build system because they contain c++ binding. Once deployed, the compiled
6+
module may not be complatible with the Amazon Lambda execution environment.
77

88
A common solution to this problem is to install the package on an EC2 instance using an Amazon Linux AMI and then to
99
deploy it in Amazon Lambda. Building serverless applications, it is ironic to be obliged to use a server to deploy code.
1010

1111
This docker image is based on the [Amazon Linux](https://hub.docker.com/_/amazonlinux/) image and contains `gcc`,
12-
`node 4.3.2`, `node 6.10.2` and `npm 4` to create packages for Amazon Lambda.
12+
`python 2.7`, `python 3.6`, `pip`, `node 4.3`, `node 6.10` and `npm 4` to create packages for Amazon Lambda.
1313

1414
Using the docker image `myrmex/lambda-packager`, avoid errors like these during execution in Amazon Lambda:
1515

@@ -23,37 +23,56 @@ Module version mismatch. Expected 46, got 48.
2323

2424
## Usage
2525

26-
You can use a docker volume to mount the code of the Lambda it in a container. The directory where `npm install` is
27-
executed inside the container is `/data`. By default, the installation will be performed for node 6.10.2.
26+
You can use a docker volume to mount the code of the Lambda it in a container. The directory where `npm install` or
27+
`pip install` is executed inside the container is `/data`. By default, the installation will be performed for node
28+
6.10.
2829

2930
```bash
3031
docker run `pwd`:/data myrmex/lambda-packager
3132
```
3233

3334
The image does not create the zip archive for your. It only install the dependencies in an environment compatible with
34-
Lambda. You will still have to zip the result and create / update the Lambda function in AWS.
35+
Lambda. You will still have to zip the result and deploy it in Amazon Lambda.
3536

36-
Take care that `node_module` does not already exist before running the command.
37+
For a node package, take care that `node_module` does not already exist before running the command.
3738

3839
### Managing permissions
3940

40-
The user that performs `npm install` inside the container may have a `uid/gid` that differs from the `uid/gid` of the
41-
host machine and npm may not be able to perform the installation. The image `myrmex/lambda-packager` accepts two
42-
environment variables that allows to modify the `uid/gid` of the container's user: `HOST_UID` and `HOST_GID`. If
43-
`HOST_GID` is omitted, its value will be set to the value of `HOST_UID`.
41+
The user that performs `npm install` or `pip install` inside the container may have a `uid/gid` that differs from the
42+
`uid/gid` of the host machine and will not be able to perform the installation. The image `myrmex/lambda-packager`
43+
accepts two environment variables that allows to modify the `uid/gid` of the container's user: `HOST_UID` and
44+
`HOST_GID`. If `HOST_GID` is omitted, its value will be set to the value of `HOST_UID`.
4445

4546
```bash
4647
docker run -e HOST_UID=`id -u` -e HOST_GID=`id -g` -v `pwd`:/data myrmex/lambda-packager
4748
```
4849

49-
### Node 4
50+
### Selecting the runtime
5051

51-
The `RUNTIME` environment variable allows to perform the installation for node 4.3.2
52+
The `RUNTIME` environment variable allows to choose the runtime.
53+
54+
#### Node 6.10
55+
56+
Node 6.10 is the default runtime and does not require any special configuration.
57+
58+
#### Node 4.3
5259

5360
```bash
5461
docker run -e RUNTIME=node4 -v `pwd`:/data myrmex/lambda-packager
55-
# or
56-
docker run -e RUNTIME=node4 -e HOST_UID=`id -u` -e HOST_GID=`id -g` -v `pwd`:/data myrmex/lambda-packager
62+
```
63+
64+
#### Python 3.6
65+
66+
```bash
67+
docker run -e RUNTIME=python3 -v `pwd`:/data myrmex/lambda-packager
68+
```
69+
70+
#### Python 2.7
71+
72+
Accepts the values `python` or `python2`.
73+
74+
```bash
75+
docker run -e RUNTIME=python -v `pwd`:/data myrmex/lambda-packager
5776
```
5877

5978
### Default command
@@ -62,8 +81,6 @@ To be able to change the user `uid/gid`, the container is executed with the root
6281
need to use a non root user because it is a good practice and it avoids some [bad surprises with
6382
`node-gyp`](https://github.com/nodejs/node-gyp/issues/454).
6483

65-
So the default command is `su myrmex -c "npm install --production"`, `myrmex` beeing the name of the non root user.
66-
67-
## What's next?
84+
So, the default command is `su myrmex -c "npm install --production"`, `myrmex` beeing the name of the non root user.
6885

69-
Support for python runtimes
86+
For python, the command is `su myrmex -c "pip install --requirement requirements.txt --upgrade --target ."`

cmd.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#!/bin/bash
22
set -e
33

4-
su $DEFAULT_USER -c "npm install --production"
4+
if [ "$RUNTIME" = "python" or "$RUNTIME" = "python2" ]; then
5+
su $DEFAULT_USER -c "pip install --requirement requirements.txt --upgrade --target ."
6+
elif [ "$RUNTIME" = "python3" ]; then
7+
su $DEFAULT_USER -c "pip3 install --requirement requirements.txt --upgrade --target ."
8+
else
9+
su $DEFAULT_USER -c "npm install --production"
10+
fi

0 commit comments

Comments
 (0)