gpt4 book ai didi

amazon-web-services - AWS Cloudformation - 如何依赖另一个嵌套堆栈中的资源

转载 作者:行者123 更新时间:2023-12-03 22:04:29 25 4
gpt4 key购买 nike

我有一个 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,而且我认为此属性甚至不支持引用堆栈外部的资源。原因是,为了引用嵌套堆栈中的值,需要从另一个堆栈的输出属性传入该值。只需将输出参数传递给嵌套堆栈,就会使该堆栈依赖于从中导出它的其他嵌套堆栈 - 仅此一点就可以实现您的目标。

获取您的代码,

嵌套 ECS 堆栈:

Parameters:

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

这就是您需要做的全部工作,甚至不需要在嵌套堆栈中的任何位置使用该参数。

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

例如,如果:

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

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

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

关于amazon-web-services - AWS Cloudformation - 如何依赖另一个嵌套堆栈中的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57959202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com