在 Cloudformation 中将 DependsOn 与 Fn::ImportValue 结合使用

Using DependsOn with Fn::ImportValue in Cloudformation

是否可以像这样将 DependsOn 与 Fn::ImportValue 一起使用?

"DependsOn": {"Fn::ImportValue": {
            "Fn::Sub": "${MyStack1}-VPCGatewayAttachment"
        }}

具体来说,我将 VPC 和 InternetGatewayAttchment 放在一个堆栈中。我在 Stack2 中导入这个堆栈,我有我的 ELB,我正在尝试使用 DependsOn 来依赖这个附件

你不应该这样使用 DependsOn。只有在模板内的资源之间创建依赖关系时才需要它。具体来说,它用于覆盖 Cloudformation 创建资源的顺序。

FN::ImportValue 本质上是一个依赖函数,因为导出值必须在堆栈生成之前存在。因此,只需在适当的资源中引用您的进口价值。

如果您认为需要添加 DependsOn attribute to the AWS::EC2::VPCGatewayAttachment because of the documentation's recommendation When a DependsOn attribute is required,请注意这些建议仅适用于在同一模板中定义的资源:

If your AWS CloudFormation template defines a VPC, a gateway, and a gateway attachment, any resources that require the gateway are dependent on the gateway attachment.

当网关附件在单独的模板中定义时,您不需要显式声明任何额外的依赖项。只要应用程序堆栈在网络堆栈之前被删除,依赖资源将在 VPC 网关附件之前被删除。

一般来说,如 中所述,从来没有必要在堆栈之间声明 DependsOn。由于如果另一个堆栈引用其输出之一,则无法删除堆栈,因此使用 Fn::ImportValue 创建跨堆栈引用会创建一个依赖项,即必须在引用堆栈之前删除包含该引用的堆栈。