[英]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: 这是我的设置:
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
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.