我正在使用 CloudFormation 来管理 Amazon API Gateway 堆栈,并尝试(重新)使用嵌套堆栈向我的每个 HTTP 端点方法添加一个 OPTIONS 方法,以便我可以使用 CORS 标头进行响应。
这是引用嵌套堆栈的 CloudFormation 代码段:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation template for example HTTP endpoint",
"Resources": {
"MyRestApi": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Name": "api.example.com"
}
},
"HelloResource": {
"Type": "AWS::ApiGateway::Resource",
"Properties": {
"RestApiId": { "Ref": "MyRestApi" },
"ParentId": { "Fn::GetAtt": [ "MyRestApi", "RootResourceId" ] },
"PathPart": "hello"
}
},
"GETHello": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"RestApiId": { "Ref": "MyRestApi" },
"ResourceId": { "Ref": "HelloResource" },
"HttpMethod": "GET",
"AuthorizationType": "NONE",
"Integration": {
"Type": "HTTP",
"IntegrationHttpMethod": "GET",
"Uri": "https://my-api-server.example.com/hello",
"IntegrationResponses": [ { "StatusCode": "200" } ],
"RequestParameters": { "integration.request.header.Authorization": "method.request.header.Authorization" }
},
"MethodResponses": [
{
"StatusCode": "200",
"ResponseModels": { "text/html": "Empty" }
}
],
"RequestParameters": { "method.request.header.Authorization": true }
}
},
"OPTIONSHello": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"Parameters": {
"RestApiId": "MyRestApi",
"ResourceId": "HelloResource"
},
"TemplateURL": "https://s3-eu-west-1.amazonaws.com/my-cloudformation-bucket/api-gateway-cors-headers.json"
}
}
}
}
然后它引用的 CloudFormation 模板 - api-gateway-cors-headers.json - 看起来像这样:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation template for CORS headers,
"Parameters": {
"RestApiId": { "Type": "String" },
"ResourceId": { "Type": "String" }
},
"Resources": {
"CORSHeader": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"AuthorizationType": "NONE",
"RestApiId": { "Ref": "RestApiId" },
"ResourceId": { "Ref": "ResourceId" },
"HttpMethod": "OPTIONS",
"Integration": {
"IntegrationResponses": [
{
"StatusCode": 200,
"ResponseParameters": {
"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
"method.response.header.Access-Control-Allow-Methods": "'POST,OPTIONS'",
"method.response.header.Access-Control-Allow-Origin": "'*'"
},
"ResponseTemplates": {
"application/json": ""
}
}
],
"PassthroughBehavior": "WHEN_NO_MATCH",
"RequestTemplates": { "application/json": "{\"statusCode\": 200}" },
"Type": "MOCK"
},
"MethodResponses": [
{
"StatusCode": 200,
"ResponseModels": {
"application/json": "Empty"
},
"ResponseParameters": {
"method.response.header.Access-Control-Allow-Headers": false,
"method.response.header.Access-Control-Allow-Methods": false,
"method.response.header.Access-Control-Allow-Origin": false
}
}
]
}
}
}
}
问题是,我无法弄清楚如何将 RestApiId 和 ResourceId 参数从父堆栈传递到嵌套堆栈。根据我尝试的语法,我收到了三到四个不同的错误消息——最近Template format error: Unresolved resource dependencies [RestApiId, ResourceId] in the Resources block of the template
一次——但找不到任何关于如何将 REST API ID 和资源 ID 传递到嵌套堆栈模板的示例。我究竟做错了什么?