CloudFormation, apply Condition on DependsOn

后端 未结 3 1374

The task that I need to do is make CDN depend on a S3 bucket. But we want to make it use the existing bucket rather than creating a new one.

Here is the sample code

3条回答
  •  臣服心动
    2020-12-24 14:00

    For yaml users, you can also use:

    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. I got this from: https://garbe.io/blog/2017/07/17/cloudformation-hacks/

提交回复
热议问题