如何将参数传递到 Amazon CloudFormation 中的嵌套堆栈?

如何将参数传递到 Amazon CloudFormation 中的嵌套堆栈?

我正在使用 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-网关-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 传递到嵌套堆栈模板的示例。我做错了什么?

答案1

您当前传递的是字符串“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"
      }
    }

相关内容