Skip to content

Commit 4f5c29a

Browse files
committed
Add initial example
1 parent 157e616 commit 4f5c29a

8 files changed

Lines changed: 152 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.idea/

docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '3'
2+
3+
services:
4+
localstack:
5+
image: localstack/localstack
6+
container_name: localstack
7+
ports:
8+
- "4567-4599:4567-4599"
9+
environment:
10+
- SERVICES=serverless
11+
- LAMBDA_EXECUTOR=docker
12+
- LAMBDA_REMOVE_CONTAINERS=false
13+
- DOCKER_HOST=unix:///var/run/docker.sock
14+
volumes:
15+
- "${TMPDIR}:/tmp/localstack"
16+
- "/var/run/docker.sock:/var/run/docker.sock"

lambda/build.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
echo "Build Layer and extract zip of dependencies"
4+
docker run --name cogeo -w /var/task --volume $PWD/:/local -itd remotepixel/amazonlinux:gdal3.0-py3.7-cogeo bash
5+
docker exec -it cogeo bash '/local/create-layer.sh'
6+
docker cp cogeo:/tmp/package.zip package.zip
7+
docker stop cogeo
8+
docker rm cogeo
9+
10+
echo "Unzip Layer and move all items up one directory"
11+
mkdir deps \
12+
&& cd deps \
13+
&& unzip -qq ../package.zip \
14+
&& mv bin/* . && rm -rf bin \
15+
&& mv lib/* . && rm -rf lib \
16+
&& mv python/* . && rm -rf python \
17+
&& mv share/* . && rm -rf share \
18+
&& rm ../package.zip
19+
20+
echo "Copy in Lambda code and zip up new 'uber' Lambda"
21+
cp ../lambda.py . \
22+
&& zip -r9q lambda.zip . \
23+
&& mv lambda.zip ../lambda.zip \
24+
&& cd .. \
25+
&& rm -rf deps

lambda/create-layer.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
echo "-----------------------"
3+
echo "Creating lambda layer"
4+
echo "-----------------------"
5+
6+
echo "Remove lambda python packages"
7+
rm -rdf $PREFIX/python/boto3* \
8+
&& rm -rdf $PREFIX/python/botocore* \
9+
&& rm -rdf $PREFIX/python/docutils* \
10+
&& rm -rdf $PREFIX/python/dateutil* \
11+
&& rm -rdf $PREFIX/python/jmespath* \
12+
&& rm -rdf $PREFIX/python/s3transfer* \
13+
&& rm -rdf $PREFIX/python/numpy/doc/
14+
15+
echo "Remove useless files"
16+
rm -rdf $PREFIX/share/doc \
17+
&& rm -rdf $PREFIX/share/man \
18+
&& rm -rdf $PREFIX/share/hdf*
19+
20+
echo "Remove uncompiled python scripts"
21+
find $PREFIX/python -type f -name '*.pyc' | while read f; do n=$(echo $f | sed 's/__pycache__\///' | sed 's/.cpython-[2-3][0-9]//'); cp $f $n; done;
22+
find $PREFIX/python -type d -a -name '__pycache__' -print0 | xargs -0 rm -rf
23+
find $PREFIX/python -type f -a -name '*.py' -print0 | xargs -0 rm -f
24+
25+
echo "Strip shared libraries"
26+
cd $PREFIX && find lib -name \*.so\* -exec strip {} \;
27+
28+
echo "Create archives"
29+
cd $PREFIX && zip -r9q /tmp/package.zip python
30+
cd $PREFIX && zip -r9q --symlinks /tmp/package.zip lib/*.so* share
31+
cd $PREFIX && zip -r9q --symlinks /tmp/package.zip bin/gdal* bin/ogr* bin/geos* bin/nearblack

lambda/lambda.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy
2+
import pygeos
3+
import rasterio
4+
import requests
5+
import rio_cogeo
6+
import rio_color
7+
import rio_tiler
8+
import rio_tiler_mosaic
9+
import rio_tiler_mvt
10+
import shapely
11+
import supermercado
12+
13+
14+
def handler(event, context):
15+
print(numpy.__version__)
16+
print(pygeos.__version__)
17+
print(rasterio.__version__)
18+
print(requests.__version__)
19+
print(rio_cogeo.version)
20+
print(rio_color.__version__)
21+
print(rio_tiler.version)
22+
print(rio_tiler_mosaic.version)
23+
print(rio_tiler_mvt.__version__)
24+
print(shapely.__version__)
25+
print(supermercado.__package__)
26+
return {"message": "Printed all versions"}

lambda/test-lambda.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
bash build.sh
4+
pytest -s .

lambda/test_lambda.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import json
2+
from unittest import TestCase
3+
4+
import boto3
5+
6+
7+
def get_lambda_client():
8+
return boto3.client('lambda',
9+
aws_access_key_id='',
10+
aws_secret_access_key='',
11+
region_name='eu-west-2',
12+
endpoint_url='http://localhost:4574')
13+
14+
15+
def deploy_lambda():
16+
with open('lambda.zip', 'rb') as f:
17+
zipped_code = f.read()
18+
get_lambda_client().create_function(
19+
FunctionName='geo-lambda',
20+
Runtime='python3.7',
21+
Role='a-role',
22+
Handler='lambda.handler',
23+
Code={'ZipFile': zipped_code},
24+
Environment={
25+
'Variables': {
26+
'GDAL_DATA': '/opt/share/gdal',
27+
'PROJ_LIB': '/opt/share/proj'
28+
}
29+
}
30+
)
31+
32+
33+
def invoke_lambda_and_get_response():
34+
return json.loads(get_lambda_client().invoke(
35+
FunctionName='geo-lambda',
36+
InvocationType='RequestResponse'
37+
)['Payload'].read().decode('utf-8'))
38+
39+
40+
class Test(TestCase):
41+
def setup_method(self, method):
42+
deploy_lambda()
43+
44+
def test_that_layer_works(self):
45+
resp = invoke_lambda_and_get_response()
46+
self.assertEqual(resp["message"], "Printed all versions")

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
boto3==1.12.0
2+
pytest==5.3.5

0 commit comments

Comments
 (0)