gpt4 book ai didi

amazon-web-services - AWS 云形成 : Passing Values between Nested Stacks

转载 作者:行者123 更新时间:2023-12-03 03:33:45 24 4
gpt4 key购买 nike

更多 AWS 问题!好的,我们的想法是一个主模板调用所有嵌套堆栈。在此处的帮助下,我弄清楚了如何将参数从主服务器传递到嵌套堆栈。现在我试图弄清楚如何将值从嵌套堆栈传递到嵌套堆栈。我认为这应该通过导出和进口来完成,但我认为我的做法不太正确。我不确定是我的导入还是导出错误。

我收到的错误是:

No export named TestStack1-VpcStackID found. Rollback requested by user.

大师:

{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Master template",
"Parameters" : {
"availabilityZone" : {
"Default" : "us-east-1d",
"Description" : "Enter AvailabilityZone.",
"Type" : "String"
},
"VpcCidrBlock" : {
"Default" : "10.0.0.0/16",
"Description" : "VPC CIDR Block.",
"Type" : "String"
},
"PublicSubnetCidrBlock" : {
"Default" : "10.0.0.0/24",
"Description" : "Public subnet CIDR block.",
"Type" : "String"
}
},
"Resources" : {
"VpcStack" : {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"Parameters" : {
"VpcCidrBlock" : {
"Ref" : "VpcCidrBlock"
}
},
"TemplateURL" : "https://s3.amazonaws.com/url/templates/vpcStack.json",
"TimeoutInMinutes" : "5"
}
},
"PublicRouteStack" : {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"Parameters" : {
"PublicSubnetCidrBlock" : {
"Ref" : "PublicSubnetCidrBlock"
},
"VpcStack" : {
"Fn::ImportValue" : {
"Fn::Sub" : "${AWS::StackName}-VpcStackID"
}
}
},
"TemplateURL" : "https://s3.amazonaws.com/url/templates/publicRouteStack.json",
"TimeoutInMinutes" : "5"
}
}
}
}

VpcStack(嵌套 - 我认为我的输出不正确):

{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "VPC template",
"Parameters" : {
"VpcCidrBlock" : {
"Description" : "Vpc CIDR Block.",
"Type" : "String"
}
},
"Resources" : {
"VpcStack" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"EnableDnsSupport" : "true",
"EnableDnsHostnames" : "true",
"CidrBlock" : {
"Ref" : "VpcCidrBlock"
},
"Tags" : [
{
"Key" : "Application",
"Value" : {
"Ref" : "AWS::StackName"
}
}
]
}
}
},
"Outputs" : {
"VpcStack" : {
"Description" : "VPC Stack ID.",
"Value" : {
"Ref" : "VpcStack"
},
"Export" : {
"Name" : {
"Fn::Sub" : "${AWS::StackName}-VpcStackID"
}
}
}
}
}

PublicStubnetStack(我认为这就是它失败的地方):

{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Public Subnet Stack",
"Parameters" : {
"PublicSubnetCidrBlock" : {
"Default" : "10.0.0.0/24",
"Description" : "Public subnet CIDR block.",
"Type" : "String"
},
"VpcStack" : {
"Description" : "VPC Stack.",
"Type" : "String"
}
},
"Resources" : {
"PublicSubnet" : {
"Type" : "AWS::EC2::Subnet",
"Properties" : {
"VpcId" : {
"Ref" : "VpcStack"
},
"CidrBlock" : {
"Ref" : "PublicSubnetCidrBlock"
},
"Tags" : [
{
"Key" : "Application",
"Value" : {
"Ref" : "AWS::StackName"
}
},
{
" Key" : "Network",
"Value" : "Public"
}
]
}
}
},
"Outputs" : {
"PublicSubnet" : {
"Description" : "Public Subnet ID.",
"Value" : {
"Ref" : "PublicSubnet"
},
"Export" : {
"Name" : {
"Fn::Sub" : "${AWS::StackName}-PublicSubnetID"
}
}
}
}
}

很抱歉发布了这么多,我对 AWS 非常陌生,并且正在努力快速掌握它。

最佳答案

问题

您的问题是您将值导出为

"Export" : {
"Name" : {
"Fn::Sub" : "${AWS::StackName}-VpcStackID"
}
}

您正在使用 ${AWS::StackName} 变量,该变量会将当前堆栈名称替换为导出变量名称。请注意,这是嵌套堆栈的堆栈名称

然而,在您的包装模板中,您尝试将值导入为:

"Fn::ImportValue" : {
"Fn::Sub" : "${AWS::StackName}-VpcStackID"
}

再次,您将用变量 ${AWS::StackName} 替换当前堆栈,在本例中是您的包装器堆栈.

请注意,当您使用嵌套堆栈时,您实际上是在创建一个新堆栈,因此堆栈名称会根据您所在的模板而变化。

决议

不要对变量使用导入/导出。

在嵌套模板中,从输出中删除 Export 元素。你不需要它们。只需使用堆栈参数将值从包装器堆栈传递到嵌套堆栈,并使用堆栈输出将值从嵌套堆栈传递回包装器堆栈。

在包装器堆栈中,使用 VpcStack 的输出,如下所示:

"PublicRouteStack" : {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"Parameters" : {
"PublicSubnetCidrBlock" : {
"Ref" : "PublicSubnetCidrBlock"
},
"VpcStack" : {
"Fn::GetAtt" : [ "VpcStack", "Outputs.VpcStack" ]
}
},
"TemplateURL" : "https://s3.amazonaws.com/url/templates/publicRouteStack.json",
"TimeoutInMinutes" : "5"
}
}

请注意,在本例中,我通过 Fn::使用 VpcStack 嵌套堆栈中名为 VpcStack输出: GetAtt 函数。

PS。为了清楚起见,您应该更改一些姓名。尽量避免在各处重复使用相同的名称。它有助于让事情变得清晰。

关于amazon-web-services - AWS 云形成 : Passing Values between Nested Stacks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392955/

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