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

时间:2019-09-16 14:40:33

标签: amazon-web-services amazon-cloudformation

我有一个带有嵌套堆栈的CF父模板。我想做的是在一个嵌套堆栈中设置DependsOn属性,以检查来自另一个嵌套堆栈的资源。

这是我的设置:

父堆栈:(在嵌套堆栈之间传递资源引用)

  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

嵌套的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

嵌套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

请参见上面的代码中的“ DependsOn:[ECSCluster,!Ref MasterDB]”。我做错了吗? 我尝试了其他变体来满足DependsOn,但到目前为止还算不上运气。

1 个答案:

答案 0 :(得分:1)

对于特定的场景,您实际上并不需要使用DependsOn,并且我认为此属性甚至不支持引用堆栈外部的资源。 原因是为了引用嵌套堆栈中的值,需要从另一个堆栈的Output属性中传递该值。 只需将Output参数传递到嵌套堆栈,即可使该堆栈依赖于其从中导出的其他嵌套堆栈-仅此一项即可实现您的目标。

接受您的代码,

嵌套的ECS堆栈:

Parameters:

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

这就是您需要做的,甚至不需要在嵌套堆栈中的任何位置使用该参数。

因此,如果一个堆栈依赖于另一个堆栈,那么它们只能一个接一个地执行并自上而下完成。

例如,如果:

堆栈A:接受来自堆栈B的Attr1输出

堆栈B:接受堆栈A的Attr2输出

以上内容将始终失败,因为无论首先执行哪个堆栈,依赖于其的Attr参数都无法准备。