Skip to content

Commit 52a9fdf

Browse files
Merge pull request #50 from developmentseed/develop
updates to python geolambda
2 parents ff70a41 + 1da07d5 commit 52a9fdf

File tree

8 files changed

+67
-23
lines changed

8 files changed

+67
-23
lines changed

.circleci/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ jobs:
4141
docker tag developmentseed/geolambda:${VERSION} developmentseed/geolambda:latest
4242
docker push developmentseed/geolambda:latest
4343
docker push developmentseed/geolambda:${VERSION}
44+
cd python
45+
docker build --build-arg VERSION=${VERSION} --build-arg PYVERSION=3.6.1 . -t developmentseed/geolambda:${VERSION}-python36
46+
docker push developmentseed/geolambda:${VERSION}-python36
47+
cd ..
4448
# deploy public Lambda layers
4549
pip install awscli
4650
docker run --rm -v $PWD:/home/geolambda --entrypoint package.sh -it developmentseed/geolambda:${VERSION}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414
- Improve documentation
1515
- More libraries (CURL with http2, webp, zstd)
1616
- GeoTIFF now compiled from scratch rather than GGDAL builtin
17+
- Published public lambda layers - see README for ARNs
1718

1819
### Changed
1920
- Major refactor, GeoLambda base now runtime agnostic contains system libraries only

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ RUN \
175175
--with-hdf4=${PREFIX} \
176176
--with-hdf5=${PREFIX} \
177177
--with-netcdf=${PREFIX} \
178-
--with-web=${PREFIX} \
178+
--with-webp=${PREFIX} \
179179
--with-zstd=${PREFIX} \
180180
--with-threads=yes \
181181
--with-curl=${PREFIX}/bin/curl-config \

python/Dockerfile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ FROM developmentseed/geolambda:${VERSION}
33

44
LABEL maintainer="Matthew Hanson <matt.a.hanson@gmail.com>"
55

6-
ARG PYVER=3.6.1
6+
ARG PYVERSION=3.6.1
77

88
# install Python
99
ENV \
1010
PYENV_ROOT=/root/.pyenv \
1111
PATH=/root/.pyenv/shims:/root/.pyenv/bin:$PATH
1212

1313
RUN \
14-
echo PYVER ${PYVER}; \
1514
curl https://pyenv.run | bash; \
16-
pyenv install ${PYVER}; \
17-
pyenv global ${PYVER}; \
15+
pyenv install ${PYVERSION}; \
16+
pyenv global ${PYVERSION}; \
1817
pip install --upgrade pip
1918

20-
COPY requirements.txt ./
19+
COPY requirements*.txt ./
2120

