Git Product home page Git Product logo

geo-layer's Introduction

GDAL based AWS Lambda Layers

geo-layer

AWS lambda (Amazonlinux) Layers and Docker images.

Test

Python package

numpy

pygeos==0.8
shapely==1.7.1
rasterio==1.1.8

pyproj==2.4.1  # Only for GDAL>=3.1

mercantile==1.1.6
supermercado==0.2.0

# HACK, those package are in the docker image but not in the lambda env
requests
pyyaml

Available layers

gdal size (Mb) unzipped size (Mb) arn
3.2 43.8 138.8 arn:aws:lambda:{REGION}:524387336408:layer:gdal32-python37-geo:{VERSION}
3.1 43.7 128.4 arn:aws:lambda:{REGION}:524387336408:layer:gdal31-python37-geo:{VERSION}
2.4 36.3 121.3 arn:aws:lambda:{REGION}:524387336408:layer:gdal24-python37-geo:{VERSION}
--- --- --- ---
3.2 44.4 140 arn:aws:lambda:{REGION}:524387336408:layer:gdal32-python38-geo:{VERSION}
3.1 44.3 139.7 arn:aws:lambda:{REGION}:524387336408:layer:gdal31-python38-geo:{VERSION}
2.4 36.7 130 arn:aws:lambda:{REGION}:524387336408:layer:gdal24-python38-geo:{VERSION}

See full list of ARN

Arns format

  • arn:aws:lambda:${region}:524387336408:layer:gdal(24|31|32)-python(37|38)-geo:${version}

Regions

  • ap-northeast-1
  • ap-northeast-2
  • ap-south-1
  • ap-southeast-1
  • ap-southeast-2
  • ca-central-1
  • eu-central-1
  • eu-north-1
  • eu-west-1
  • eu-west-2
  • eu-west-3
  • sa-east-1
  • us-east-1
  • us-east-2
  • us-west-1
  • us-west-2

Layer content

layer.zip
  |
  |___ bin/      # Binaries
  |___ lib/      # Shared libraries (GDAL, PROJ, GEOS...)
  |___ share/    # GDAL/PROJ data directories
  |___ python/   # Python modules

The layer content will be unzip in /opt directory in AWS Lambda. For the python libs to be able to use the C libraries you have to make sure to set 2 important environment variables:

  • GDAL_DATA: /opt/share/gdal
  • PROJ_LIB: /opt/share/proj

How To

There are 2 ways to use the layers:

1. Simple (No dependencies)

If you don't need to add more python module (dependencies), you can just create a lambda package (zip file) with you lambda handler.

zip -r9q package.zip handler.py

Content:

package.zip
  |___ handler.py   # aws lambda python handler

AWS Lambda Config:

  • arn: arn:aws:lambda:us-east-1:524387336408:layer:gdal32-python38-geo:1 (example)
  • env:
    • GDAL_DATA: /opt/share/gdal
    • PROJ_LIB: /opt/share/proj
  • lambda handler: handler.handler

2. Advanced (need other python dependencies)

If your lambda handler needs more dependencies you'll have to use the exact same environment. To ease this you can find the docker images for each lambda on docker hub.

  • Create a docker file
FROM lambgeo/lambda-gdal:3.2-python3.8-geo

ENV PYTHONUSERBASE=/var/task

# Install dependencies
COPY handler.py $PYTHONUSERBASE/handler.py

# Here we use the `--user` option to make sure to not replicate modules.
RUN pip install rio-tiler --user

