【问题标题】:BadRequestException when creating AWS APIGateway via terraform通过 terraform 创建 AWS APIGateway 时出现 BadRequestException
【发布时间】:2021-09-05 05:49:09
【问题描述】:

我正在尝试使用 terraform 创建 rest API,但是我遇到了无法解决的错误。

Error: Error creating API Gateway Integration Response: BadRequestException: Invalid mapping expression specified: Validation Result: warnings : [], errors : [No method response exists for method.]
│ 
│   with aws_api_gateway_integration_response.create_report_integration_options_response,
│   on test_api_post.tf line 119, in resource "aws_api_gateway_integration_response" "create_report_integration_options_response":
│  119: resource "aws_api_gateway_integration_response" "create_report_integration_options_response" {

下面是terraform配置

provider "aws" {
  region = "us-east-2"
}


terraform {
  required_version = "> 0.14.0"
  required_providers {
    aws = "~> 3.0"
  }
}

resource "aws_api_gateway_rest_api" "first_api" {
  name = "test-api"
  tags = {
    By = "Terraform"
  }
}

resource "aws_api_gateway_resource" "create_report" {
  parent_id   = aws_api_gateway_rest_api.first_api.root_resource_id
  path_part   = "create_report"
  rest_api_id = aws_api_gateway_rest_api.first_api.id
}

resource "aws_api_gateway_method" "create_report_method" {

  rest_api_id   = aws_api_gateway_rest_api.first_api.id
  http_method   = "POST"
  resource_id   = aws_api_gateway_resource.create_report.id
  authorization = "NONE"
}


resource "aws_api_gateway_integration" "create_report_integration" {
  http_method = aws_api_gateway_method.create_report_method.http_method
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  type        = "MOCK"
  request_templates = {
    "application/json" = jsonencode({
      statusCode = 200
    })
  }
}

resource "aws_api_gateway_integration_response" "create_report_integration_response" {
  http_method = "POST"
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  status_code = 200
  response_templates = {
    "application/json" = jsonencode({

      "message_str" : "report requested, check your phone shortly"

    })
  }

  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
    "method.response.header.Access-Control-Allow-Methods" : "'POST'"
    "method.response.header.Access-Control-Allow-Origin" : "'*'",
  }
  depends_on = [aws_api_gateway_integration.create_report_integration]
}

resource "aws_api_gateway_method_response" "create_report_method_response" {
  http_method = "POST"
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  status_code = 200
  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" : false,
    "method.response.header.Access-Control-Allow-Origin" : false,
    "method.response.header.Access-Control-Allow-Methods" : false
  }
  depends_on = [aws_api_gateway_method.create_report_method, aws_api_gateway_integration.create_report_integration]
}


// options method
resource "aws_api_gateway_method" "create_report_options_method" {
  http_method   = "OPTIONS"
  resource_id   = aws_api_gateway_resource.create_report.id
  rest_api_id   = aws_api_gateway_rest_api.first_api.id
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "create_report_options_integration" {
  http_method = aws_api_gateway_method.create_report_options_method.http_method
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  type        = "MOCK"
  request_templates = {
    "application/json" = jsonencode({
      statusCode = 200
    })
  }
}

resource "aws_api_gateway_method_response" "create_report_method_options_response" {
  http_method = "OPTIONS"
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  status_code = 200
  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" : true,
    "method.response.header.Access-Control-Allow-Origin" : true,
    "method.response.header.Access-Control-Allow-Methods" : true,
    "method.response.header.Access-Control-Allow-Credentials" : true
  }
  response_models = {
    "application/json" = "Empty"
  }

  depends_on = [aws_api_gateway_method.create_report_options_method, aws_api_gateway_integration.create_report_options_integration]
}

resource "aws_api_gateway_integration_response" "create_report_integration_options_response" {
  http_method = "OPTIONS"
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  status_code = 200
  response_templates = {

  }
  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
    "method.response.header.Access-Control-Allow-Methods" : "'POST,OPTIONS'",
    "method.response.header.Access-Control-Allow-Origin" : "'*'",
    "method.response.header.Access-Control-Allow-Credentials" : "'true'"
  }
  depends_on = [aws_api_gateway_method.create_report_options_method, aws_api_gateway_integration.create_report_options_integration]
}

有人可以帮我找出我做错了什么,因为已经 2 天了,但我仍然无法弄清楚到底是什么问题。谢谢

【问题讨论】:

    标签: terraform aws-api-gateway


    【解决方案1】:

    发生此错误是因为 terraform 在开始尝试创建相关 aws_api_gateway_integration_responses 之前尚未完成创建 aws_api_gateway_method_response 资源。

    要解决这个问题:

    • aws_api_gateway_method_response.create_report_method_response 添加到 aws_api_gateway_integration_response.create_report_integration_response 的depends on 列表中
    • aws_api_gateway_method_response.create_report_method_options_response 添加到 aws_api_gateway_integration_response.create_report_integration_options_response 的depends on 列表中。

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 2021-03-16
      • 1970-01-01
      • 2017-07-27
      • 2020-07-01
      • 2020-08-05
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多