【问题标题】:CloudFormation cnf-lint Obsolete "DependsOn"CloudFormation cnf-lint 已过时的“DependsOn”
【发布时间】:2022-01-20 17:52:14
【问题描述】:

在以下代码上运行 cfn-lint 时,我收到警告

"MicroserviceSG": {
   "Type": "AWS::EC2::SecurityGroup",
⚠  "DependsOn": "MicroserviceLoadBalancerSGPrivate",
   "Properties": {
      "GroupName": {"Fn::Join": ["-", [{"Ref": "Name"}, {"Ref": "Env"}, "container-sg"]]},
      "GroupDescription": "HTTP",
         "VpcId": {"Ref": "VpcId"},
         "SecurityGroupIngress": [
            {
                "IpProtocol": "tcp",
                "FromPort": 80,
                "ToPort": 80,
                "SourceSecurityGroupId": { "Ref": "MicroserviceLoadBalancerSGPrivate" }
             }
           ]
        }
    },
W3005 Obsolete DependsOn on resource (MicroserviceLoadBalancerSGPrivate),
dependency already enforced by a "Ref" at 
Resources/MicroserviceSG/Properties/SecurityGroupIngress/0/SourceSecurityGroupId/Ref

这个警告有效吗?某些对象引用会执行隐式依赖检查吗?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    是的,警告是有效的,因为 Ref 用法隐式定义了对 MicroserviceLoadBalancerSGPrivate 的依赖。从技术上讲,redundant 会比 obsolete 更正确。

    除非您特别需要在 MicroserviceLoadBalancerSGPrivate 之后创建 MicroserviceSG,否则您应该删除 DependsOn 并让 CloudFormation 完成它,因为 CloudFormation 将优化和并行化部署。


    DependsOn 最常见的用途是强制按特定顺序创建和删除资源,如文档所述:

    您可以对任何资源使用 DependsOn 属性。以下是一些典型用途: 声明必须按特定顺序创建或删除的资源的依赖关系

    DependsOn documentation 提供了更多关于何时以及为何要使用DependsOn 的方案。

    【讨论】:

    • "除非您特别需要在 MicroserviceLoadBalancerSGPrivate 之后创建 MicroserviceSG"。这是最初的问题,陈述是否多余(相等)?添加了“DependsOn”:“MicroserviceLoadBalancerSGPrivate”语句,因为没有这个,CloudFormation 模板就失败了。我后来添加了入口 Ref,导致当前的 cnf-lint 警告。我想删除 DependsOn 语句,但我的经验是 Ref 本身并不能保证构建顺序。
    • 区别很微妙。 DependsOn 保证资源 B 的创建过程只会在资源 A 完成创建后开始。 Ref 不保证这一点,因为创建可以并行。 because the CloudFormation template failed without this .. 错误是什么?
    猜你喜欢
    • 2016-04-09
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2022-12-10
    • 2021-06-03
    • 2016-12-03
    相关资源
    最近更新 更多