# Move some files around
RUN mv ${PYTHONUSERBASE}/lib/python3.8/site-packages/* ${PYTHONUSERBASE}/
RUN rm -rf ${PYTHONUSERBASE}/lib

echo "Create archive"
RUN cd $PYTHONUSERBASE && zip -r9q /tmp/package.zip *
  • create package
docker build --tag package:latest .
docker run --name lambda -w /var/task -itd package:latest bash
docker cp lambda:/tmp/package.zip package.zip
docker stop lambda
docker rm lambda

Content:

package.zip
  |___ handler.py   # aws lambda python handler
  |___ module1/     # dependencies
  |___ module2/
  |___ module3/
  |___ ...

AWS Lambda Config:

  • arn: arn:aws:lambda:us-east-1:524387336408:layer:gdal32-python38-geo:1 (example)
  • env:
    • GDAL_DATA: /opt/share/gdal
    • PROJ_LIB: /opt/share/proj
  • lambda handler: handler.handler

Refactor

We recently refactored the repo, to see old documentation please refer to https://github.com/lambgeo/geo-layer/tree/60c9cf69a4529e14d4394a0a3e78dd5f84d9e6ec

geo-layer's People

Contributors

vincentsarago avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

geo-layer's Issues

Using binaries cannot find libexpat.so.1

Hi

I am trying to use the geo-layer with a NodeJS 10.x AWS Lambda function and calling ogr2ogr using child_process.exec however I get the following error

ogr2ogr: error while loading shared libraries: libexpat.so.1: cannot open shared object file: No such file or directory

Looking at the docker image it does seem to be built with expat so maybe I am not correctly setting the environment variables or maybe I need to set the LD_LIBRARY_PATH?

I have used the same method from a Python 3.7 AWS Lambda function using subprocess and it worked fine.

Any ideas?

Many thanks

rename "geo" to "geolayer"

ref #5
Because I originally had -geo layer in my aws account, the version count was different throught the region and layers.

To keep everything uniq, I'm going to release the layers and docker images with a new geolayer suffix

The new ARN will be
arn:aws:lambda:{REGION}:524387336408:layer:gdal{GDAL_VERSION_NODOT}-py{PYTHON_VERSION_NODOT}-geolayer:{LAYER_VERSION}

and the docker name
lambgeo/lambda:gdal{GDAL_VERSION_SHORT}-py{PYTHON_VERSION_SHORT}-geolayer

Update!

I've just pushed a lot of fix in docker-lambda so we need to publish new layer here too!

add a note about jinja2

Jinja2 is present in lambci image but not on AWS Lambda

quick fix:

pip install jinja2 --user --ignore-installed

libexpat.so.1: cannot open shared object file: No such file or director

Hi.
This error has appearead again in this new layer. And I can reproduce it, finally.

I've cloned this repo and created a makefile with the commands described in the README. However, when trying to run a simple python function, it gives the error from the title.

Makefile:

.PHONY: layer test

layer:
	docker build --tag package:latest . --build-arg GDAL_VERSION=3.0 --build-arg PYTHON_VERSION=3.8
	docker run --name lambda -w /var/task -itd package:latest bash
	docker cp scripts/create-lambda-layer.sh lambda:/create-lambda-layer.sh
	docker exec -it lambda bash /create-lambda-layer.sh
	docker cp lambda:/tmp/package.zip package.zip
	docker stop lambda
	docker rm lambda

test:
	unzip package.zip -d layer
	docker run \
		--rm \
		-v $(shell pwd):/var/task:ro,delegated \
		-v $(shell pwd)/layer:/opt:ro,delegated \
    	-e GDAL_DATA="/opt/share/gdal" \
    	-e PROJ_LIB="/opt/share/proj" \
		lambci/lambda:python3.8 handler.handler '{"some": "data"}'

Python function:

import numpy
import rasterio

def handler(event, context):
    print(numpy.__version__)
    print(rasterio.__version__)

I've already checked the linking using ldd and it seems to be linked correctly. But when executing the function, it does not work.

$ docker run --rm -it lambgeo/lambda:gdal3.0-py3.8 bash -c "ldd /opt/lib/libgdal.so | grep expat"
	libexpat.so.1 => /lib64/libexpat.so.1 (0x00007fb514a28000)

Am I missing something?

Again, thanks for your work with these layers. They help A LOT. And sorry for pestering you with my issues ๐Ÿ˜

Make layers public

I do not know if it is your plan to make these layers publicly available. I hope it is. They are very good! But, I was trying to use the new ones today and I am getting access denied (see below).

$ aws lambda get-layer-version-by-arn --arn "arn:aws:lambda:eu-central-1:524387336408:layer:gdal31-py38-geolayer:1" --region "eu-central-1"

An error occurred (AccessDeniedException) when calling the GetLayerVersionByArn operation: User: arn:aws:iam::1234567890:user/user is not authorized to perform: lambda:GetLayerVersion on resource: arn:aws:lambda:eu-central-1:524387336408:layer:gdal31-py38-geolayer:1

Version are not 1

๐Ÿคฆ I though I cleanup all old -geo layer in my account but it might not be true

    {
        "region": "us-east-1",
        "layers": [
            {
                "name": "gdal24-py37-geo",
                "arn": "arn:aws:lambda:us-east-1:524387336408:layer:gdal24-py37-geo:9",
                "version": 9
            },
            {
                "name": "gdal30-py37-geo",
                "arn": "arn:aws:lambda:us-east-1:524387336408:layer:gdal30-py37-geo:8",
                "version": 8
            },
            {
                "name": "gdal31-py37-geo",
                "arn": "arn:aws:lambda:us-east-1:524387336408:layer:gdal31-py37-geo:1",
                "version": 1
            },
            {
                "name": "gdal24-py38-geo",
                "arn": "arn:aws:lambda:us-east-1:524387336408:layer:gdal24-py38-geo:1",
                "version": 1
            },
            {
                "name": "gdal30-py38-geo",
                "arn": "arn:aws:lambda:us-east-1:524387336408:layer:gdal30-py38-geo:1",
                "version": 1
            },
            {
                "name": "gdal31-py38-geo",
                "arn": "arn:aws:lambda:us-east-1:524387336408:layer:gdal31-py38-geo:1",
                "version": 1
            }
        ]
    },

cc @kylebarron

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.