Lambda Web API 对于除 POST 之外的动词返回 403

发布于 2025-01-11 01:22:00 字数 2621 浏览 0 评论 0原文

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

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

问题是,当使用从 Terraform 创建的资源时,我无法使用 POST 之外的 HTTP 动词调用 Web API。下面是我的 API 网关 terraform 脚本。

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
}

I have created a Serverless Lambda Web API using a built-in template. The web api is deployed to AWS using CloudFormation and everything is working fine (i.e. All GET, POST etc).

I have then checked the resources created by CloudFormation and created all of the resources using Terraform. I have double checked and REST API in the API Gateway, Lambda, Lambda permissions and triggers are exactly the same when created from both serverless template and terraform scripts.

The issue is the when using the resources created from Terraform, I am not able to call the Web API using HTTP verb other then POST. Below is my terraform script for the API Gateway.

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2025-01-18 01:22:00

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

integration_http_method = "ANY"

当我使用时,

integration_http_method = "POST"

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

It turned out to be very strange but simple solution.
Instead of using,

integration_http_method = "ANY"

When I used,

integration_http_method = "POST"

Then it started working for all HTTP Verbs (GET, POST, PUT etc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文