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

Viewed 710

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

2 Answers

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

For those looking for the quick hack workaround to get it working from the console (like me).

I found the answer here: 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.

Related