gpt4 book ai didi

python - 为什么在使用 Terraform 部署 Lambda 函数时得到 "Error: handler and runtime must be set when PackageType is Zip"?

转载 作者:行者123 更新时间:2023-12-05 02:38:09 26 4
gpt4 key购买 nike

我已经使用 Terraform 定义了一个 Lambda 函数,如下所示:

resource "aws_lambda_function" "this" {
filename = "${path.module}/src/existing-files-lambda.zip"
function_name = "ingest-existing-files-lambda"
role = aws_iam_role.lambda.arn

runtime = "python3.9"
timeout = 900
environment {
variables = {
source_bucket_arn = var.source_bucket_arn
destination_bucket_arn = var.destination_bucket_arn
}
}
}

resource "aws_iam_role" "lambda" {
name = "${var.prefix}-lambda-ingest"
path = "/service-role/"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "lambda.amazonaws.com" }
Action = "sts:AssumeRole"
}]
})
}

我的 python 文件是这样的:

import os


def lambda_handler(event, context):
print('Hello world from Terraform')
return {
'statusCode': 200,
}

但是,我目前收到一个错误:

│ Error: handler and runtime must be set when PackageType is Zip

│ with module.ingest_lambda.aws_lambda_function.this,
│ on ingest_lambda/main.tf line 8, in resource "aws_lambda_function" "this":
│ 8: resource "aws_lambda_function" "this" {

我应该把什么作为 handler 放在这里?

我已经指定了运行时

最佳答案

您已经定义了 Lambda 函数运行时,但您没有提到函数的入口点在哪里。


这就是handler参数指定 - it is the method in your function code that processes events .

它的格式应该类似于:

def handler_name(event, context): 
...
return some_value

处理程序参数的值由以下组成,以点分隔:

  • Lambda 处理函数所在文件的名称
  • Python 处理函数的名称。

例如ingest-existing-files-lambda.lambda_handler 调用 ingest-existing-files-lambda.py 中定义的lambda_handler 函数。

如果您的 Lambda 处理程序方法称为 lambda_handler 并且位于 ingest-existing-files-lambda.py 中,这应该有效:

resource "aws_lambda_function" "this" {
filename = "${path.module}/src/existing-files-lambda.zip"
function_name = "ingest-existing-files-lambda"
handler = "ingest-existing-files-lambda.lambda_handler"
role = aws_iam_role.lambda.arn

runtime = "python3.9"
timeout = 900
environment {
variables = {
source_bucket_arn = var.source_bucket_arn
destination_bucket_arn = var.destination_bucket_arn
}
}
}

关于python - 为什么在使用 Terraform 部署 Lambda 函数时得到 "Error: handler and runtime must be set when PackageType is Zip"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69740875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com