Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'google.generativeai'

I'm trying to use the google-generativeai
package in my AWS Lambda function (Python 3.9 runtime). I want to package the dependency as a Lambda Layer, but I'm running into issues.
I followed these steps:
# install google-generativeai package
pip install \
--platform manylinux2014_x86_64 \
--only-binary=:all: \
-t python/
google-generativeai
# .zip file includes the 'python' directory at the root level, Lambda can locate and import your packages
zip -r lambda-layer.zip python/
# publish the dependency as a lambda layer
aws lambda publish-layer-version \
--layer-name my-lambda-layer \
--zip-file fileb://lambda-layer.zip \
--compatible-runtimes python3.13
# Add the layer to your Lambda function
aws lambda update-function-configuration \
--function-name my-lambda-function-name \
--layers arn:aws:lambda:your-region:your-account-id:layer:my-lambda-layer:1
References:
[1] https://docs.aws.amazon.com/lambda/latest/dg/python-layers.html
[2] https://repost.aws/knowledge-center/lambda-import-module-error-python
Answer
This issue is unique because standard fixes for Python package conflicts don’t work when dealing with Google’s shared google
namespace, particularly with google-generativeai
and protobuf
. The core problem is that both packages install modules under the same google/
directory, so installing them separately or with pip -t
can cause files to overwrite each other, resulting in missing or broken imports
The solution is to use a virtual environment to isolate and install all dependencies together, then copy the entire site-packages
directory into your Lambda Layer. This preserves the correct namespace structure and avoids conflicts and dependencies like protobuf
and google-generativeai
coexist without overwriting each other.
Fix#1 (using pip):
python3 -m venv venv
source venv/bin/activate
pip install \
--no-cache-dir \
protobuf google-generativeai
mkdir -p python/lib/python3.9/site-packages
cp -r venv/lib/python3.9/site-packages/* python/lib/python3.9/site-packages/
cd python
zip -r lambda-layer.zip python/*
aws lambda publish-layer-version \
--layer-name lambda-layer \
--zip-file fileb://lambda-layer.zip \
--compatible-runtimes python3.9
# Add the layer to your Lambda function
aws lambda update-function-configuration \
--function-name my-lambda-function-name \
--layers arn:aws:lambda:your-region:your-account-id:layer:my-lambda-layer:2
Fix#2 (using container):
# Variables (replace as needed)
AWS_REGION="ap-southeast-2"
ACCOUNT_ID="123456789012"
REPO_NAME="my-lambda-repo"
IMAGE_TAG="latest"
FUNCTION_NAME="my-lambda-function"
LAMBDA_ROLE_ARN="arn:aws:iam::123456789012:role/lambda-execution-role"
PYTHON_VERSION="3.9"
# 1. Create requirements.txt
cat <<EOF > requirements.txt
google-generativeai
protobuf
EOF
# 2. Create Dockerfile
cat <<EOF > Dockerfile
FROM public.ecr.aws/lambda/python:${PYTHON_VERSION}
WORKDIR \${LAMBDA_TASK_ROOT}
# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# Copy your function code
COPY lambda_function.py .
# Command to run Lambda function
CMD ["lambda_function.lambda_handler"]
EOF
# 3. (Optional) Create a sample lambda_function.py if not present
if [ ! -f lambda_function.py ]; then
cat <<EOF > lambda_function.py
def lambda_handler(event, context):
return {"status": "success", "message": "Hello from Lambda container!"}
EOF
fi
# 4. Build the Docker image
docker build -t ${REPO_NAME}:${IMAGE_TAG} .
# 5. Authenticate Docker to ECR
aws ecr get-login-password --region ${AWS_REGION} | \
docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
# 6. Create ECR repository if it doesn't exist
aws ecr describe-repositories --repository-names ${REPO_NAME} --region ${AWS_REGION} \
|| aws ecr create-repository --repository-name ${REPO_NAME} --region ${AWS_REGION}
# 7. Tag and push the image to ECR
docker tag ${REPO_NAME}:${IMAGE_TAG} ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:${IMAGE_TAG}
docker push ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:${IMAGE_TAG}
# 8. update the Lambda function
aws lambda update-function-code \
--function-name ${FUNCTION_NAME} \
--image-uri ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${REPO_NAME}:${IMAGE_TAG} \
--region ${AWS_REGION}
echo "Deployment complete: Lambda function '${FUNCTION_NAME}' is now running the new container image."