通过DependsOn在另一个嵌套堆栈中使用嵌套堆栈中的资源

时间:2020-11-05 21:19:48

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

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

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

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

这是嵌套堆栈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

这与我在此堆栈中拥有的其他资源一起发生,因此我希望我可以轻松地做到这一点。如果没有,我相信我将不得不再重构一些。

1 个答案:

答案 0 :(得分:0)

如注释中所建议,我将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