简体   繁体   English

psycopg2 文件夹压缩到 python venv lambda function

[英]psycopg2 folder zipped into python venv lambda function

I am trying to add the packages for psycopg2 into my zipped folder for my python lambda function.我正在尝试将 psycopg2 的包添加到我的 python lambda function 的压缩文件夹中。 I am using Python version 3.8.我正在使用 Python 版本 3.8。 Here is the psycopg2 documentation psycopg2 .这是 psycopg2 文档psycopg2

From these docs it seems like I have to pull the folder for psycopg2-3.8 into the zipped file for my lambda function.从这些文档中,似乎我必须将 psycopg2-3.8 的文件夹拉到我的 lambda function 的压缩文件中。 However, I cannot seem to get this to successfully run.但是,我似乎无法让它成功运行。

I am using a virtual environment and creating the zip file according to these AWS docs for virtual environment我正在使用虚拟环境并根据虚拟环境的这些 AWS 文档创建 zip 文件

To get the psycopg folder zipped I am running zip -g my-deployment-package.zip psycopg2要压缩 psycopg 文件夹,我正在运行zip -g my-deployment-package.zip psycopg2

The error I am receiving is我收到的错误是

{
  "errorMessage": "Unable to import module 'test': No module named 'psycopg2._psycopg'",
  "errorType": "Runtime.ImportModuleError"
}

Any help troubleshooting this would be great.任何解决此问题的帮助都会很棒。

psycopg2-binary is a binary pip package of psycopg2 which comes with all dependencies and does not require compiling. psycopg2-binarypsycopg2的二进制 pip package,它带有所有依赖项,不需要编译。 To use it in your lambda function, you can create a lambda layer using docker as described in the AWS blog .要在您的 lambda function 中使用它,您可以使用lambda层创建一个lambda 层,如AWS 博客中所描述的 FCB6053C41A21306AFD 中所述。

Thus you can add psycopg2-binary to your function as follows:因此,您可以将psycopg2-binary添加到 function 中,如下所示:

  1. Create empty folder, eg mylayer .创建空文件夹,例如mylayer

  2. Go to the folder and create requirements.txt file with the content of Go 到文件夹并创建requirements.txt文件,内容为

psycopg2-binary
  1. Run the following docker command:运行以下 docker 命令:

The command will create layer for python3.8:该命令将为 python3.8 创建层:

docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
  1. Archive the layer as zip:将层归档为 zip:
zip -9 -r mylayer.zip python 
  1. Create lambda layer based on mylayer.zip in the AWS Console.在 AWS 控制台中基于mylayer.zip创建 lambda 层。 Don't forget to specify Compatible runtime to python3.8 .不要忘记将Compatible runtime指定为python3.8

  2. Add the the layer created in step 5 to your function.将步骤 5 中创建的层添加到您的 function。

  3. I tested the layer using your code:我使用您的代码测试了该层:

import psycopg2

def lambda_handler(event, context):
    
    print(dir(psycopg2))
    
    return "ok"

It works correctly :它工作正常

['BINARY', 'Binary', 'DATETIME', 'DataError', 'DatabaseError', 'Date', 'DateFromTicks', 'Error', 'IntegrityError', 'InterfaceError', 'InternalError', 'NUMBER', 'NotSupportedError', 'OperationalError', 'ProgrammingError', 'ROWID', 'STRING', 'Time', 'TimeFromTicks', 'Timestamp', 'TimestampFromTicks', 'Warning', '__builtins__', '__cached__', '__doc__', '__file__', '__libpq_version__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_connect', '_ext', '_json', '_psycopg', '_range', 'apilevel', 'compat', 'connect', 'errors', 'extensions', 'paramstyle', 'threadsafety', 'tz']

ps ps

The steps were execution on linux.这些步骤是在 linux 上执行的。 If you don't have one you can create a linux ec2 instance and setup docker there if you are not sure how to modify the commands for Windows or Mac.如果您没有一个,您可以创建一个 linux ec2 实例并在那里设置 docker 如果您不确定如何修改 Windows 或 Mac 的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM