【问题标题】:Build API Gateway on AWS using Terraform returning statusCode 200使用返回 statusCode 200 的 Terraform 在 AWS 上构建 API 网关
【发布时间】:2021-08-18 19:02:33
【问题描述】:

我是菜鸟也不知道怎么提问, 实际上我想使用应该返回 statusCode 200 的 TF 创建一个 API 网关,我尝试了官方 TF 示例来创建一个,但是 API 网关返回 500 而不是 200,我无法弄清楚错误是什么,你可以看看在我下面的代码中,它没有使用 lambda 或任何东西,我只是尝试使用模拟端点但仍然无法弄清楚

TF 代码

resource "aws_api_gateway_rest_api" "MyDemoAPI" {
  name        = "MyDemoAPI"
  description = "This is my API for demonstration purposes"
}

resource "aws_api_gateway_resource" "MyDemoResource" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  parent_id   = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id
  path_part   = "mydemoresource"
}

resource "aws_api_gateway_method" "MyDemoMethod" {
  rest_api_id   = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id   = aws_api_gateway_resource.MyDemoResource.id
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "MyDemoIntegration" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id = aws_api_gateway_resource.MyDemoResource.id
  http_method = aws_api_gateway_method.MyDemoMethod.http_method
  type        = "MOCK"
}

resource "aws_api_gateway_method_response" "response_200" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id = aws_api_gateway_resource.MyDemoResource.id
  http_method = aws_api_gateway_method.MyDemoMethod.http_method
  status_code = "200"
}

resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id = aws_api_gateway_resource.MyDemoResource.id
  http_method = aws_api_gateway_method.MyDemoMethod.http_method
  status_code = aws_api_gateway_method_response.response_200.status_code

  # Transforms the backend JSON response to XML
  response_templates = {
    "application/xml" = <<EOF
#set($inputRoot = $input.path('$'))
<?xml version="1.0" encoding="UTF-8"?>
<message>
    $inputRoot.body
</message>
EOF
  }
}

当我在 AWS 控制台中创建一个时,它工作正常,但我无法使用此 TF 代码来做到这一点

【问题讨论】:

  • 进展如何?仍然不清楚该怎么做?

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


【解决方案1】:

您尚未为您的MyDemoIntegration 指定request_templates

resource "aws_api_gateway_integration" "MyDemoIntegration" {
  rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
  resource_id = aws_api_gateway_resource.MyDemoResource.id
  http_method = aws_api_gateway_method.MyDemoMethod.http_method
  type        = "MOCK"
  
  request_templates = {
    "application/json" = <<EOF
{
   "statusCode": 200
}
EOF
  }  
}

【讨论】:

    猜你喜欢
    • 2021-02-28
    • 2017-11-24
    • 2017-11-13
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2020-09-30
    • 2017-09-07
    相关资源
    最近更新 更多