AWS Cloudformation APIGateway 尝试设置静态标头值时遇到不受支持的属性 IntegrationResponses

Joe*_*mpf 1 json amazon-web-services aws-cloudformation devops aws-api-gateway

我正在尝试使用 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
      }
    ]
  }
},
Run Code Online (Sandbox Code Playgroud)

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

    14:23:06 UTC-0400   CREATE_FAILED   AWS::ApiGateway::Method AlertDetailMock Encountered unsupported property IntegrationResponses
Run Code Online (Sandbox Code Playgroud)

这里的文档说:

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

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

jen*_*ter 7

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

第一个是,元素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
            }]
        }
    }
Run Code Online (Sandbox Code Playgroud)