【问题标题】:AWS Cloud Formation Conditions on DependsOnDependsOn 上的 AWS 云形成条件
【发布时间】:2017-04-27 07:27:34
【问题描述】:

我正在编写一个云形成模板,并且在我的堆栈中创建一个资源取决于环境。
因此,我检查一个参数(环境)的值,并基于它创建该资源(条件:ISProduction)。
但是,我的问题是,如果创建了该资源 (MyProductionResource),另一个资源 (AnotherResource) 将依赖于它,并且需要使用另一个 (MyProductionResource) 的输出属性。
代码如下:

Conditions:
  ISProduction:
    "Fn::Equals":
      - !Ref Environment
      - production
 ...

 MyProductionResource:
    Type: AWS::CloudFormation::Stack
    Condition: ISProduction
    Properties:
    [.. properties..]

 AnotherResource:
    Type: AWS::CloudFormation::Stack
    DependsOn:
      - AResource
      - MyProductionResource
    Properties:
      TemplateURL: whatever
      Parameters:
        AParameter: !GetAtt MyProductionResource.Outputs.SomeString

我的问题是,仅当 ISProduction 为 true 时,我才希望 AnotherResource 依赖于 MyProductionResource。一个想法是在 DependsOn 项目中添加某种条件,或者任何会带来相同结果的条件。
如何在 AWS Cloud Formation 上做到这一点?
此外,我不确定当未创建 dependsOn 列表中列出的资源时会发生什么。云形成模板会产生错误吗?如何让这个属性读取安全 !GetAtt MyProductionResource.Outputs.SomeString ?

【问题讨论】:

    标签: amazon-web-services cloud amazon-cloudformation


    【解决方案1】:

    你可以使用 !If 作为参数

    AParameter: !If [ISProduction, !GetAtt MyProductionResource.Outputs.SomeString, "default value?!?"]
    

    但不幸的是 DependsOn 不允许 Fn::If。

    所以你可以创建两次资源。

    AnotherProductionResource:
      Type: AWS::CloudFormation::Stack
      Condition: ISProduction
      DependsOn:
      - AResource
      - MyProductionResource
      Properties:
        [...]
    AnotherNonProductionResource:
      Type: AWS::CloudFormation::Stack
      Condition: ISNotProduction
      DependsOn:
      - AResource
      Properties:
        [...]
    

    但是有这么多的 if 有点违背你的环境应该尽可能相似的想法。所以也许你可以摆脱这整件事?

    【讨论】:

      【解决方案2】:

      这是“DependsOn 不允许 Fn::If”的替代方法。

      Conditions:
        CreateConfigRecorder: !Equals [ !Ref ConfigRecorderExists, 'false' ]
      
      Resource:
      #my 1st AWS Resource
        ConfigRecorder: 
          Condition: CreateConfigRecorder
          Type: AWS::Config::ConfigurationRecorder
          *more codes below*
      
      #added, since DependsOn: !If is not possible, trigger by WaitCondition if CreateConfigRecorder is true
      #Hacks: https://garbe.io/blog/2017/07/17/cloudformation-hacks/
        ConfigRecorderWaitHandle: 
          Condition: CreateConfigRecorder
          DependsOn: ConfigRecorder
          Type: "AWS::CloudFormation::WaitConditionHandle"
      #added, since DependsOn: !If is not possible, trigger by WaitCondition if CreateConfigRecorder is false
        WaitHandle: 
          Type: "AWS::CloudFormation::WaitConditionHandle"
      #added, since DependsOn: !If is not possible
        WaitCondition: 
          Type: "AWS::CloudFormation::WaitCondition"
          Properties: 
            Handle: !If [CreateConfigRecorder, !Ref ConfigRecorderWaitHandle, !Ref WaitHandle]
            Timeout: "1"
            Count: 0
      #my 2nd AWS Resource that requires DependsOn Attribute
        AWSConfigRule:
          Type: AWS::Config::ConfigRule
          DependsOn: WaitCondition #added, since DependsOn: !If is not possible
          *more codes below*
      

      在运行 CFN 之前,如果我的第一个资源不存在,我的第二个资源基本上只有 DependsOn 属性。我来自:https://garbe.io/blog/2017/07/17/cloudformation-hacks/

      【讨论】:

        猜你喜欢
        • 2017-05-21
        • 1970-01-01
        • 2016-04-09
        • 2020-04-14
        • 2019-07-02
        • 2020-07-22
        • 1970-01-01
        • 2012-12-16
        相关资源
        最近更新 更多