skip to Main Content

I’m having issues running basically any lambda function on AWS as the Lambda Function tool would not be able to import the module.

I tried to import the packages as layers – pretty wrong I think. (https://www.linkedin.com/pulse/add-external-python-libraries-aws-lambda-using-layers-gabe-olokun/)

Then I’ve tried to import the packages as environments (from local or bash scripted):

local – like this boy https://www.youtube.com/watch?v=NGteAkN2WYc

or using bash scripting (AWS Cloudshell) – https://docs.aws.amazon.com/lambda/latest/dg/python-package.html

Either ways:
enter image description here

The lambda environment would look like this:
enter image description here

import psycopg2



def lambda_handler(event, context):
    # Connect to PostgreSQL database
    conn = psycopg2.connect(
        host=['host'],
        database=['postgres'],
        user=['user'],
        password=['password']
    )

And I’m hitting the following errors:

-if the lambda_function.py is inside the "psycopg2+py" directory: errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'

-if the lambda_function.py is outside the "psycopg2+py" directory and just inside the "postgresql" directory: errorMessage": "Unable to import module 'lambda_function': No module named 'psycopg2'

And I suposse the Handler is set correctly :
enter image description here

I must also mention, when I set up the environment to install the packages, I was using Python 3.9, the same version that I’m using on Lambda function.
Also, I’ve tried the same methods with another package, like fastapi, still not working, so it seems to be a functionality issue not a package issue.

I don’t have any idea what else should I try.

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution, just use Python 3.8 and use the packages as layers. Here's a link from my git with 2 layers working, psycopg2 and pandas (boto3/os are prebuilt in AWS): https://github.com/alexdragut20/AWS Just create the layers using "Upload a zip file" option, create a function from scratch and add the layers upon the function.


  2. Here is how you can go about adding dependencies for a lambda function:

    1. Install required python packages (in this case psycopg2):

      # mkdir workspace; cd workspace

      # pip3.9 install pip --upgrade

      # pip3.9 install --platform manylinux2014_x86_64
      --target=./python/lib/python3.9/site-packages --implementation cp
      --python 3.9 --only-binary=:all: --upgrade psycopg2

    2. Rename _psycopg.xxxxxx.so file:

      mv ./python/lib/python3.9/site-packages/psycopg2/_psycopg*.so ./python/lib/python3.9/site-packages/psycopg2/_psycopg.so

    3. Zip the created python directory:

      # zip -r requirements.zip python/

    4. Create a lambda layer from the requirements.zip zip file (you can also use the portal instead of awscli):

      # aws lambda publish-layer-version --layer-name dependencies
      --description "Python packages" --license-info "MIT"
      --zip-file fileb://requirements.zip --compatible-runtimes python3.9
      --compatible-architectures "x86_64"

    5. Add the published layer version to your lambda function (or use the portal):

      # aws lambda update-function-configuration --function-name Your_Lambda_Func_Name --layers arn:aws:lambda:us-east-1:xxxxxxxx:layer:dependencies:1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search