serverless-python-requirements icon indicating copy to clipboard operation
serverless-python-requirements copied to clipboard

mysqlclient and LD_LIBRARY_PATH with lambda layers

Open eldos-dl opened this issue 3 years ago • 2 comments

The steps mentioned in the documentation for mysqlclient are not working as expected and is raising the following error

ImportError: libmysqlclient.so.1020: cannot open shared object file: No such file or directory

Dockerfile contents:

FROM lambci/lambda:build-python3.7
RUN yum -y install mysql57-devel

serverless-python-requriements config

pythonRequirements:
    useDownloadCache: false
    useStaticCache: false
    dockerizePip: true
    dockerFile: Dockerfile
    dockerExtraFiles:
      - /usr/lib64/mysql57/libmysqlclient.so.1020
    layer:
      name: ${self:service}-${self:provider.stage}-requirements-python37
      compatibleRuntimes:
        - python3.7

I have observed that even though, the layer has the files _mysql.cpython-37m-x86_64-linux-gnu.so, libmysqlclient.so.1020, at /opt/python, we are still getting this error because the default LD_LIBRARY_PATH in Lambda Python 3.7 Runtime doesn't consider /opt/python, it only checks in the following paths.

/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib

I feel if there is provision to specify the destination path for the files mentioned in dockerExtraFiles, this issue can be resolved.

eldos-dl avatar Aug 20 '21 16:08 eldos-dl

I am running into the same issue. I worked around it by setting the LD_LIBRARY_PATH env variable

service: hello-lambda-vpc
frameworkVersion: '3'

plugins:
  - serverless-python-requirements

package:
  individually: false
  patterns:
    - "!*"
    - "!*/**"
    - main.py

custom:
  pythonRequirements:
    useStaticCache: false
    dockerizePip: true
    dockerFile: Dockerfile.build
    dockerExtraFiles:
      - /usr/lib/oracle/21/client64/lib/libociicus.so
      - /usr/lib/oracle/21/client64/lib/libnnz21.so
      - /usr/lib/oracle/21/client64/lib/libclntshcore.so.21.1
      - /usr/lib/oracle/21/client64/lib/libclntsh.so.21.1
      - /lib64/libaio.so.1
    layer: true
    noDeploy:
      - invoke
      - uvicorn

provider:
  name: aws
  runtime: python3.7
  stage: ${opt:stage, 'stage'}

functions:
  app:
    handler: main.lambda_handler
    environment:
      LD_LIBRARY_PATH: /var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/python
    layers: 
      - { Ref: PythonRequirementsLambdaLayer }
    events:
      - httpApi: "ANY /{proxy+}"

jgriffith-hbs avatar Dec 20 '22 09:12 jgriffith-hbs

@jgriffith you saved my day! Thank you

cristiansandru11 avatar Apr 04 '23 11:04 cristiansandru11