Home > OS >  AWS Cloudformation to pick ScheduleExpression from the Mapping for regions
AWS Cloudformation to pick ScheduleExpression from the Mapping for regions

Time:11-01

How to pick the AWS backup SheduleExpression automatically based on the Region defined under Mappings Block in a CloudFormation template.

This means if i am deploying the stack in the eu-west-1 then it should automatically take sched3 and if deploying the stack in the ap-southeast-1 then it should pick the sched4.

My CloundFormation code block:

Mappings: 
  RegionMap: 
    us-east-1: 
      sched1: "cron(00 19 * * ? *)"
    us-west-1: 
      sched2: "cron(00 18 * * ? *)"
    eu-west-1: 
      sched3: "cron(00 17 * * ? *)"
    ap-southeast-1: 
      sched4: "cron(00 16 * * ? *)"
    ap-northeast-1: 
      sched5: "cron(00 15 * * ? *)"

  FSxBackupPlan:
    Type: "AWS::Backup::BackupPlan"
    Properties:
      BackupPlan:
        BackupPlanName: !Ref FsxBackupPlanName
        BackupPlanRule:
          -
            RuleName: !Ref FsxBackupRuleName
            TargetBackupVault: !Ref FSxBackupsVault
            StartWindowMinutes: 240
            ScheduleExpression: !FindInMap
              - RegionMap
              - !Ref 'AWS::Region'

Other way i am trying:

             ScheduleExpression: !FindInMap
                !If [sched1, !Ref "AWS::Region"] 

I am trying to understand above but not getting it as a newbie to aws cloudformation.

CodePudding user response:

You are making your life more difficult than it should be by using the extra unnecessary key.

Instead, use:

Mappings: 
  RegionMap: 
    us-east-1: 
      sched: "cron(00 19 * * ? *)"
    us-west-1: 
      sched: "cron(00 18 * * ? *)"
    eu-west-1: 
      sched: "cron(00 17 * * ? *)"
    ap-southeast-1: 
      sched: "cron(00 16 * * ? *)"
    ap-northeast-1: 
      sched: "cron(00 15 * * ? *)"

and:

ScheduleExpression: !FindInMap
  - RegionMap
  - !Ref 'AWS::Region'
  - sched
  • Related