0

I wanted to test a JDBC connection using Python lambda in the AWS console. I am using py4j. I added this as a layer for the lambda. I have the following code.

from py4j.java_gateway import *

gg = launch_gateway()

When I run this in AWS console, getting error:

"could not find py4j jar" for the line gg = launch_gateway().

Where do I add py4j.jar in AWS?

This is a Python lambda function.

How do I fix this issue?

1 Answer 1

0

I encountered the same error when following the documentation here: https://docs.aws.amazon.com/lambda/latest/dg/python-layers.html#python-layer-packaging. To ensure py4j<<version>>.jar was included in my layer I had to modify the 2 shell scripts as follows.

1-install.sh becomes:

python3.11 -m venv create_layer
source create_layer/bin/activate
pip install -r requirements.txt --prefix=vendor
pip install -r requirements.txt

This ensures pip includes dependencies such as the missing py4j<<version>>.jar in an additional directory vendor/share.

2-package.sh becomes:

mkdir python
cp -r create_layer/lib python/
cp -r vendor/share python/lib/python3.11/site-packages/
zip -r layer_content.zip python

This ensures those dependencies are included in the zipfile, which can then be used to create a layer as described in the docs.

Sign up to request clarification or add additional context in comments.

1 Comment

Note: It appears aws only included openjdk version "1.8.0_201" up to python 3.7. From 3.8 and upwards it appears java is unavailable in the python runtime environments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.