gpt4 book ai didi

terraform - 通过 terraform 创建 AWS APIGateway 时出现 BadRequestException

转载 作者:行者123 更新时间:2023-12-05 04:48:29 24 4
gpt4 key购买 nike

我正在尝试使用 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_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 的依赖列表
  • aws_api_gateway_method_response.create_report_method_options_response 添加到 aws_api_gateway_integration_response.create_report_integration_options_response 的取决于列表。

关于terraform - 通过 terraform 创建 AWS APIGateway 时出现 BadRequestException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68069910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com