【问题标题】:I'm creating an API Gateway using terraform but the API gives {"message": "Internal server error"}我正在使用 terraform 创建 API 网关,但 API 给出 ​​{"message": "Internal server error"}
【发布时间】:2021-09-11 13:51:06
【问题描述】:

我是 Terraform 的新手,我是使用 API-Gateway、Lambda 实现无服务器架构的新手。当我使用相同的设置从控制台手动创建 API 网关并链接相同的 lambda 函数时,它可以正常工作。但是当将相同的 lambda 函数与通过 terraform 创建的 rest api 链接时,它会给出“内部服务器错误”。 我的 apiGateway.tf 文件中是否缺少某些我错过的内容。

Lambda 函数工作正常。

" apiGateway.tf"

variable "stage_name" {
  default = "example"
  type    = string
}

variable "profile" {
    default = "Dev"
}

//####  Creating REST API  ####

resource "aws_api_gateway_rest_api" "airbnbAPI" {
  name = "airbnbAPI"
  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

//####  Create Resource  ####

resource "aws_api_gateway_resource" "listing" {
  rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
  parent_id   = aws_api_gateway_rest_api.airbnbAPI.root_resource_id
  path_part   = "listing"
}

//#### Create Method GET  ####

resource "aws_api_gateway_method" "get" {
  rest_api_id       = aws_api_gateway_rest_api.airbnbAPI.id
  resource_id       = aws_api_gateway_resource.listing.id
  http_method       = "GET"
  authorization     = "NONE"
  api_key_required  = false
}

resource "aws_api_gateway_integration" "integration-get" {
  rest_api_id             = aws_api_gateway_rest_api.airbnbAPI.id
  resource_id             = aws_api_gateway_resource.listing.id
  http_method             = aws_api_gateway_method.get.http_method
  integration_http_method = "GET"
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_function.getListLambda.invoke_arn
}

//####  Method Response  ####

resource "aws_api_gateway_method_response" "response_200" {
  rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
  resource_id = aws_api_gateway_resource.listing.id
  http_method = aws_api_gateway_method.get.http_method
  status_code = "200"
  response_models = {
       "application/json" = "Empty"
   }
}

//####  Integration Response  ####

resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" {
  rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
  resource_id = aws_api_gateway_resource.listing.id
  http_method = aws_api_gateway_method.get.http_method
  status_code = aws_api_gateway_method_response.response_200.status_code
  response_templates = {
       "application/json" = ""
   } 
}

//####  Create Stage  ####

resource "aws_api_gateway_stage" "test" {
  deployment_id = aws_api_gateway_deployment.deployment1.id
  rest_api_id   = aws_api_gateway_rest_api.airbnbAPI.id
  stage_name    = var.profile
}

//######  Deploy API Gateway  ######

resource "aws_api_gateway_deployment" "deployment1" {
  rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id

  triggers = {
    redeployment = sha1(jsonencode(aws_api_gateway_rest_api.airbnbAPI.body))
  }

  depends_on = [aws_api_gateway_integration.integration-get]
  lifecycle {
    create_before_destroy = true
  }
}

//####  Creating trigger for Lambda for REST API  ####

resource "aws_lambda_permission" "apigw" {
   statement_id  = "AllowAPIGatewayInvoke"
   action        = "lambda:InvokeFunction"
   function_name = aws_lambda_function.getListLambda.function_name
   principal     = "apigateway.amazonaws.com"

   /# The "/*/*" portion grants access from any method on any resource
   /# within the API Gateway REST API.

   source_arn = "${aws_api_gateway_rest_api.airbnbAPI.execution_arn}/*/*"
}

/# Output with invoke URL

output "complete_invoke_url"   {value = "${aws_api_gateway_deployment.deployment1.invoke_url}${aws_api_gateway_stage.test.stage_name}/${aws_api_gateway_resource.listing.path_part}"}

图片和截图

Creating API Gateway manually from console working correctly

API Gateway Rest API Created via Terraform

Expected Output from RestAPI getting correctly when creating API from console manually

【问题讨论】:

    标签: amazon-web-services terraform aws-api-gateway


    【解决方案1】:

    解决了这个问题。显然 GET 函数不起作用(APIGW --> Lambda)而是必须使用 POST 函数。使用 POST 时按预期获取输出。

    找到以下链接,让我意识到这不是我 :D https://github.com/hashicorp/terraform/issues/9271

    【讨论】:

      猜你喜欢
      • 2020-12-20
      • 2021-08-08
      • 2022-09-23
      • 2018-12-29
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      相关资源
      最近更新 更多