我有一个带有嵌套堆栈的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
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
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,但到目前为止还算不上运气。
答案 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参数都无法准备。