[Python] How to import any lib from zip lambda layer despite nesting libs properly

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    I did try the 2 solutions - nesting properly my Python libs inside zip folder:

    layer_content.zip
    └ python
    └ lib
    └ python3.11
    └ site-packages
    └ requests
    └ <other_dependencies> (i.e. dependencies of the requests package)
    └ ...


    and following another tip that Amazon just changed its requirement and all libs should go directly into python folder. But both options don't work.

    my_layer.zip:
    - python
    - requests
    - psycopg2
    - snowflake-connector

    my_layer_nested.zip:
    └ python
    └ lib
    └ python3.11
    └ site-packages
    └ requests
    └ snowflake-connector



    Neither work, it does not see the requests module:

    import json
    import requests



    def lambda_handler(event, context):
    url = "https://api.github.com"
    response = requests.get(url)

    return {
    'statusCode': 200,
    'body': json.dumps({
    'message': 'Layer test successful!',
    'data': response.json()
    })
    }



    I tried using both separate zip folders as layers, here is the output:

    Status: Failed
    Test Event Name: kljlj

    Response:
    {
    "errorMessage": "Unable to import module 'lambda_function': No module named 'requests'",
    "errorType": "Runtime.ImportModuleError",
    "requestId": "",
    "stackTrace": []
    }

    Continue reading...

Compartilhe esta Página