|
| 1 | +# GDAL based docker-lambda |
| 2 | + |
| 3 | +[](https://circleci.com/gh/lambgeo/docker-lambda) |
| 4 | + |
| 5 | +Create an **AWS lambda** like docker images and lambda layer with GDAL. |
| 6 | + |
| 7 | + |
| 8 | +## Images |
| 9 | +### GDAL - Based on lambci/lambda-base:build |
| 10 | + - 3.1.0 (April. 2020) - **lambgeo/lambda:gdal3.1** - Pre-release |
| 11 | + - 3.0.4 (April. 2020) - **lambgeo/lambda:gdal3.0** |
| 12 | + - 2.4.4 (April. 2020) - **lambgeo/lambda:gdal2.4** |
| 13 | + |
| 14 | +## Lambda Layers |
| 15 | + |
| 16 | +We are publishing each gdal version as lambda layer on the AWS infrastructure. |
| 17 | +Each layer are available for all runtimes. |
| 18 | + |
| 19 | +**gdal${version}** |
| 20 | + |
| 21 | +arn: **arn:aws:lambda:{REGION}:524387336408:layer:gdal${version}** |
| 22 | + |
| 23 | +[Full list of version and ARN](https://github.com/RemotePixel/amazonlinux/blob/master/arns.json) |
| 24 | + |
| 25 | +#### versions: |
| 26 | + |
| 27 | +gdal | version | size (Mb)| unzipped size (Mb) |
| 28 | + ---| ---| ---| --- |
| 29 | +3.1 | 1| 46.4| 136.9 |
| 30 | +3.0 | 1| 46.4| 136.9 |
| 31 | +2.4 | 1| 37.7| 126.2 |
| 32 | + |
| 33 | +#### Regions |
| 34 | +- ap-northeast-1 |
| 35 | +- ap-northeast-2 |
| 36 | +- ap-south-1 |
| 37 | +- ap-southeast-1 |
| 38 | +- ap-southeast-2 |
| 39 | +- ca-central-1 |
| 40 | +- eu-central-1 |
| 41 | +- eu-north-1 |
| 42 | +- eu-west-1 |
| 43 | +- eu-west-2 |
| 44 | +- eu-west-3 |
| 45 | +- sa-east-1 |
| 46 | +- us-east-1 |
| 47 | +- us-east-2 |
| 48 | +- us-west-1 |
| 49 | +- us-west-2 |
| 50 | + |
| 51 | +### Python - Based on lambci/lambda:build-python* |
| 52 | + |
| 53 | +Those images are here to help for the creation of lambda package or lambda layer. |
| 54 | + |
| 55 | +- **3.1** |
| 56 | + - **lambgeo/lambda:gdal3.1-py3.7** |
| 57 | + - **lambgeo/lambda:gdal3.1-py3.8** |
| 58 | + |
| 59 | +- **3.0** |
| 60 | + - **lambgeo/lambda:gdal3.0-py3.7** |
| 61 | + - **lambgeo/lambda:gdal3.0-py3.8** |
| 62 | + |
| 63 | +- **2.4** |
| 64 | + - **lambgeo/lambda:gdal2.4-py3.7** |
| 65 | + - **lambgeo/lambda:gdal2.4-py3.8** |
| 66 | + |
| 67 | +Content: GDAL Libs and python with numpy and cython |
| 68 | + |
| 69 | +Checkout [/base/python/Dockerfile](/base/python/Dockerfile) to see how to create other runtime supported images. |
| 70 | + |
| 71 | +# Create a Python Lambda package |
| 72 | + |
| 73 | +You can use the docker container to either build a full package (you provide all the libraries) |
| 74 | +or adapt for the use of AWS Lambda layer. |
| 75 | + |
| 76 | +## 1. Create full package (see [/examples/package](/examples/package)) |
| 77 | +This is like we used to do before (with remotepixel/amazonlinux-gdal images) |
| 78 | + |
| 79 | +- dockerfile |
| 80 | +```Dockerfile |
| 81 | +FROM lambgeo/lambda:gdal3.0-py3.7 |
| 82 | + |
| 83 | +ENV PACKAGE_PREFIX=/var/task |
| 84 | + |
| 85 | +COPY handler.py ${PACKAGE_PREFIX}/handler.py |
| 86 | +RUN pip install numpy rasterio mercantile --no-binary :all: -t ${PACKAGE_PREFIX}/ |
| 87 | +``` |
| 88 | + |
| 89 | +- package.sh |
| 90 | +```bash |
| 91 | +#!/bin/bash |
| 92 | +echo "-----------------------" |
| 93 | +echo "Creating lambda package" |
| 94 | +echo "-----------------------" |
| 95 | +echo "Remove lambda python packages" |
| 96 | +rm -rdf $PACKAGE_PREFIX/boto3/ \ |
| 97 | + && rm -rdf $PACKAGE_PREFIX/botocore/ \ |
| 98 | + && rm -rdf $PACKAGE_PREFIX/docutils/ \ |
| 99 | + && rm -rdf $PACKAGE_PREFIX/dateutil/ \ |
| 100 | + && rm -rdf $PACKAGE_PREFIX/jmespath/ \ |
| 101 | + && rm -rdf $PACKAGE_PREFIX/s3transfer/ \ |
| 102 | + && rm -rdf $PACKAGE_PREFIX/numpy/doc/ |
| 103 | + |
| 104 | +echo "Strip shared libraries" |
| 105 | +cd $PREFIX && find lib -name \*.so\* -exec strip {} \; |
| 106 | + |
| 107 | +echo "Create archive" |
| 108 | +cd $PACKAGE_PREFIX && zip -r9q /tmp/package.zip * |
| 109 | +cd $PREFIX && zip -r9q --symlinks /tmp/package.zip lib/*.so* share bin |
| 110 | +cp /tmp/package.zip /local/package.zip |
| 111 | +``` |
| 112 | + |
| 113 | +- commands |
| 114 | +```bash |
| 115 | +docker build --tag package:latest . |
| 116 | +docker run --name lambda -w /var/task --volume $(shell pwd)/:/local -itd package:latest bash |
| 117 | +docker exec -it lambda bash '/local/package.sh' |
| 118 | +docker stop lambda |
| 119 | +docker rm lambda |
| 120 | +``` |
| 121 | + |
| 122 | +## 2. Use Lambda Layer (see [/examples/layer](/examples/layer)) |
| 123 | + |
| 124 | +- dockerfile |
| 125 | + |
| 126 | +Here we install rasterio and we add our handler method. |
| 127 | +The final package structure should be |
| 128 | + |
| 129 | +``` |
| 130 | +package/ |
| 131 | + |___ handler.py |
| 132 | + |___ rasterio/ |
| 133 | +``` |
| 134 | + |
| 135 | +```Dockerfile |
| 136 | +FROM lambgeo/lambda:gdal3.0-py3.7 |
| 137 | + |
| 138 | +# Basically we don't want to replicated existant modules found in the layer ($PYTHONPATH) |
| 139 | +# So we use the $PYTHONUSERBASE trick to set the output directory |
| 140 | +ENV PYTHONUSERBASE=/var/task |
| 141 | + |
| 142 | +# Create a package |
| 143 | +COPY handler.py $PYTHONUSERBASE/handler.py |
| 144 | +RUN pip install --user rasterio --no-binary rasterio |
| 145 | +``` |
| 146 | +- layer.sh |
| 147 | +```bash |
| 148 | +# We move all the package to the root directory |
| 149 | +version=$(python -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")') |
| 150 | +PYPATH=${PYTHONUSERBASE}/lib/python${version}/site-packages/ |
| 151 | +mv ${PYPATH}/* ${PYTHONUSERBASE}/ |
| 152 | +rm -rf ${PYTHONUSERBASE}/lib |
| 153 | + |
| 154 | +echo "Create archive" |
| 155 | +cd $PYTHONUSERBASE && zip -r9q /tmp/layer.zip * |
| 156 | + |
| 157 | +cp /tmp/layer.zip /local/layer.zip |
| 158 | +``` |
| 159 | +- commands |
| 160 | +```bash |
| 161 | +docker build --tag package:latest . |
| 162 | +docker run --name lambda -w /var/task --volume $(shell pwd)/:/local -itd package:latest bash |
| 163 | +docker exec -it lambda bash '/local/layer.sh' |
| 164 | +docker stop lambda |
| 165 | +docker rm lambda |
| 166 | + |
| 167 | +``` |
| 168 | + |
| 169 | +# AWS Lambda Layer architecture |
| 170 | + |
| 171 | +The AWS Layer created within this repository have this architecture: |
| 172 | + |
| 173 | +``` |
| 174 | +layer.zip |
| 175 | + | |
| 176 | + |___ bin/ # Binaries |
| 177 | + |___ lib/ # Shared libraries (GDAL, PROJ, GEOS...) |
| 178 | + |___ share/ # GDAL/PROJ data directories |
| 179 | +``` |
| 180 | + |
| 181 | +You may want to extent this layer by adding runtime specific code |
| 182 | + |
| 183 | +``` |
| 184 | +layer.zip |
| 185 | + | |
| 186 | + ... |
| 187 | + |___ python/ # Runtime |
| 188 | + |__ rasterio/ |
| 189 | + |__ rio_tiler/ |
| 190 | + |__ handler.py |
| 191 | +``` |
| 192 | + |
| 193 | +# AWS Lambda config |
| 194 | +- When using lambgeo gdal layer |
| 195 | + |
| 196 | + - **GDAL_DATA:** /opt/share/gdal |
| 197 | + - **PROJ_LIB:** /opt/share/proj |
| 198 | + |
| 199 | +- When creating full package |
| 200 | + - **GDAL_DATA:** /var/task/share/gdal |
| 201 | + - **PROJ_LIB:** /var/task/share/proj |
| 202 | + |
| 203 | +### Other variable for optimal config |
| 204 | +- **GDAL_CACHEMAX:** 512 |
| 205 | +- **VSI_CACHE:** TRUE |
| 206 | +- **VSI_CACHE_SIZE:** 536870912 |
| 207 | +- **CPL_TMPDIR:** "/tmp" |
| 208 | +- **GDAL_HTTP_MERGE_CONSECUTIVE_RANGES:** YES |
| 209 | +- **GDAL_HTTP_MULTIPLEX:** YES |
| 210 | +- **GDAL_HTTP_VERSION:** 2 |
| 211 | +- **GDAL_DISABLE_READDIR_ON_OPEN:** "EMPTY_DIR" |
| 212 | +- **CPL_VSIL_CURL_ALLOWED_EXTENSIONS:** ".tif" |
0 commit comments