【问题标题】:How can I install psycopg2 (Python Postgres driver) on AWS Lambda?如何在 AWS Lambda 上安装 psycopg2(Python Postgres 驱动程序)?
【发布时间】:2017-08-03 03:08:43
【问题描述】:

这应该有效吗?当我尝试使用 psycopg2 运行代码时,似乎什么也没发生。

我遵循以下说明:

http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

在一个 Amazon Linux 实例上,我创建了一个 Python 2.7 virtualenv

然后我执行“pip install --upgrade pip”

然后我执行 pip install psycopg2 - 我现在看到了这些文件:

(venv27)[ec2-user@ip-172-30-0-194 applyreplyPythonTest]$ ls /home/ec2-user/venv27/lib64/python2.7/site-packages/psycopg2
errorcodes.py   extensions.py   extras.py   __init__.py   _ipaddress.py   _json.py   pool.py   psycopg1.py   _psycopg.so  _range.pyc  sql.pyc  tz.py
errorcodes.pyc  extensions.pyc  extras.pyc  __init__.pyc  _ipaddress.pyc  _json.pyc  pool.pyc  psycopg1.pyc  _range.py    sql.py      tests    tz.pyc
(venv27)[ec2-user@ip-172-30-0-194 applyreplyPythonTest]$

我将 psycopg2 复制到我的 Lambda 代码目录的根目录,其中有一个 lambda_function.py

#!/usr/bin/python
from __future__ import print_function
import psycopg2
import sys
import pprint
import json
import urllib
import boto3


def getdata():

    conn_string = "host='some address' dbname='DBNAME' user='XXXXXXX' password='XXXXXXX'"
    # print the connection string we will use to connect
    print("Connecting to database\n ->%s" % (conn_string))

    # get a connection, if a connect cannot be made an exception will be raised here
    print('floob')
    conn = psycopg2.connect(conn_string)
    print('conn.status', conn.status)
    print('conn.server_version', conn.server_version)

    # conn.cursor will return a cursor object, you can use this cursor to perform queries
    cursor = conn.cursor()

    # execute our Query
    cursor.execute("SELECT * FROM cognitouser")

    # retrieve the records from the database

    results = []
    for row in cursor.fetchall():
        print(row)
        #results.append(row)

    # print out the records using pretty print
    # note that the NAMES of the columns are not shown, instead just indexes.
    # for most people this isn't very useful so we'll show you how to return
    # columns as a dictionary (hash) in the next example.
    #pprint.pprint(records)




def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))
    getdata()
    return json.dumps(event)

然后我通过 S3 将我的函数作为 zipfile 上传到 AWS。

它确实运行,但是在打印“floob”后,Lambda Cloudwatch 日志上没有输出。

我已独立检查数据库服务器是否可访问。

谁能建议我做错了什么?

谢谢

【问题讨论】:

  • 需要静态链接共享库。请参阅this answer

标签: postgresql python-2.7 amazon-web-services aws-lambda psycopg2


【解决方案1】:

我在包含 psycopg2 的 lambda 函数中添加了一个 lamdba 层。以下是可用 Lambda 层的列表:https://github.com/jetbridge/psycopg2-lambda-layer

我正在使用无服务器框架,这就是我的 Lambda 函数的样子:

functions:
  example:
    handler: handler.example
    layers:
      - arn:aws:lambda:us-east-1:898466741470:layer:psycopg2-py37:3
    events:
      - http:
          path: example
          method: post
          authorizer: aws_iam
          cors: true

【讨论】:

    【解决方案2】:

    3 年后,python 2.7 在 AWS Lambda 上被弃用。

    对于 Python 3.x,您可能需要一些步骤来添加此依赖项。

    您可以查看 [1] 以获取有关执行 psycopg2 的非标准构建的更多信息。

    这些是我使用 [2] 中的 Amazon Linux 1 AMI 在 EC2 实例上构建层 zip 的 bash 命令:

    $ sudo yum update
    $ sudo yum install python36
    $ curl -O https://bootstrap.pypa.io/get-pip.py
    $ python3 get-pip.py --user
    $ sudo yum groupinstall "Development Tools"
    $ sudo yum install python36-devel
    $ sudo yum install postgresql-devel
    $ mkdir -p package/python/lib/python3.6/site-packages/
    $ pip3 install psycopg2 -t package/python/lib/python3.6/site-packages/
    $ mkdir package/lib
    $ sudo find / -name libpq.so*
    $ cp /usr/lib64/libpq.so package/lib
    $ cp /usr/lib64/libpq.so.5 package/lib
    $ cp /usr/lib64/libpq.so.5.5 package/lib
    $ cd package
    $ zip -r psycopg2.zip *
    

    然后,您可以使用在 Lambda 控制台上创建层时创建的 zip 文件。

    [1]http://initd.org/psycopg/docs/install.html

    [2]https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      相关资源
      最近更新 更多