AWS Cloudformation APIGateway 在尝试设置静态 header 值时遇到不受支持的 属性 IntegrationResponses
AWS Cloudformation APIGateway Encountered unsupported property IntegrationResponses when trying to set static header value
我正在尝试使用 Cloudformation 在 AWS APIGateway 的方法执行上设置我的 Header 映射。
这是我希望我的最终状态:
这是我尝试使用的 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
文档 here 说:
Static value 'STATIC_VALUE'. The STATIC_VALUE is a string literal and must be enclosed within a pair of single quotes.
能想到的转义组合我都试过了,都没有用。
您给定的模板有两个问题。
第一个是,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
}]
}
}
我正在尝试使用 Cloudformation 在 AWS APIGateway 的方法执行上设置我的 Header 映射。
这是我希望我的最终状态:
这是我尝试使用的 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
文档 here 说:
Static value 'STATIC_VALUE'. The STATIC_VALUE is a string literal and must be enclosed within a pair of single quotes.
能想到的转义组合我都试过了,都没有用。
您给定的模板有两个问题。
第一个是,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
}]
}
}