-
I've seen discussion #79 related to this, which was very helpful. This is a really great repo, so thanks for putting this together. Similar to @mirouml, I'm trying to import the GDAL python package into my Lambda function, but I can't seem to get it working. Here are the steps I've taken so far: Dockerfile (including a couple of changes different from the example provided in readme):
I then followed the instructions exactly in readme to create the zip file from the Docker container. The resulting zip file has the following structure:
I upload this zip file as a new version of my Lambda layer, and update the version of the layer used in my function: The function's environment variables are set like so: Then, I run my Lambda function with the code: import os
import sys
def lambda_handler(event, context):
# Print the Python path
print("Python path: ", sys.path)
# List the contents of /opt
print(f"Contents of /opt: {os.listdir('/opt')}")
# List the contents of /opt/share
print(f"Contents of /opt/share: {os.listdir('/opt/share')}")
# Try to import osgeo
try:
import osgeo
print("osgeo module imported successfully!")
from osgeo.gdal import gdal
print("gdal module imported successfully!")
except ImportError as e:
print("Error importing module:", e)
return {
'statusCode': 200,
'body': 'Check the logs for details.'
} The output from the function is:
I'm very new to Lambda and Docker, so any suggestions are much appreciated. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I did make it work using Docker. It was actually very helpful to learn as I had to continue to also use Docker to add ArcGIS as I needed to store some result to ArcGIS Online. My full workflow to create and get lambda layer running so I can use "from osgeo import gdal": How to build lambda layer with GDAL and python 3.11
Create Docker fileExample for python 3.11 and gdal3.8 with numpy (possibly also rasterio, fiona...): # Use Python 3.11 base image from AWS Lambda
FROM public.ecr.aws/lambda/python:3.11 AS base
# Bring C libs from lambgeo/lambda-gdal image
FROM ghcr.io/lambgeo/lambda-gdal:3.8-python3.11 AS gdal
# Create a new build stage based on the base image
FROM base AS build
COPY --from=gdal /opt/lib/ /opt/lib/
COPY --from=gdal /opt/include/ /opt/include/
COPY --from=gdal /opt/share/ /opt/share/
COPY --from=gdal /opt/bin/ /opt/bin/
ENV \
GDAL_DATA=/opt/share/gdal \
PROJ_LIB=/opt/share/proj \
GDAL_CONFIG=/opt/bin/gdal-config \
GEOS_CONFIG=/opt/bin/geos-config \
PATH=/opt/bin:$PATH
ENV PACKAGE_PREFIX=/opt/python
# Install build tools and GDAL Python bindings
RUN yum -y groupinstall "Development Tools" && \
yum -y install python3-devel && \
python -m pip install numpy && \
python -m pip install GDAL==$(gdal-config --version) -t $PACKAGE_PREFIX && \
# pip install rasterio fiona shapely geopandas - all together are too big for lambda
# python -m pip install -t $PACKAGE_PREFIX && \
yum -y groupremove "Development Tools" && \
yum -y remove python3-devel && \
yum -y clean all
# Create layer.zip
RUN cd /opt && zip -r9 /tmp/layer.zip python
# Final stage to create the final image
FROM base
COPY --from=build /tmp/layer.zip /tmp/layer.zip
For GDAL lambda layerBuilddocker build -t my-lambda-layer . Create containerdocker create --name my-lambda-layer-container my-lambda-layer Copydocker cp my-lambda-layer-container:/tmp/layer.zip . Remove containerdocker rm my-lambda-layer-container Create lambda layerIn AWS console go to Lambda - Additional Resources - Layers and Create layer |
Beta Was this translation helpful? Give feedback.
-
Thanks for sharing @mirouml, your approach is very similar. I just ended up figuring out what I was missing. The Lambda layer expects dependencies to be located in So I changed the structure of my zip file from:
to
Now, I can import osgeo and gdal without any problems. Hopefully, this discussion can help others get gdal working in their Lambda functions as well. Thanks! |
Beta Was this translation helpful? Give feedback.
Thanks for sharing @mirouml, your approach is very similar. I just ended up figuring out what I was missing. The Lambda layer expects dependencies to be located in
\python
within the zip file, according to this AWS documentation.So I changed the structure of my zip file from:
to
Now, I can import osgeo and gdal without any problems. Hopefully, this discussion can help others get gdal working in their Lambda functions as well. Thanks!