簡體   English   中英

AWS Cloudformation-如何依賴另一個嵌套堆棧中的DependsOn資源

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

我有一個帶有嵌套堆棧的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,但到目前為止還沒有運氣。

您實際上不需要為特定的方案使用DependsOn,而且我認為該屬性甚至不支持引用堆棧外部的資源。 原因是為了引用嵌套堆棧中的值,需要從另一個堆棧的Output屬性中傳遞該值。 只需將Output參數傳遞到嵌套堆棧,即可使該堆棧依賴於其從中導出的另一個嵌套堆棧-僅此一項即可實現您的目標。

拿你的代碼,

嵌套ECS堆棧:

Parameters:

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

這就是您需要做的所有事情,甚至不需要在嵌套堆棧中的任何位置使用該參數。

因此,如果一個堆棧依賴於另一個堆棧,那么它們只能一個接一個地上下執行並完成。

例如,如果:

堆棧A:接受堆棧B的Attr1輸出

堆棧B:接受來自堆棧A的Attr2輸出

上面的方法總是會失敗的,因為無論首先執行哪個堆棧,依賴於它的Attr參數都不會准備好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM