DevOps Hive

Patterns / Recover

AWS Backup plan for daily RDS backups selected by tag

A backup plan that copies every database tagged backup = daily into a vault once a day and deletes each copy after 35 days, so adding a new database to backups is one tag.

  1. Tagged database
  2. Backup plan
  3. Daily job
  4. Backup vault

How it works

  1. Why the built-in RDS backups are not enough

    RDS automated backups are configured per database, so a new database is only covered if someone remembers to set it up. They are also deleted along with the database unless you choose otherwise at that moment. A backup plan sets the schedule and retention in one place, and recovery points in the vault outlive the database they came from.

  2. Create a vault

    A vault is the container that holds recovery points, encrypted at rest. A vault with recovery points in it cannot be deleted, which protects against a careless tofu destroy.

  3. Give AWS Backup a role

    AWS Backup does its work through an IAM role that only the backup service can assume. The pattern attaches the two policies AWS maintains for this: one for taking backups and one for restoring. People often leave out the restore policy and find out during an incident.

  4. Set the schedule and retention

    The plan runs at 05:00 UTC every day and keeps each recovery point for 35 days. Move the schedule to your quietest hour. Longer retention costs more storage, so choose the number of days from how far back you would realistically need to go.

  5. Tag the databases you want backed up

    The selection picks up any RDS instance or Aurora cluster with the tag backup = daily. The mistake people make is applying the plan and never adding the tag: the plan then runs every day and backs up nothing. Check the vault the next morning for a recovery point per database.

  6. Practise a restore

    A restore creates a new database from a recovery point. It does not overwrite the existing one, so your application has to be pointed at the new endpoint afterwards. Do this once on a quiet day and write down how long it took, because that time is your real recovery time.

What you set

region
Region where the databases live For example us-east-1
backup_tag_key
Tag key that marks a database for backup For example backup
backup_tag_value
Tag value that marks a database for backup For example daily
retention_days
Days to keep each recovery point For example 35

What it costs to run

Driven by how many GB of backups you keep, billed per GB each month. For one database of around 20 GB with 35 days of retention, expect roughly 2 to 10 dollars a month. The vault, plan and role are free. Estimate.

The configuration

# Daily backups with AWS Backup, chosen by tag.
#
# What this creates:
#   - a backup vault to hold recovery points
#   - a plan that runs once a day and deletes recovery points after a set number of days
#   - a selection that picks up every RDS database carrying the tag backup = daily
#   - the IAM role AWS Backup uses to take and restore backups

terraform {
  required_version = ">= 1.6.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.region
}

variable "region" {
  description = "AWS region where the databases to back up live."
  type        = string
  default     = "us-east-1"
}

variable "name" {
  description = "Short name used for the vault, plan and role."
  type        = string
  default     = "daily"
}

variable "backup_tag_key" {
  description = "Tag key that marks a resource for backup."
  type        = string
  default     = "backup"
}

variable "backup_tag_value" {
  description = "Tag value that marks a resource for backup."
  type        = string
  default     = "daily"
}

variable "schedule" {
  description = "When the backup runs, as a cron expression in UTC. The default is 05:00 UTC every day."
  type        = string
  default     = "cron(0 5 * * ? *)"
}

variable "retention_days" {
  description = "Days to keep each recovery point before AWS Backup deletes it."
  type        = number
  default     = 35
}

variable "resource_arns" {
  description = "Resource types the plan may back up. The default covers RDS instances and Aurora clusters. Use [\"*\"] to include every tagged resource AWS Backup supports."
  type        = list(string)
  default = [
    "arn:aws:rds:*:*:db:*",
    "arn:aws:rds:*:*:cluster:*",
  ]
}

variable "vault_kms_key_arn" {
  description = "ARN of a KMS key to encrypt the vault with. Leave null to use the key AWS manages for AWS Backup."
  type        = string
  default     = null
}

resource "aws_backup_vault" "this" {
  name        = var.name
  kms_key_arn = var.vault_kms_key_arn

  # With force_destroy off, deleting a vault that still holds recovery points
  # fails. To tear down a test, set this to true, apply, then destroy.
  force_destroy = false
}

data "aws_iam_policy_document" "assume" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]

    # Only the AWS Backup service can use this role.
    principals {
      type        = "Service"
      identifiers = ["backup.amazonaws.com"]
    }
  }
}

resource "aws_iam_role" "backup" {
  name               = "${var.name}-aws-backup"
  assume_role_policy = data.aws_iam_policy_document.assume.json
}

# The two policies AWS maintains for this purpose: one to take backups, one to
# restore them. Without the second, the first restore you try fails.
resource "aws_iam_role_policy_attachment" "backup" {
  role       = aws_iam_role.backup.name
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
}

resource "aws_iam_role_policy_attachment" "restore" {
  role       = aws_iam_role.backup.name
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForRestores"
}

resource "aws_backup_plan" "this" {
  name = var.name

  rule {
    rule_name         = "daily"
    target_vault_name = aws_backup_vault.this.name
    schedule          = var.schedule

    # The job must start within one hour of the schedule and finish within
    # six, or it is marked as failed instead of running into the working day.
    start_window      = 60
    completion_window = 360

    lifecycle {
      delete_after = var.retention_days
    }
  }
}

# A resource is backed up when it matches resource_arns AND carries the tag.
# New databases join the plan by getting the tag, with no change here.
resource "aws_backup_selection" "tagged" {
  name         = "tagged-${var.backup_tag_value}"
  plan_id      = aws_backup_plan.this.id
  iam_role_arn = aws_iam_role.backup.arn
  resources    = var.resource_arns

  selection_tag {
    type  = "STRINGEQUALS"
    key   = var.backup_tag_key
    value = var.backup_tag_value
  }

  # The role needs its policies before the first job runs.
  depends_on = [
    aws_iam_role_policy_attachment.backup,
    aws_iam_role_policy_attachment.restore,
  ]
}

output "vault_name" {
  description = "Where recovery points are stored. Look here to confirm backups are arriving."
  value       = aws_backup_vault.this.name
}

output "vault_arn" {
  description = "ARN of the backup vault."
  value       = aws_backup_vault.this.arn
}

output "backup_role_arn" {
  description = "Role AWS Backup uses. Choose it when you start a restore."
  value       = aws_iam_role.backup.arn
}

output "tag_to_apply" {
  description = "Add this tag to a database to include it in the plan."
  value       = "${var.backup_tag_key} = ${var.backup_tag_value}"
}

Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.

Written by DevOps Hive. Last updated 2026-11-27.

More in recover