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

Using a Resource from a Nested Stack in Another Nested Stack with DependsOn

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

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

是重复的 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 语句移至需要依赖项的资源中的主要 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