请求指导:在Python 3.6版本的AWS Lambda中安装psycopg2包以连接RDS PostgreSQL数据库
Ah, that classic Lambda + psycopg2 headache! The error you're seeing happens because the standard psycopg2 package isn't built to work with Lambda's Amazon Linux environment—especially for older Python versions like 3.6. Let's walk through two reliable solutions to get your PostgreSQL connection working.
Option 1: Package a Lambda-compatible psycopg2 with your code
Lambda requires packages built for its specific runtime environment. The easiest way to get a compatible psycopg2 is to use Docker to simulate Lambda's Amazon Linux environment and build the package there:
- Spin up an Amazon Linux 2 container (matches Lambda's underlying OS for Python 3.6):
docker run -it --rm amazonlinux:2 - Install Python 3.6 and pip inside the container:
yum install -y python36 python36-pip - Install psycopg2-binary to a local directory:
pip3.6 install psycopg2-binary -t ./package - Add your code files to the
packagedirectory:- Copy
postgres_test.pyanddb_util.pyinto./package
- Copy
- Create a deployment zip:
cd package && zip -r lambda-deploy.zip . - Upload the zip to Lambda:
- In the Lambda console, go to your function's "Code" tab, select "Upload from" > ".zip file", and upload
lambda-deploy.zip.
- In the Lambda console, go to your function's "Code" tab, select "Upload from" > ".zip file", and upload
Option 2: Use a Lambda Layer (reusable for multiple functions)
Layers let you share dependencies across Lambda functions without re-packaging them every time. Here's how to create a psycopg2 layer:
- Follow steps 1-3 from Option 1 to install psycopg2-binary in the Docker container.
- Create the required layer directory structure:
mkdir -p python/lib/python3.6/site-packages - Copy psycopg2 files to the layer structure:
cp -r /usr/local/lib/python3.6/site-packages/psycopg2* python/lib/python3.6/site-packages/ - Package the layer into a zip:
zip -r psycopg2-layer.zip python/ - Upload the layer to AWS:
- Go to the Lambda console's "Layers" section, click "Create layer", name it, upload
psycopg2-layer.zip, and select Python 3.6 as the compatible runtime.
- Go to the Lambda console's "Layers" section, click "Create layer", name it, upload
- Attach the layer to your function:
- In your Lambda function's "Layers" tab, click "Add a layer", select your new psycopg2 layer, and save.
Critical Additional Fixes
Before testing, make sure to fix these issues in your code (they'll cause errors even after resolving psycopg2):
- Python 3 print syntax: Your
db_util.pyuses Python 2-styleprintstatements (no parentheses). Update them to Python 3:# Replace this: print "I am unable to connect to the database" # With this: print("I am unable to connect to the database") # And this: print "Now executing: %s" % (query) # With this: print("Now executing: %s" % (query)) - Database credentials: Replace the placeholder values (
db_host,db_user, etc.) indb_util.pywith your actual RDS PostgreSQL details. - IAM permissions: Ensure your Lambda function's IAM role has permissions to connect to your RDS instance (e.g., network access via security groups that allow Lambda's VPC traffic to reach RDS).
Bonus: Consider Upgrading Python
Python 3.6 reached end-of-life in 2021, and newer Python versions (3.9+) have better support for modern psycopg2 versions and receive security updates. If possible, migrate your Lambda function to a supported Python runtime—it'll save you headaches down the line.
内容的提问来源于stack exchange,提问作者Javier Gallego




