I’m working on a Django project where we use AWS Lambda to do some geocoding. In order to use the functionality we need from the GeoDjango contrib app, we needed to install the geospatial library GDAL.

Initially I tried compiling GDAL from source on a EC2 instance with Amazon Linux that matches the execution environment of the Lambda runtime1, but I found that the .so shared library files where too large (10+ MB) to add to our Git repository.

I figured I could create a custom Docker image for AWS CodeBuild and inject the shared library files into our Lambda deployment package on the build server. However, since we’re using a standard CodeBuild image that’s provided by AWS, I didn’t want to create and maintain a custom build image just to add some files to a Lambda function.

After some searching I found a much better way to do this with Geolambda:

Geolambda greatly simplifies the process to develop and deploy code that uses standard geospatial libraries. It takes the guesswork out of bundling native binaries with your AWS Lambda functions, so you can focus on building.

Essentially it’s a Docker image that contains all the common geospatial native libraries, including GDAL. You can use the publicly available Lambda Layers to add to your Lambda function, or clone the repo and run the bin/package.sh script to deploy your own Lambda Layer.

Since we’re using the Serverless Framework to deploy the application to Lambda, adding the layer was as simple as adding two lines to serverless.yml:

functions:
  my_geo_function:
    handler: handler.my_geo_function
    layers:
      - arn:aws:lambda:eu-west-1:xxxxxxxxxxxx:layer:geolambda:1

The last step is to tell Django where the GDAL library can be found. Add this to your settings.py:

GDAL_LIBRARY_PATH = '/opt/lib/libgdal.so'

This is a much more elegant and time saving solution than to compile the libraries myself and somehow add them to the Lambda function. Thanks to Development Seed for providing Geolambda.

  1. I started with this tutorial but didn’t like the many steps involved.