You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

请求指导:在Python 3.6版本的AWS Lambda中安装psycopg2包以连接RDS PostgreSQL数据库

Fixing "No module named 'psycopg2'" in AWS Lambda (Python 3.6)

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:

  1. Spin up an Amazon Linux 2 container (matches Lambda's underlying OS for Python 3.6):
    docker run -it --rm amazonlinux:2
    
  2. Install Python 3.6 and pip inside the container:
    yum install -y python36 python36-pip
    
  3. Install psycopg2-binary to a local directory:
    pip3.6 install psycopg2-binary -t ./package
    
  4. Add your code files to the package directory:
    • Copy postgres_test.py and db_util.py into ./package
  5. Create a deployment zip:
    cd package && zip -r lambda-deploy.zip .
    
  6. 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.

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:

  1. Follow steps 1-3 from Option 1 to install psycopg2-binary in the Docker container.
  2. Create the required layer directory structure:
    mkdir -p python/lib/python3.6/site-packages
    
  3. Copy psycopg2 files to the layer structure:
    cp -r /usr/local/lib/python3.6/site-packages/psycopg2* python/lib/python3.6/site-packages/
    
  4. Package the layer into a zip:
    zip -r psycopg2-layer.zip python/
    
  5. 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.
  6. 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.py uses Python 2-style print statements (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.) in db_util.py with 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

火山引擎 最新活动