AWS Cloudformation-如何依赖另一个嵌套堆栈中的DependsOn资源 [英] AWS Cloudformation - how to DependsOn resource from another nested stack
问题描述
我有一个带有嵌套堆栈的CF父模板.我想做的是在一个嵌套堆栈中设置DependsOn属性,以检查另一个嵌套堆栈中的资源.
I have a CF parent template with nested stacks in it. 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.
这是我的设置:
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,但到目前为止还算不上运气.
See "DependsOn: [ECSCluster, !Ref MasterDB]" in the above code. Am I doing this wrong? I tried other variations trying to satisfy DependsOn but so far no luck.
推荐答案
对于特定的场景,您实际上并不需要使用DependsOn,而且我认为该属性甚至不支持引用堆栈外的资源.原因是为了引用嵌套堆栈中的值,需要从另一个堆栈的Output属性中传递该值.只需将Output参数传递到嵌套堆栈,即可使该堆栈依赖于它从中导出的其他嵌套堆栈-仅此一项即可实现您的目标.
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. 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. 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.
获取您的代码,
嵌套的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.
例如,如果:
堆栈A:接受堆栈B的Attr1输出
Stack A: accepts Attr1 Output from stack B
和
堆栈B:接受堆栈A的Attr2输出
Stack B: accepts Attr2 Output from stack A
以上内容将始终失败,因为无论首先执行哪个堆栈,依赖于其的Attr参数都无法准备.
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.
这篇关于AWS Cloudformation-如何依赖另一个嵌套堆栈中的DependsOn资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!