简体   繁体   English

AWS Cloudformation-如何依赖另一个嵌套堆栈中的DependsOn资源

[英]AWS Cloudformation - how to DependsOn resource from another nested stack

I have a CF parent template with nested stacks in it. 我有一个带有嵌套堆栈的CF父模板。 What I'm trying to do is set DependsOn attribute in one of the nested stacks, to check for the resource from another nested stack. 我想做的是在一个嵌套堆栈中设置DependsOn属性,以检查来自另一个嵌套堆栈的资源。

Here is my setup: 这是我的设置:

Parent stack: (Passing the resource reference between nested stacks) 父堆栈:(在嵌套堆栈之间传递资源引用)

  RDS:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://cf-app-stack.s3.eu-west-2.amazonaws.com/infrastructure/rds.yaml
      Parameters:
        EnvironmentName: !Ref AWS::StackName
        DBVPCSecurityGroup: !GetAtt SecurityGroups.Outputs.DBVPCSecurityGroup
        PrivateSubnet1: !GetAtt VPC.Outputs.PrivateSubnet1
        PrivateSubnet2: !GetAtt VPC.Outputs.PrivateSubnet2

  ECS:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://cf-app-stack.s3.eu-west-2.amazonaws.com/infrastructure/ecs-cluster.yaml
      Parameters:
        EnvironmentName: !Ref AWS::StackName
        MasterDB: !GetAtt RDS.Outputs.MasterDB
        InstanceType: t2.micro
        ClusterSize: 1
        VPC: !GetAtt VPC.Outputs.VPC
        SecurityGroup: !GetAtt SecurityGroups.Outputs.ECSHostSecurityGroup
        Subnets: !GetAtt VPC.Outputs.PrivateSubnets

nested RDS stack: (exports the DB resource ref) 嵌套的RDS堆栈:(导出数据库资源引用)

 MasterDB:
    Type: AWS::RDS::DBInstance
    Properties:
      DBSnapshotIdentifier: arn:aws:rds:eu-west-2:731152906121:snapshot:db-starter-image
      AllocatedStorage: !Ref DBAllocatedStorage
      DBInstanceClass: !Ref DBInstanceClass
      Engine: MySQL
      # Some DB instance properties aren't valid when you restore from a snapshot, such as the MasterUsername and MasterUserPassword properties. 
      #MasterUsername: !Ref DBUser
      #MasterUserPassword: !Ref DBPassword
      MultiAZ: !Ref 'MultiAZ'
      Tags:
      - Key: Name
        Value: !Sub ${EnvironmentName}-Database
      DBSubnetGroupName: !Ref myDBSubnetGroup
      VPCSecurityGroups: [ !Ref DBVPCSecurityGroup ]
    DeletionPolicy: Snapshot

Outputs:
  MasterDB:
    Description: A reference to the created DB
    Value: MasterDB

nested ECS stack: (I want this one to depend on the RDS instance from the above nested stack) 嵌套的ECS堆栈:(我希望这个依赖于上述嵌套堆栈中的RDS实例)

Parameters:
  MasterDB:
    Description: A reference to the created DB
    Type: String

Resources:
  ECSCluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: !Ref EnvironmentName

  ECSAutoScalingGroup:
    DependsOn: [ECSCluster, !Ref MasterDB]
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
      VPCZoneIdentifier: !Ref Subnets
      LaunchConfigurationName: !Ref ECSLaunchConfiguration
      MinSize: !Ref ClusterSize
      MaxSize: !Ref ClusterSize
      DesiredCapacity: !Ref ClusterSize
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} ECS host
          PropagateAtLaunch: true

See "DependsOn: [ECSCluster, !Ref MasterDB]" in the above code. 请参见上面的代码中的“ DependsOn:[ECSCluster,!Ref MasterDB]”。 Am I doing this wrong? 我做错了吗? I tried other variations trying to satisfy DependsOn but so far no luck. 我尝试了其他变体来满足DependsOn,但到目前为止还没有运气。

You don't really need to use DependsOn for you specific Scenario and I think this attribute doesn't even support referring to resources outside of the stack. 您实际上不需要为特定的方案使用DependsOn,而且我认为该属性甚至不支持引用堆栈外部的资源。 The reason is that in order to reference a value in a nested stack, it needs to be passed in from Output attributes from another stack. 原因是为了引用嵌套堆栈中的值,需要从另一个堆栈的Output属性中传递该值。 And just passing an Output parameter to a nested stack makes this stack dependent on the other nested stack it was exported from - and that alone achieves your goal. 只需将Output参数传递到嵌套堆栈,即可使该堆栈依赖于其从中导出的另一个嵌套堆栈-仅此一项即可实现您的目标。

Taking your code, 拿你的代码,

nested ECS stack: 嵌套ECS堆栈:

Parameters:

  MasterDB:
    Description: Make this stack dependent on RDS resource
    Type: String

That's all you need to do, the parameter does not even need to be used anywhere in the nested stack. 这就是您需要做的所有事情,甚至不需要在嵌套堆栈中的任何位置使用该参数。

So if one stack is dependent on another, they can only be executed and completed top to bottom, one after another. 因此,如果一个堆栈依赖于另一个堆栈,那么它们只能一个接一个地上下执行并完成。

For example if: 例如,如果:

Stack A: accepts Attr1 Output from stack B 堆栈A:接受堆栈B的Attr1输出

and

Stack B: accepts Attr2 Output from stack A 堆栈B:接受来自堆栈A的Attr2输出

The above will always fail, because regardless of which stack will be executed first, the Attr param it is dependent on will not be ready. 上面的方法总是会失败的,因为无论首先执行哪个堆栈,依赖于它的Attr参数都不会准备好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 DependsOn 在另一个嵌套堆栈中使用嵌套堆栈中的资源 - Using a Resource from a Nested Stack in Another Nested Stack with DependsOn AWS Cloudformation 如何将条件资源用作其他资源的 DependsOn - AWS Cloudformation how to use conditional resource as DependsOn for other resource 如何将值从嵌套堆栈导入父堆栈资源的依赖项? - How do you import value from a nested stack to a parent stack resource's dependson? 如何从AWS CloudFormation模板为特定资源类型创建堆栈 - How to create stack for specific resource types from aws cloudformation template AWS CloudFormation 堆栈:具有嵌套路径的 API 网关资源? - AWS CloudFormation stack: API Gateway resource with nested paths? 如何保护AWS CloudFormation堆栈不被删除? - How to protect an AWS CloudFormation stack from deletion? 如何确定 AWS 资源属于哪个 CloudFormation 堆栈? - How to determine what CloudFormation stack an AWS resource belongs to? 如何将列表传递给AWS CloudFormation中的嵌套堆栈参数? - How to pass a list to a nested stack parameter in AWS CloudFormation? 如何使用Cloudformation在AWS RestAPI中创建嵌套的资源路径? - How to create a nested Resource path in AWS RestAPI using Cloudformation? 嵌套cloudformation堆栈中的资源依赖关系问题 - Resource dependency issues in nested cloudformation stack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM