我正在尝试在我的 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 目录。
我正在使用 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 导入...
我尝试了几种不同的方法,但没有成功,我不知道这是不是我做错了。
通过将以下键添加到我的 lambda 定义中来解决:
source_code_hash = data.archive_file.expire_invites.output_base64sha256
我的代码没有更新,包也没有上传到AWS。