首页 > 解决方案 > AWS Cloudformation APIGateway 在尝试设置静态标头值时遇到不受支持的属性 IntegrationResponses

问题描述

我正在尝试使用 Cloudformation 在 AWS APIGateway 的方法执行上设置我的标头映射。

这是我想要的最终状态: 在此处输入图像描述

这是我尝试使用的 Cloudformation JSON 模板片段:

   "AlertDetailMock": {
  "Type": "AWS::ApiGateway::Method",
  "Properties": {
    "RestApiId": {
      "Ref": "RestApi"
    },
    "ResourceId": {
      "Ref": "AlertDetailResource"
    },
    "HttpMethod": "OPTIONS",
    "AuthorizationType": "NONE",
    "Integration": {
      "Type": "MOCK",
      "RequestTemplates": {
        "application/json": "{\"statusCode\": 200}"
      }
    },
    "IntegrationResponses": [
      {
        "ResponseTemplates": {
          "application/json": ""
        },
        "ResponseParameters": {
          "method.response.header.Access-Control-Allow-Origin": "\\'*\\'",
          "method.response.header.Access-Control-Allow-Methods": "\\'GET,OPTIONS\\'",
          "method.response.header.Access-Control-Allow-Headers": "\\'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token\\'"
        },
        "StatusCode": 200
      }
    ],
    "MethodResponses": [
      {
        "ResponseModels": {
          "application/json": {
            "Ref": "AlertDetailsModel"
          }
        },
        "ResponseParameters": {
          "method.response.header.Access-Control-Allow-Origin": true,
          "method.response.header.Access-Control-Allow-Methods": true,
          "method.response.header.Access-Control-Allow-Headers": true
        },
        "StatusCode": 200
      }
    ]
  }
},

当我运行模板时,出现以下错误:

    14:23:06 UTC-0400   CREATE_FAILED   AWS::ApiGateway::Method AlertDetailMock Encountered unsupported property IntegrationResponses

这里的文档说:

静态值“STATIC_VALUE”。STATIC_VALUE 是一个字符串文字,必须用一对单引号括起来。

我尝试了所有我能想到的逃跑组合,但都无济于事。

标签: jsonamazon-web-servicesaws-api-gatewayamazon-cloudformationdevops

解决方案


您给定的模板有两个问题。

第一个是,IntegrationResponses元素需要在Integration元素内部。

第二个问题与转义有关,您实际上不需要在 JSON 中转义单引号。所以只要把值放在里面就可以了。

    "AlertDetailMock": {
        "Type": "AWS::ApiGateway::Method",
        "Properties": {
            "RestApiId": {
                "Ref": "RestApi"
            },
            "ResourceId": {
                "Fn::GetAtt": ["RestApi", "RootResourceId"]
            },
            "HttpMethod": "OPTIONS",
            "AuthorizationType": "NONE",
            "Integration": {
                "Type": "MOCK",
                "RequestTemplates": {
                    "application/json": "{\"statusCode\": 200}"
                },

                "IntegrationResponses": [{
                    "ResponseTemplates": {
                        "application/json": ""
                    },
                    "ResponseParameters": {
                        "method.response.header.Access-Control-Allow-Origin": "'*'",
                        "method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'",
                        "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
                    },
                    "StatusCode": 200
                }]
            },
            "MethodResponses": [{
                "ResponseParameters": {
                    "method.response.header.Access-Control-Allow-Origin": true,
                    "method.response.header.Access-Control-Allow-Methods": true,
                    "method.response.header.Access-Control-Allow-Headers": true
                },
                "StatusCode": 200
            }]
        }
    }

推荐阅读