|
| 1 | +// Create the AWS Backup Vault and plans |
| 2 | + |
| 3 | +// The vault |
| 4 | + |
| 5 | +resource "aws_backup_vault" "remote_backup_vault" { |
| 6 | + name = "${var.instance_name}-remote_backup" |
| 7 | + kms_key_arn = aws_kms_key.remote_backup_vault.arn |
| 8 | +} |
| 9 | + |
| 10 | +// The policy to use for the vault allowing the remote AWS account to copy snapshots |
| 11 | +// back in case of incidents |
| 12 | + |
| 13 | +resource "aws_backup_vault_policy" "remote_backup" { |
| 14 | + backup_vault_name = aws_backup_vault.remote_backup_vault.name |
| 15 | + |
| 16 | + policy = <<POLICY |
| 17 | +{ |
| 18 | + "Version": "2012-10-17", |
| 19 | + "Statement": [ |
| 20 | + { |
| 21 | + "Effect": "Allow", |
| 22 | + "Action": "backup:CopyIntoBackupVault", |
| 23 | + "Resource": "*", |
| 24 | + "Principal": { |
| 25 | + "AWS": [ |
| 26 | + "arn:aws:iam::${var.remote_account}:root" |
| 27 | + ] |
| 28 | + } |
| 29 | + } |
| 30 | + ] |
| 31 | +} |
| 32 | +POLICY |
| 33 | +} |
| 34 | + |
| 35 | +// The backup plan which automatically copies snapshots off-account |
| 36 | + |
| 37 | +resource "aws_backup_plan" "remote_backup" { |
| 38 | + name = "${var.instance_name}-remote_backup" |
| 39 | + |
| 40 | + rule { |
| 41 | + rule_name = "${var.instance_name}-remote_backup" |
| 42 | + target_vault_name = aws_backup_vault.remote_backup_vault.name |
| 43 | + schedule = var.backup_schedule |
| 44 | + |
| 45 | + lifecycle { |
| 46 | + delete_after = var.local_lifecycle |
| 47 | + } |
| 48 | + copy_action { |
| 49 | + destination_vault_arn = var.remote_vault_arn |
| 50 | + |
| 51 | + lifecycle { |
| 52 | + delete_after = var.remote_lifecycle |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// A selection which backs up ALL assets in the AWS account which have |
| 59 | +// the tag BackupRemote=True |
| 60 | + |
| 61 | +resource "aws_backup_selection" "remote_backup_account" { |
| 62 | + count = var.use_env ? 0 : 1 |
| 63 | + iam_role_arn = aws_iam_role.remote_backup.arn |
| 64 | + name = "${var.instance_name}-remote-backup-account" |
| 65 | + plan_id = aws_backup_plan.remote_backup.id |
| 66 | + resources = ["*"] |
| 67 | + |
| 68 | + condition { |
| 69 | + string_equals { |
| 70 | + key = "aws:ResourceTag/BackupRemote" |
| 71 | + value = "true" |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// A selection which backs up assets in the AWS account which have |
| 77 | +// the tag BackupRemote=True AND the requested Environment tag. |
| 78 | + |
| 79 | +resource "aws_backup_selection" "remote_backup_env" { |
| 80 | + count = var.use_env ? 1 : 0 |
| 81 | + iam_role_arn = aws_iam_role.remote_backup.arn |
| 82 | + name = "${var.instance_name}-remote-backup-env" |
| 83 | + plan_id = aws_backup_plan.remote_backup.id |
| 84 | + resources = ["*"] |
| 85 | + |
| 86 | + condition { |
| 87 | + string_equals { |
| 88 | + key = "aws:ResourceTag/BackupRemote" |
| 89 | + value = "true" |
| 90 | + } |
| 91 | + string_equals { |
| 92 | + key = "aws:ResourceTag/Environment" |
| 93 | + value = var.environment |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments