Lambda Web API 为 POST 以外的动词返回 403

Lambda Web API returning 403 for verbs other than POST

我使用内置模板创建了一个无服务器 Lambda Web API。 Web api 使用 CloudFormation 部署到 AWS,一切正常(即所有 GET、POST 等)。

然后我检查了 CloudFormation 创建的资源并使用 Terraform 创建了所有资源。我仔细检查了 API 网关中的 REST API,Lambda,Lambda 权限和触发器在从无服务器模板和 Terraform 脚本创建时完全相同。

问题是在使用从 Terraform 创建的资源时,我无法使用 POST 以外的 HTTP 谓词调用 Web API。下面是我的 API 网关的地形脚本。

resource "aws_api_gateway_rest_api" "SimulationServer_api_gateway_rest" {
  name        = "SimulationServer"
  description = "Terraform Serverless Application Example"
}

resource "aws_api_gateway_resource" "api_gw_proxy_resource" {
  rest_api_id = aws_api_gateway_rest_api.SimulationServer_api_gateway_rest.id
  parent_id   = aws_api_gateway_rest_api.SimulationServer_api_gateway_rest.root_resource_id
  path_part   = "{proxy+}"
}

resource "aws_api_gateway_method" "api_gw_proxy_method" {
  rest_api_id   = aws_api_gateway_rest_api.SimulationServer_api_gateway_rest.id
  resource_id   = aws_api_gateway_resource.api_gw_proxy_resource.id
  http_method   = "ANY"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "api_gw_proxy_integration" {
  rest_api_id = aws_api_gateway_rest_api.SimulationServer_api_gateway_rest.id
  resource_id = aws_api_gateway_method.api_gw_proxy_method.resource_id
  http_method = aws_api_gateway_method.api_gw_proxy_method.http_method
  integration_http_method = "ANY"
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_function.SimulationServer.invoke_arn
}

resource "aws_api_gateway_method" "api_gw_proxy_root_method" {
  rest_api_id   = aws_api_gateway_rest_api.SimulationServer_api_gateway_rest.id
  resource_id   = 
aws_api_gateway_rest_api.SimulationServer_api_gateway_rest.root_resource_id
  http_method   = "ANY"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "api_gw_proxy_root_integration" {
  rest_api_id = aws_api_gateway_rest_api.SimulationServer_api_gateway_rest.id
  resource_id = aws_api_gateway_method.api_gw_proxy_root_method.resource_id
  http_method = aws_api_gateway_method.api_gw_proxy_root_method.http_method
  integration_http_method = "ANY"
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_function.SimulationServer.invoke_arn
}

resource "aws_api_gateway_deployment" "api_gw_deployment" {
  depends_on = [
    aws_api_gateway_integration.api_gw_proxy_integration,
    aws_api_gateway_integration.api_gw_proxy_root_integration,
  ]
  rest_api_id = aws_api_gateway_rest_api.SimulationServer_api_gateway_rest.id
  stage_name  = var.env
}

output "base_url" {
  value = aws_api_gateway_deployment.api_gw_deployment.invoke_url
}

事实证明这是一个非常奇怪但简单的解决方案。 而不是使用,

integration_http_method = "ANY"

我用的时候,

integration_http_method = "POST"

然后它开始适用于所有 HTTP 动词(GET、POST、PUT 等)