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

jar*_*row 4 amazon-web-services aws-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
Run Code Online (Sandbox Code Playgroud)

嵌套 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
Run Code Online (Sandbox Code Playgroud)

嵌套 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
Run Code Online (Sandbox Code Playgroud)

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

mwd*_*dev 6

你真的不需要为你的特定场景使用 DependsOn,我认为这个属性甚至不支持引用堆栈外的资源。原因是为了引用嵌套堆栈中的值,它需要从另一个堆栈的输出属性传入。只需将 Output 参数传递给嵌套堆栈,就可以使该堆栈依赖于它从中导出的另一个嵌套堆栈 - 仅此一项即可实现您的目标。

拿着你的代码,

嵌套 ECS 堆栈:

Parameters:

  MasterDB:
    Description: Make this stack dependent on RDS resource
    Type: String
Run Code Online (Sandbox Code Playgroud)

这就是您需要做的所有事情,甚至不需要在嵌套堆栈中的任何地方使用该参数。

因此,如果一个堆栈依赖于另一个堆栈,则它们只能从上到下依次执行和完成。

例如,如果:

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

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

上面的总是会失败,因为不管先执行哪个栈,它所依赖的 Attr 参数都不会准备好。