简体   繁体   中英

AWS cloudformation condition for creating a resource

How to use a condition for AWS Child resource type

  • I wanted to create an AWS backup plan with 2 backup rules with a condition (example, if I set create2backup rule as "true", it should create the rule 1 and rule 2, if the condition is false, it should ignore creating the second rule and it should create the rule 1.

Condition - create rule is true --- Creates Rule 1 and Rule 2

Condition - create rule is false --- Creates Rule1 and should ignore creating rule 2 and exit


whatever thee condition is it should create the Rule 1, the condition should only apply to Rule 2.

Try1 :
BackupPlan:
    Type: AWS::Backup::BackupPlan
    Properties:
      BackupPlan: 
        BackupPlanName: backupplan
        BackupPlanRule:
          -  
            RuleName: !Ref RuleName   
          - <Some condition>
            RuleName: !Ref RuleName2



Try2:
StorageBackupPlan:
    Type: AWS::Backup::BackupPlan
   # DependsOn: StorageBackupVault
    Properties:
      BackupPlan: 
        BackupPlanName: !Ref BackupPlanName
        BackupPlanRule:
          !If
            - Createbackuprule2
            - 
              RuleName: !Ref RuleName
              
            - 
              RuleName: !Ref RuleName2
              

Error for try 2 - Properties validation failed for resource StorageBackupPlan with message: #/BackupPlan/BackupPlanRule: expected type: JSONArray, found: JSONObject

Try 3 : worked but not as I expected, if condition is true it creates rule 1 if the condition is false it creates rule 2 - got this from below answer

StorageBackupPlan:
    Type: AWS::Backup::BackupPlan
   # DependsOn: StorageBackupVault
    Properties:
      BackupPlan: 
        BackupPlanName: !Ref BackupPlanName
        BackupPlanRule:
          !If
            - Createbackuprule2 
            -
              - RuleName: !Ref RuleName1
            -
              - RuleName: !Ref RuleName2
                

            

Conditions is a top level section and can't be used inside Properties section. It is used to decide whether a resource will be created or not. You could have two different resources and create one of them based on the condition. This of course creates some duplication.

You should be able to achieve the desired result by using the intrinsic Fn:If condition function like that:

Parameters:
  CreateNewRole:
    Type: String
    AllowedValues:
      - yes
      - no
  RuleName:
    Type: String
  RuleName2:
    Type: String

Conditions:
  CreateNewRoleCondition:
    !Equals
      - !Ref CreateNewRole
      - yes

Resources:
  MyBackupPlan:
    Type: AWS::Backup::BackupPlan
    Properties:
      BackupPlan:
        BackupPlanName: backupplan
        BackupPlanRule:
          !If
            - CreateNewRoleCondition
            -
              - RuleName: !Ref RuleName
            -
              - RuleName: !Ref RuleName2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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