0

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
                

            
  • "I couldn't use the condition there, it thrown error" - what error exactly? What did you actually try? – Marcin Oct 26 '21 at 06:26
  • I used the condition there and it thrown error saying "Condition: CreateNewRole" property is not expected here – user9488542 Oct 26 '21 at 19:56

1 Answers1

1

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
            -
              - RuleName: !Ref RuleName
Dunedan
  • 7,848
  • 6
  • 42
  • 52