从另一个模板导入 VPCGatewayAttachment
Importing VPCGatewayAttachment from another template
我有一个包含 InternetGateway 的网络堆栈,它通过 VPCGatewayAttachment 元素附加到 VPC。
我有一个应用程序堆栈,其中有一个 EIP,它应该依赖于当前看起来像这样的 VPCGatewayAttachment:
"MyEIP": {
"Type": "AWS::EC2::EIP",
"DependsOn": [
{ "Fn::ImportValue" : {"Fn::Sub": "${NetworkStackName}-GatewayAttachment" } }
],
}
但是我收到一个模板错误,指出每个 DependsOn 值都必须是一个字符串。那么如何导入这个值呢?
您的 import 语句周围有括号。在 JSON 中意味着该对象将是一个数组而不是字符串。删除这些括号,您将克服此错误。
"MyEIP": {
"Type": "AWS::EC2::EIP",
"DependsOn": { "Fn::ImportValue": { "Fn::Sub": "${NetworkStackName}-GatewayAttachment" } }
}
正如文档所说(您已阅读),DependsOn
属性接受字符串(或字符串列表)。
这意味着字面意思,因为您并不是要使用 CloudFormation Fn
或 Ref
函数在那里插入一些值,而是您只需插入 CloudFormation 资源的名称,就像在模板中一样。
因此,您的模板应如下所示:
"MyEIP": {
"Type": "AWS::EC2::EIP",
"DependsOn": [
"MyGatewayAttachment"
],
}
,其中 MyGatewayAttachment
是模板中相应 CloudFormation 资源的 名称。
如果您再看一下官方文档中的 examples,您会发现所有示例都是这样做的。
您不需要使用 DependsOn
attribute on the AWS::EC2::EIP
resource in your application stack, because the AWS::EC2::VPCGatewayAttachment
resource is being created in a separate network stack. This is noted in the EIP resource's Domain
属性:
Note
If you define an Elastic IP address and associate it with a VPC that is defined in the same template, you must declare a dependency on the VPC-gateway attachment by using the DependsOn
attribute on this resource.
当在单独的模板中定义 VPC 时,您无需显式声明任何依赖项。只要应用程序堆栈在网络堆栈之前被删除,EIP 就会在 VPC 网关附件之前被删除。
更一般地说,在堆栈之间声明 DependsOn
从来都不是必要的。由于如果另一个堆栈引用其输出之一,则无法删除堆栈,因此使用 Fn::ImportValue
创建跨堆栈引用会创建一个依赖项,即必须在引用堆栈之前删除包含该引用的堆栈。
我有一个包含 InternetGateway 的网络堆栈,它通过 VPCGatewayAttachment 元素附加到 VPC。
我有一个应用程序堆栈,其中有一个 EIP,它应该依赖于当前看起来像这样的 VPCGatewayAttachment:
"MyEIP": {
"Type": "AWS::EC2::EIP",
"DependsOn": [
{ "Fn::ImportValue" : {"Fn::Sub": "${NetworkStackName}-GatewayAttachment" } }
],
}
但是我收到一个模板错误,指出每个 DependsOn 值都必须是一个字符串。那么如何导入这个值呢?
您的 import 语句周围有括号。在 JSON 中意味着该对象将是一个数组而不是字符串。删除这些括号,您将克服此错误。
"MyEIP": {
"Type": "AWS::EC2::EIP",
"DependsOn": { "Fn::ImportValue": { "Fn::Sub": "${NetworkStackName}-GatewayAttachment" } }
}
正如文档所说(您已阅读),DependsOn
属性接受字符串(或字符串列表)。
这意味着字面意思,因为您并不是要使用 CloudFormation Fn
或 Ref
函数在那里插入一些值,而是您只需插入 CloudFormation 资源的名称,就像在模板中一样。
因此,您的模板应如下所示:
"MyEIP": {
"Type": "AWS::EC2::EIP",
"DependsOn": [
"MyGatewayAttachment"
],
}
,其中 MyGatewayAttachment
是模板中相应 CloudFormation 资源的 名称。
如果您再看一下官方文档中的 examples,您会发现所有示例都是这样做的。
您不需要使用 DependsOn
attribute on the AWS::EC2::EIP
resource in your application stack, because the AWS::EC2::VPCGatewayAttachment
resource is being created in a separate network stack. This is noted in the EIP resource's Domain
属性:
Note
If you define an Elastic IP address and associate it with a VPC that is defined in the same template, you must declare a dependency on the VPC-gateway attachment by using the
DependsOn
attribute on this resource.
当在单独的模板中定义 VPC 时,您无需显式声明任何依赖项。只要应用程序堆栈在网络堆栈之前被删除,EIP 就会在 VPC 网关附件之前被删除。
更一般地说,在堆栈之间声明 DependsOn
从来都不是必要的。由于如果另一个堆栈引用其输出之一,则无法删除堆栈,因此使用 Fn::ImportValue
创建跨堆栈引用会创建一个依赖项,即必须在引用堆栈之前删除包含该引用的堆栈。