【问题标题】:Using a Resource from a Nested Stack in Another Nested Stack with DependsOn通过 DependsOn 在另一个嵌套堆栈中使用嵌套堆栈中的资源
【发布时间】:2020-11-11 05:09:37
【问题描述】:

我一直在重构已经变得相当大的堆栈,因为它正在克服 AWS 上 CloudFormation 脚本的大小限制。在这样做的过程中,我不得不解决一些依赖关系(通常使用输出),但我遇到了以前从未遇到过的情况......

在使用DependsOn时,如何将一个嵌套堆栈(A)中创建的资源用于另一个嵌套堆栈(B)?

This question 是一个重复的问题,但答案并不合适,因为它实际上并不能解决我遇到的问题,它会根据特定用户的需求采取不同的方法。

这是嵌套堆栈 A 中的资源:

EndpointARestApi:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Body:
        Fn::Transform:
          Name: 'AWS::Include'
          Parameters:
            Location: !Join ['/', [ 's3:/', !Ref SharedBucketName, !Ref WorkspacePrefix, 'endpoint.yaml' ]]

这是堆栈 B 中的 DependsOn 请求:

EndpointUserPoolResourceServer:
    Type: Custom::CognitoUserPoolResourceServer
    DependsOn:
      - EndpointARestApi
      - CustomResource ## this resource is in the same stack and resolves properly

这发生在我在这个堆栈中的另一个资源上,所以我希望我可以轻松地做到这一点。如果没有,我相信我将不得不进行更多重构。

【问题讨论】:

  • DependsOn 仅适用于同一堆栈中的资源。您不能在不同的堆栈或嵌套堆栈之间使用它。
  • @Marcin 我可以将资源放在主 CFN 中并让嵌套堆栈使用它吗?
  • 是的,您可以将参数传递给嵌套堆栈。这些参数可以基于主模板中的资源。
  • 另一种可能性是将DependsOn: Stack A 属性添加到主堆栈中堆栈B 的CloudFormation 资源。这样,堆栈 B 将在堆栈 A 之后创建。
  • @berenbums 是的!希望我明天可以测试一下。

标签: amazon-web-services dependencies amazon-cloudformation


【解决方案1】:

按照 cmets 中的建议,我将 DependsOn 语句向上移动到需要依赖项的资源中的主 CFN 脚本,并确保依赖项在其他资源上,而不是嵌套资源上,如下所示:

Primary
  ResourceA
  ResourceB
    DependsOn: ResourceA

最终在 CloudFormation 脚本中看起来像这样:

EndpointUserPoolResourceServer:
    Type: "AWS::CloudFormation::Stack"
    DependsOn:
      - EndpointARestApiResource
    Properties:
      Parameters:
        AppName: !Ref AppName
        Environment: !Ref Environment
        DeveloperPrefix: !Ref DeveloperPrefix
        DeployPhase: !Ref DeployPhase

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2018-10-27
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2020-10-31
    • 2015-04-07
    相关资源
    最近更新 更多