Backups are an essential part of every implementation as they provide us with the required copies that can be used to restore in case of any incidents. Backups must be done automatically, consistently, and reliably for them to be effective. AWS Backup is one such service in the Amazon Cloud which tries to tackle the problem of having backups that meet this criterion. In this blog post, we are going to describe the steps and processes that can be used to automate backups using a tag-based assignment so that it can be extended to a large number of resources automatically. We will be using AWS CloudFormation and AWS Backup services for achieving this objective.
AWS Backup offers a cost-effective, fully-managed, policy-based service that enables us to centralize and automate data protection at scale. Amongst its various other features is "Tag-based backup policies". AWS Backup allows us to apply backup plans to AWS resources by using AWS tags. This particular feature integrates with AWS Tags and enables us to quickly apply a backup plan to a group of AWS resources. This is particularly useful for cases where we have more than one instance of a resource (like EC2s, RDSs, etc.) and we want them to be backed up in a consistent and compliant manner. AWS recommends using Tag-based backup policies when we are trying to protect more than 100 resources in a single plan. But there is no restriction to use this for a lower volume as well and I recommend using this approach wherever possible.
AWS CloudFormation (CF) provides us with the capability to provision and manage AWS resources using templates in a repeatable and consistent fashion. A CF template basically describes all the resources and their dependencies so that they can be launched and configured together as a stack.
There are some other terms also associated and used as part of AWS Backups for which I am giving a brief overview below. Details for each of them can be looked upon in the AWS documentation available.
- Backup Vault - storage container for backups
- Backup Plan - scheduler for backups. This basically defines when the backups should be done and with what frequency. A backup plan will be attached to the backup vault
- Recovery Point - snapshot/backup of resource that can be restored with AWS Backups
- Resource Assignment/Backup Selection - defines the target resources to backup. It gets attached to the backup vault
With all the items now defined, let's just look at the CloudFormation template that we are going to use. In the below template, we are creating a new AWS backup configuration and applying it to all AWS resources that will have a tag "backup-plan" with the value set as "BackupPlan-01-31days-daily".
- Parameters:
- UpdateParameter:
- Description: (optional) This parameter is a fake param please use/change it just if you want to force the update to the new SC version
- Type: String
- Default: ""
- BackupPlanName:
- Description: Enter the name of the backup plan (Required)
- Type: String
- Default: "test-01"
- CronExpression:
- Description: Enter the cron expression for your backup plan (Required)
- Type: String
- Default: "cron(0 0 ? * * *)"
- Retention:
- Description: This value will identify how many days your backup will be expired after (Required)
- Type: String
- Default: 31
- Metadata:
- AWS::CloudFormation::Interface:
- ParameterGroups:
- -
- Label:
- default: BackupPlan Configurations (Mandatory)
- Parameters:
- - BackupPlanName
- -
- Label:
- default: Bckup Rule configuration
- Parameters:
- - CronExpression
- - Retention
- Resources:
- BackupRole:
- Type: 'AWS::IAM::Role'
- Properties:
- RoleName: SampleBackupRole
- AssumeRolePolicyDocument:
- Version: 2012-10-17
- Statement:
- - Effect: Allow
- Principal:
- Service:
- - backup.amazonaws.com
- Action:
- - 'sts:AssumeRole'
- ManagedPolicyArns:
- - >-
- arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup
- BackupVault:
- Type: "AWS::Backup::BackupVault"
- Properties:
- BackupVaultName: !Sub ${BackupPlanName}-BackupVaultName
- AccessPolicy:
- Version: '2012-10-17'
- Statement:
- -
- Sid: 'Vault-Access-Policy'
- Effect: Deny
- Principal: "*"
- Action: "backup:DeleteRecoveryPoint"
- Resource:
- - "*"
- BackupPlan:
- Type: "AWS::Backup::BackupPlan"
- Properties:
- BackupPlan:
- BackupPlanName: !Ref BackupPlanName
- BackupPlanRule:
- -
- RuleName: !Sub ${BackupPlanName}-BackupRuleName
- TargetBackupVault: !Ref BackupVault
- ScheduleExpression: !Ref CronExpression
- Lifecycle:
- DeleteAfterDays: !Ref Retention
- DependsOn: BackupVault
- TagBasedBackupSelection:
- Type: "AWS::Backup::BackupSelection"
- Properties:
- BackupSelection:
- SelectionName: "TagBasedBackupSelection-01"
- IamRoleArn: !GetAtt BackupRole.Arn
- ListOfTags:
- -
- ConditionType: "STRINGEQUALS"
- ConditionKey: "backup-plan"
- ConditionValue: !Sub ${BackupPlanName}-${Retention}days-daily
- BackupPlanId: !Ref BackupPlan
- DependsOn: BackupPlan


Join the conversation! Your thoughts help the community grow.