API 网关 - 使用 cloudformation 创建堆栈时的消息 "select an integration response."

API gateway - message "select an integration response." when creating stack using cloudformation

这是我希望在创建堆栈后在 API 网关中看到的内容。

但这就是实际发生的事情。
在方法响应中,它显示消息“select 集成响应。”,但是 我确实在方法响应中添加了模型,应该显示“HTTP 状态:代理”
怎么回事?

resources.json

{
    "AWSTemplateFormatVersion": "2010-09-09",
    
    "Resources": {
      "HelloWorldApi": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
          "Name": "hello-api",
          "Description": "API used for practice",
          "FailOnWarnings": true
        }
      },
      "getBannerMethod": {
        "Type": "AWS::ApiGateway::Method",
        "DependsOn": ["HelloWorldApi"],
        "Properties": {
          "RestApiId": {
            "Ref": "HelloWorldApi"
          },
          "ResourceId": {
            "Ref": "BannerResource"
          },
          "HttpMethod": "GET",
          "MethodResponses":[
            {
              "ResponseModels" : {"application/json" : "Empty"},
              "ResponseParameters":{
                "method.response.header.Access-Control-Allow-Origin": "'*'"
              },
              "StatusCode" : "200"
            },
            {
              "StatusCode": "500"
            }
          ],
          "AuthorizationType": "NONE",
          "Integration": {
            "Credentials": {
              "Fn::ImportValue": {
                "Fn::Sub": "${RolesStack}-ApiGatewayRoleArn"
              }
            },
            "IntegrationHttpMethod": "POST",
            "Type": "AWS_PROXY",
            "Uri": {
              "Fn::Join": ["",
                [
                  "arn:aws:apigateway:",
                  {
                    "Ref": "AWS::Region"
                  },
                  ":lambda:path/2015-03-31/functions/",
                  {
                    "Fn::GetAtt": ["getBannerHandler", "Arn"]
                  },
                  "/invocations"
                ]
              ]
            }
          }
        }
      }
    }
  }

只需在 Integration 中添加:

"IntegrationResponses": [{ 
 "ResponseParameters":{
                "method.response.header.Access-Control-Allow-Origin": "'*'"
              },  
  "StatusCode" : "200"
}]

下面这个区块

"MethodResponses":[
            {
              "ResponseModels" : {"application/json" : "Empty"},
              "ResponseParameters":{
                "method.response.header.Access-Control-Allow-Origin": "'*'"
              },
              "StatusCode" : "200"
            },
            {
              "StatusCode": "500"
            }
          ],

设置为方法响应级别。您正在查看 lambda 表示集成响应级别。为此你必须设置 IntegrationResponses.

完整模板:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    
    "Resources": {
      "HelloWorldApi": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
          "Name": "hello-api",
          "Description": "API used for practice",
          "FailOnWarnings": true
        }
      },
      "getBannerMethod": {
        "Type": "AWS::ApiGateway::Method",
        "DependsOn": ["HelloWorldApi"],
        "Properties": {
          "RestApiId": {
            "Ref": "HelloWorldApi"
          },
          "ResourceId": {
            "Ref": "BannerResource"
          },
          "HttpMethod": "GET",
          "MethodResponses":[
            {
              "ResponseModels" : {"application/json" : "Empty"},
              "ResponseParameters":{
                "method.response.header.Access-Control-Allow-Origin": "'*'"
              },
              "StatusCode" : "200"
            },
            {
              "StatusCode": "500"
            }
          ],
          "AuthorizationType": "NONE",
          "Integration": {
            "Credentials": {
              "Fn::ImportValue": {
                "Fn::Sub": "${RolesStack}-ApiGatewayRoleArn"
              }
            },
            "IntegrationHttpMethod": "POST",
            "IntegrationResponses": [{ 
               "ResponseParameters":{
                "method.response.header.Access-Control-Allow-Origin": "'*'"
                },  
              "StatusCode" : "200"
            }],
            "Type": "AWS_PROXY",
            "Uri": {
              "Fn::Join": ["",
                [
                  "arn:aws:apigateway:",
                  {
                    "Ref": "AWS::Region"
                  },
                  ":lambda:path/2015-03-31/functions/",
                  {
                    "Fn::GetAtt": ["getBannerHandler", "Arn"]
                  },
                  "/invocations"
                ]
              ]
            }
          }
        }
      }
    }
  }

对于那些正在寻找快速解决方法以使其从控制台运行的人(比如我)。

我在这里找到了答案:https://github.com/hashicorp/terraform-provider-aws/issues/11561

The only way to fix this issue is to login to the AWS Console and the do the following:

Go to "Integration request" and then uncheck "Use Lambda Proxy integration" and then check it again.

After performing the above steps the Method response correctly shows the mapped model.