2221
RUN \
23-
pip install -r requirements.txt
22+
pip install -r requirements-pre.txt; \
23+
pip install -r requirements.txt
24+
25+
COPY bin/* /usr/local/bin/

python/README.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@ The Python GeoLambda image is meant to be used as a template for your own Python
66

77
These instructions show how to use the files in this directory to create your own Python based geospatial Lambda function and get it deployed.
88

9-
### Create docker image
9+
1. Update requirements
1010

11-
First a Docker image will need to be created based on GeoLambda that includes the dependencies needed. Use the [Dockerfile](Dockerfile) can be used to create a new Docker image based on any version of GeoLambda with any version of Python by providing the versions as build arguments to `docker run`. This will install the specified version of Python along with any Python packages provided in [requirements.txt](requirements.txt).
11+
First a Docker image will need to be created based on GeoLambda that includes the dependencies needed. The first thing is to is update the `requirements-pre.txt` and `requirement.txt` files with the dependencies needed. The `requirements-pre.txt` is a way to specify build dependencies to packages in `requirements.txt`. For instance, rasterio requires numpy is installed before rasterio. This is a well known pip problem that is fixed in [PEP 518](https://www.python.org/dev/peps/pep-0518/) but is not used by all packages so this work-around is needed.
1212

13-
$ docker build . --build-arg VERSION=1.1.0 --build-arg PYVER=3.6.1 -t <myimage>:latest
13+
2. Create handler
1414

15-
If not provided, `VERSION` (the version of GeoLambda to use) will default to `latest` and `PYVER` (Python version) will default to `3.6.1`.
15+
An example Lambda handler is located at [lambda/lambda_function.py](lambda/lambda_function.py). Ideally most of the logic in your Lambda should be in packages and the handler should only be responsible for taking in an event, calling relevant functions, and returning some output. Keep the handler code as small as possible. Move more complex code into packages or make them other functions in the [lambda/lambda_function.py](lambda/lambda_function.py) file.
1616

17-
### Create handler
17+
3. Build image
1818

19-
An example Lambda handler is located at [lambda/lambda_function.py](lambda/lambda_function.py). Ideally most of the logic in your Lambda should be in packages and the handler should only be responsible for taking in an event, calling relevant functions, and returning some output. Keep the handler code as small as possible. Move more complex code into packages or make them other functions in the [lambda/lambda_function.py](lambda/lambda_function.py) file.
19+
Now, use the [Dockerfile](Dockerfile) can be used to create a new Docker image based on any version of GeoLambda with any version of Python by providing the versions as build arguments to `docker run`. This will install the specified version of Python along with any Python packages provided in [requirements.txt](requirements.txt).
2020

21-
### Create deployment package
21+
$ docker build . --build-arg VERSION=1.1.0 --build-arg PYVERSION=3.6.1 -t <myimage>:latest
2222

23-
All that's needed to create a deployment package is to install the Python packages into the lambda/ directory.
23+
If not provided, `VERSION` (the version of GeoLambda to use) will default to `latest` and `PYVERSION` (Python version) will default to `3.6.1`.
2424

25-
$ docker run -v ${PWD}:/home/geolambda -t <myimage>:latest pip install -r requirements -t lambda/
25+
4. Create deployment package
2626

27-
The lambda/ directory can now be zipped up and deployed as a Lambda function:
27+
All that's needed to create a deployment package is to install the Python packages into the lambda/ directory using the package-python.sh script. This copies the installed site-packages over the lambda directory, but excluding some libraries since they won't be needed on Lambda. This includes packages that are already pre-installed in Lambda like boto3, as well as files and libraries that wouldn't be used operationally (e.g., testing files).
2828

29-
$ cd lambda
30-
$ zip -ru ../lambda-deploy.zip ./
29+
$ docker run -v ${PWD}:/home/geolambda -t <myimage>:latest package-python.sh
3130

32-
### Testing deployment package
31+
This will also create a lambda-deployment.zip file in the current directory.
32+
33+
5. Testing deployment package
3334

3435
You can use the [LambCI Docker images](https://github.com/lambci/docker-lambda) to test out your handler in the Lambda environment by mounting the base GeoLambda Lambda layer (see the [GeoLambda README](../README.md)) and the lambda directory created above.
3536

@@ -40,11 +41,19 @@ $ docker run --rm -v ${PWD}/lambda:/var/task -v ${PWD}/../lambda:/opt \
4041

4142
The last argument is a JSON string that will be passed as the event payload to the handler function.
4243

44+
6. Deploy
45+
46+
Deploy the Lambda function now by zipping up the lambda directory and using the AWS CLI.
47+
48+
```
49+
$ aws lambda update-function-code --function-name <mylambda> --zip-file fileb://lambda-deploy.zip
50+
```
51+
4352
### Pre-built images
4453

4554
Builds of this default Python image are also available on Docker Hub as tags in the `developmentseed/geolambda` repository.
4655

47-
$ docker pull developmentseed/geolambda:1.1.0-python36
56+
$ docker pull developmentseed/geolambda:${VERSION}-python36
4857

4958
To run the image interactively:
5059

@@ -64,7 +73,7 @@ $ VERSION=${cat ../VERSION}
6473
$ docker build . --build-arg VERSION=${VERSION} -t developmentseed/geolambda:${VERSION}-python36
6574
```
6675

67-
Provide `--build-arg PYVER=X.Y.Z` to build other Python versions and change the tag to be `${VERSION}-pythonX.Y`
76+
Provide `--build-arg PYVERSION=X.Y.Z` to build other Python versions and change the tag to be `${VERSION}-pythonX.Y`
6877

6978

7079

python/bin/package-python.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# directory used for deployment
4+
export DEPLOY_DIR=lambda
5+
6+
# Get Python version
7+
PYVERSION=$(cat /root/.pyenv/version)
8+
MAJOR=${PYVERSION%%.*}
9+
MINOR=${PYVERSION#*.}
10+
PYVER=${PYVERSION%%.*}${MINOR%%.*}
11+
PYPATH=/root/.pyenv/versions/$PYVERSION/lib/python3.6/site-packages/
12+
13+
echo Creating deploy package for Python $PYVERSION
14+
15+
EXCLUDE="boto3* botocore* pip* docutils* *.pyc setuptools* wheel* coverage* testfixtures* mock* *.egg-info *.dist-info __pycache__ easy_install.py"
16+
17+
EXCLUDES=()
18+
for E in ${EXCLUDE}
19+
do
20+
EXCLUDES+=("--exclude ${E} ")
21+
done
22+
23+
rsync -ax $PYPATH/ $DEPLOY_DIR/ ${EXCLUDES[@]}
24+
25+
# zip up deploy package
26+
cd $DEPLOY_DIR
27+
zip -ruq ../lambda-deploy.zip ./

python/requirements-pre.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
numpy

python/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
GDAL==2.4.0
2-
rasterio==1.0.21
2+
rasterio==1.0.21 --no-binary rasterio

0 commit comments

Comments
 (0)