AWS Lambda 无法导入模块没有名为“psycopg2”的模块

问题描述 投票:0回答:1

我正在尝试在我的 lambda 中导入 psycopg2,但出现此错误:

[ERROR] Runtime.ImportModuleError: Unable to import module 'expire_invites': No module named 'psycopg2'

我已经从 [jkehler/awslambda-psycopg2] (https://github.com/jkehler/awslambda-psycopg2) 下载了 python3.6 的捆绑包并复制到我的 lambda 目录。

Lambda directory screenshot

我正在使用 terraform 为 psycopg2 创建 lambda 及其层:

数据.tf

data "archive_file" "psycopg2_layer" {
  type        = "zip"
  source_dir  = "${path.module}/../../scripts/python/expire_invites/psycopg2"
  output_path = "${path.module}/../../scripts/python/expire_invites/psycopg2_layer.zip"
}

main.tf

resource "aws_lambda_layer_version" "pg_dependencies_layer" {
  layer_name          = "pg-dependencies-layer"
  filename            = data.archive_file.psycopg2_layer.output_path
  source_code_hash    = data.archive_file.psycopg2_layer.output_base64sha256
  compatible_runtimes = ["python3.6"]
}

resource "aws_lambda_function" "expire_invites" {
  filename      = "${path.module}/../../scripts/python/expire-invites.zip"
  function_name = "expire_invites-${terraform.workspace}"
  role          = aws_iam_role.lambda.arn
  layers        = [aws_lambda_layer_version.pg_dependencies_layer.arn]
  handler       = "expire_invites.lambda_handler"
  runtime       = "python3.6"

  environment {
    variables = {
      ...database variables
    }
  }

  vpc_config {
    ...vpc config
  }
}

lambda 已创建并且工作正常,除了 psycopg2 导入...

我尝试了几种不同的方法,但没有成功,我不知道这是不是我做错了。

  • 尝试将python3.7用于psycopg2
  • 尝试使用requirements.txt文件并使用null_resource安装
python amazon-web-services aws-lambda terraform psycopg2
1个回答
0
投票

通过将以下键添加到我的 lambda 定义中来解决:

source_code_hash  = data.archive_file.expire_invites.output_base64sha256

我的代码没有更新,包也没有上传到AWS。

© www.soinside.com 2019 - 2024. All rights reserved.