我正在使用CloudFormation来pipe理Amazon API网关堆栈,并尝试(重新)使用嵌套堆栈来为每个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到嵌套的堆栈模板中。 我究竟做错了什么?
您当前正在传递string“MyRestApi”和“HelloResource” – 而不是资源引用。 通过他们
"OPTIONSHello": { "Type": "AWS::CloudFormation::Stack", "Properties": { "Parameters": { "RestApiId": { "Ref": "MyRestApi" }, "ResourceId": { "Ref": "HelloResource"} }, "TemplateURL": "https://s3-eu-west-1.amazonaws.com/my-cloudformation-bucket/api-gateway-cors-headers.json" } }