简体   繁体   English

基于 DependsOn 的 AWS 云形成条件

[英]AWS Cloud Formation Conditions on DependsOn

I am writing a cloud formation template and the creation of a resource in my stack it depends on the environment.我正在编写一个云形成模板,并且在我的堆栈中创建一个资源取决于环境。
Therefore, I check the value of a parameter (Environment), and based on it I create that resource (Condition: ISProduction).因此,我检查参数(环境)的值,并基于它创建该资源(条件:ISProduction)。
However, my problem is that in case that resource is created (MyProductionResource) another resource (AnotherResource) becomes dependent on it and needs to use an output attribute from the other (MyProductionResource).但是,我的问题是,在创建资源 (MyProductionResource) 的情况下,另一个资源 (AnotherResource) 依赖于它并且需要使用另一个 (MyProductionResource) 的输出属性。
Here the code:这里的代码:

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

My problem is that I want AnotherResource to be dependent on MyProductionResource only when ISProduction is true.我的问题是,只有当 ISProduction 为真时,我才希望 AnotherResource 依赖于 MyProductionResource。 An idea is to add some kind of conditions in the DependsOn item, or anything that would bring to the same result.一个想法是在 DependsOn 项中添加某种条件,或者任何可以带来相同结果的条件。
How can I do that on AWS Cloud Formation?我如何在 AWS Cloud Formation 上做到这一点?
Also I am not sure what happen when the resource that is listed in the dependsOn list is not created.此外,我不确定当未创建dependsOn 列表中列出的资源时会发生什么。 Would the cloud formation template generate an error?云形成模板会产生错误吗? How can I make this attribute read safety !GetAtt MyProductionResource.Outputs.SomeString ?我怎样才能使这个属性读取安全!GetAtt MyProductionResource.Outputs.SomeString ?

you can use !If for the parameter您可以使用 !If 作为参数

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

but unfortunately DependsOn does not allow Fn::If.但不幸的是 DependsOn 不允许 Fn::If。

So you could create to resource twice.所以你可以创建资源两次。

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

But having so many ifs is kind of against the idea that your environments should be as similar as possible.但是拥有如此多的 if 与您的环境应该尽可能相似的想法背道而驰。 So maybe you can get rid of this whole thing?所以也许你可以摆脱这整个事情?

Here's a alternative for "DependsOn does not allow Fn::If."这是“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*

Basically my 2nd resource only has DependsOn attribute if my 1st resource is non-existent, before running the CFN.在运行 CFN 之前,如果我的第一个资源不存在,基本上我的第二个资源只有 DependsOn 属性。 I got this from: https://garbe.io/blog/2017/07/17/cloudformation-hacks/我从: https : //garbe.io/blog/2017/07/17/cloudformation-hacks/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM