gpt4 book ai didi

amazon-web-services - API 网关和 Lambda 部署 : Invalid permissions on Lambda function

转载 作者:行者123 更新时间:2023-12-05 06:27:30 25 4
gpt4 key购买 nike

我正在尝试使用 Terraform 部署 AWS API 网关和 Lambda。网关只是我的功能的代理。运行 terraform apply 后,对端点的每个 Web 请求都会失败。

API 网关将此错误消息记录到 Cloudwatch:

Execution failed due to configuration error: Invalid permissions on Lambda function

但现在它变得很奇怪:如果我在 AWS Web 控制台中点击部署按钮并再次部署(没有其他任何改变)API 网关,一切正常。下一个“terraform apply”再次部署 aws_api_gateway_stage.staging,这再次破坏了一切。

这是我的 Terraform 设置:

lambda.tf:

resource "aws_lambda_function" "contactform-api" {
filename = "deploy.zip"
function_name = "contactform-api"
handler = "main"
runtime = "go1.x"
role = "${aws_iam_role.lambda_role.arn}"
publish = "false"
timeout = "60"
}

resource "aws_iam_role" "lambda_role" {
name = "iam_for_lambda"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": "IAMRoleForLambda"
}
]
}
EOF
}

resource "aws_lambda_permission" "apigw" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.contactform-api.arn}"
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.contactform.execution_arn}/*/*/*"
}

api_gateway.tf:

resource "aws_api_gateway_rest_api" "contactform" {
name = "ContactformAPI"
description = "Contactform REST API"
}

resource "aws_api_gateway_resource" "api" {
rest_api_id = "${aws_api_gateway_rest_api.contactform.id}"
parent_id = "${aws_api_gateway_rest_api.contactform.root_resource_id}"
path_part = "{proxy+}"
}

resource "aws_api_gateway_method" "api" {
rest_api_id = "${aws_api_gateway_rest_api.contactform.id}"
resource_id = "${aws_api_gateway_resource.api.id}"
http_method = "ANY"
authorization = "NONE"
}

resource "aws_api_gateway_integration" "lambda" {
rest_api_id = "${aws_api_gateway_rest_api.contactform.id}"
resource_id = "${aws_api_gateway_resource.api.id}"
http_method = "${aws_api_gateway_method.api.http_method}"
integration_http_method = "POST"
type = "AWS_PROXY"
uri = "${aws_lambda_function.contactform-api.invoke_arn}"
}

resource "aws_api_gateway_deployment" "staging" {
depends_on = ["module.cors", "aws_api_gateway_method.api", "aws_api_gateway_resource.api", "aws_api_gateway_integration.lambda"]
stage_name = "staging"
rest_api_id = "${aws_api_gateway_rest_api.contactform.id}"
}

resource "aws_api_gateway_stage" "staging" {
stage_name = "staging"
rest_api_id = "${aws_api_gateway_rest_api.contactform.id}"
deployment_id = "${aws_api_gateway_deployment.staging.id}"
}

最佳答案

您正在使用 Lambda 函数的 ARN 作为 aws_lambda_permission 资源中的 function_name

将其更改为名称而不是 ARN 应该可以为您解决该问题:

resource "aws_lambda_permission" "apigw" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.contactform-api.function_name}"
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.contactform.execution_arn}/*/*/*"
}

至于为什么它在您通过 AWS 控制台部署时有效; AWS 控制台神奇地为您设置了权限,因此您无需通过 Web UI 进行处理。当 Terraform 重新部署更改时,它会再次破坏它,因为 Terraform 需要明确声明 Lambda 函数的权限。

关于amazon-web-services - API 网关和 Lambda 部署 : Invalid permissions on Lambda function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55229403/

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