gpt4 book ai didi

amazon-web-services - 在 CloudFormation 堆栈之间引用资源

转载 作者:行者123 更新时间:2023-12-04 08:09:30 25 4
gpt4 key购买 nike

如果我有两个 cloudformation 堆栈,如何从另一个堆栈引用一个堆栈中的资源?

在下面的示例中,我有一个创建 EBS 卷的堆栈,并希望通过我的 EC2 实例的第二个堆栈中的 Ref: 键来引用它,但我不断收到回滚,因为它无法从第一个堆栈:

“模板格式错误: Unresolved 资源依赖性”

我已经尝试过 DependsOn 子句,但它不起作用。我需要通过参数传递信息吗?

{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"CubesNetworking": {
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/mybucket/cf_network.json"
}
},
"CubesInstances": {
"DependsOn": ["CubesNetworking"],
"Type": "AWS::CloudFormation::Stack",
"Properties": {
"TemplateURL": "https://s3.amazonaws.com/mybucket/cf_instances.json"
}
}
}
}

最佳答案

在每个嵌套堆栈中,都​​应该有一个输出部分。然后,您可以使用以下语法在调用堆栈(上面列出的那个)中获取这些值:

      { "Fn::GetAtt" : [ "CubesNetworking", "Outputs.VolumeID" ] }

然后,您可以通过参数将值传递到其他嵌套堆栈中:

  "Parameters" : {
"VolumeId" : { "Fn::GetAtt" : [ "CubesNetworking", "Outputs.VolumeID" ] }

您仍然需要 DependsOn,因为您需要在实例之前创建的卷。

编辑,2017 年中:

CloudFormation 引入了从一个堆栈导出值并在不必嵌套的其他堆栈中引用它们的功能。

因此您的输出可以指定导出:

Outputs:
Desc:
Value: !Ref CubesNetworking.VolumeID
Export:
Name: some-unique-name

然后在另一个堆栈中:

Fn::ImportValue: some-unique-name

关于amazon-web-services - 在 CloudFormation 堆栈之间引用资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662665/

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