首页 > 解决方案 > API gateway - message "select an integration response." when creating stack using cloudformation

问题描述

This is what I am expecting to see in API Gateway after creating the stack.

enter image description here

But this is what's actually happen.
In the method response, it shows message "select an integration response.", but I did add the model in the method response, and "HTTP status: Proxy" should be shown
What's going on?

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

标签: amazon-web-servicesamazon-cloudformationaws-api-gateway

解决方案


Just add this inside Integration :

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

This below block

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

is set for method response level. You are looking at lambda means integration response level. For that you have to set IntegrationResponses.

Full template :

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

推荐阅读