API网关-消息“选择集成响应”使用cloudformation创建堆栈时

问题描述

这是我期望在创建堆栈后在API Gateway中看到的内容

enter image description here

但这是实际发生的情况。
方法响应中,它显示消息“选择积分响应”,但是 我确实在方法响应中添加了模型,并且应该显示“ HTTP状态:代理”
发生了什么事?

enter image description here

enter image description here

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"},"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"
                ]
              ]
            }
          }
        }
      }
    }
  }