2024-04-29 19:02:31 +08:00
|
|
|
# build local data structure
|
|
|
|
|
|
|
|
locals {
|
|
|
|
backup-config = {
|
|
|
|
"Aurora" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.Aurora.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:rds:*:*:cluster:*"
|
|
|
|
}
|
|
|
|
"DynamoDB" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.DynamoDB.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:dynamodb:*:*:table/*"
|
|
|
|
}
|
|
|
|
"EBS" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.EBS.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:ec2:*:*:volume/*"
|
|
|
|
}
|
|
|
|
"EC2" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.EC2.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:ec2:*:*:instance/*"
|
|
|
|
}
|
|
|
|
"EFS" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.EFS.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:elasticfilesystem:*:*:file-system/*"
|
|
|
|
}
|
|
|
|
"FSx" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.FSx.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:*:fsx:*"
|
|
|
|
}
|
|
|
|
"Redshift" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.Redshift.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:redshift:*:*:cluster:*"
|
|
|
|
}
|
|
|
|
"RDS" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.RDS.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:rds:*:*:db:*"
|
|
|
|
}
|
|
|
|
# this version can't handle space
|
|
|
|
# "Storage Gateway" : {
|
|
|
|
# enabled = var.opt-in-storagegateway
|
|
|
|
# arn-prefix = "arn:aws:storagegateway:*:*:gateway/*"
|
|
|
|
# }
|
|
|
|
"VirtualMachine" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.VirtualMachine.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:backup-gateway:*:*:vm/*"
|
|
|
|
}
|
|
|
|
"S3" : {
|
2024-04-30 09:58:58 +08:00
|
|
|
enabled = var.service-opt-in.S3.enabled
|
2024-04-29 19:02:31 +08:00
|
|
|
arn-prefix = "arn:aws:s3:::*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-29 14:25:25 +08:00
|
|
|
resource "aws_backup_region_settings" "ab-settings" {
|
|
|
|
resource_type_opt_in_preference = {
|
2024-04-30 10:03:48 +08:00
|
|
|
for k, v in local.backup-config : k => v.enabled
|
2024-04-29 14:25:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_backup_vault" "ab-vault" {
|
|
|
|
for_each = toset([
|
2024-04-29 19:02:31 +08:00
|
|
|
for k, v in local.backup-config : k
|
|
|
|
if v.enabled
|
2024-04-29 14:25:25 +08:00
|
|
|
])
|
|
|
|
name = "BackupVault-${each.value}"
|
|
|
|
kms_key_arn = aws_kms_key.ab-kms-key.arn
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_backup_vault_policy" "ab-vault-policy" {
|
|
|
|
for_each = aws_backup_vault.ab-vault
|
|
|
|
backup_vault_name = each.value.name
|
|
|
|
policy = jsonencode(
|
|
|
|
{
|
|
|
|
"Version" : "2012-10-17",
|
|
|
|
"Id" : "default",
|
|
|
|
"Statement" : [
|
|
|
|
{
|
|
|
|
"Sid" : "default",
|
|
|
|
"Effect" : "Allow",
|
|
|
|
"Principal" : {
|
|
|
|
"AWS" : data.aws_caller_identity.this.account_id
|
|
|
|
},
|
|
|
|
"Action" : [
|
|
|
|
"backup:DescribeBackupVault",
|
|
|
|
"backup:DeleteBackupVault",
|
|
|
|
"backup:PutBackupVaultAccessPolicy",
|
|
|
|
"backup:DeleteBackupVaultAccessPolicy",
|
|
|
|
"backup:GetBackupVaultAccessPolicy",
|
|
|
|
"backup:StartBackupJob",
|
|
|
|
"backup:GetBackupVaultNotifications",
|
|
|
|
"backup:PutBackupVaultNotifications"
|
|
|
|
],
|
|
|
|
"Resource" : each.value.arn
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_backup_plan" "ab-plan" {
|
|
|
|
for_each = aws_backup_vault.ab-vault
|
|
|
|
name = "BackupPlan-${replace(each.value.name, "BackupVault-", "")}"
|
|
|
|
|
2024-04-30 10:19:12 +08:00
|
|
|
# daily backup
|
2024-04-29 14:25:25 +08:00
|
|
|
rule {
|
2024-04-30 10:19:12 +08:00
|
|
|
rule_name = "Daily"
|
2024-04-29 14:25:25 +08:00
|
|
|
target_vault_name = each.value.name
|
2024-04-30 10:19:12 +08:00
|
|
|
schedule = var.daily-backup-cron
|
2024-04-29 14:25:25 +08:00
|
|
|
start_window = 60
|
|
|
|
completion_window = 240
|
|
|
|
|
|
|
|
lifecycle {
|
2024-04-30 10:19:12 +08:00
|
|
|
delete_after = var.daily-backup-retention
|
2024-04-29 14:25:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
recovery_point_tags = {
|
|
|
|
"CreatedBy" : "AWSBackup"
|
2024-04-30 10:19:12 +08:00
|
|
|
"AWSBackupPlan" : "BackupPlan-${replace(each.value.name, "BackupVault-", "")}-Daily"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 10:54:57 +08:00
|
|
|
# monthly backup (when overlap with daily, only monthly backup will be created.
|
|
|
|
# see https://docs.aws.amazon.com/aws-backup/latest/devguide/creating-a-backup-plan.html)
|
2024-04-30 10:19:12 +08:00
|
|
|
rule {
|
|
|
|
rule_name = "Monthly"
|
|
|
|
target_vault_name = each.value.name
|
|
|
|
schedule = var.monthly-backup-cron
|
|
|
|
start_window = 60
|
|
|
|
completion_window = 240
|
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
delete_after = var.monthly-backup-retention
|
2024-04-30 10:54:57 +08:00
|
|
|
cold_storage_after = var.daily-backup-retention # move to cold storage after daily retention
|
2024-04-30 10:19:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
recovery_point_tags = {
|
|
|
|
"CreatedBy" : "AWSBackup"
|
|
|
|
"AWSBackupPlan" : "BackupPlan-${replace(each.value.name, "BackupVault-", "")}-Monthly"
|
2024-04-29 14:25:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# advanced_backup_setting {
|
|
|
|
# backup_options = {
|
|
|
|
# WindowsVSS = "enabled"
|
|
|
|
# }
|
|
|
|
# resource_type = "EC2"
|
|
|
|
# }
|
|
|
|
}
|
|
|
|
#
|
|
|
|
resource "aws_iam_role" "ab-iam-role" {
|
|
|
|
name = "AwsBackupRole"
|
|
|
|
assume_role_policy = jsonencode(
|
|
|
|
{
|
|
|
|
"Version" : "2012-10-17",
|
|
|
|
"Statement" : [
|
|
|
|
{
|
|
|
|
"Action" : ["sts:AssumeRole"],
|
|
|
|
"Effect" : "allow",
|
|
|
|
"Principal" : {
|
|
|
|
"Service" : ["backup.amazonaws.com"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy_attachment" "ab-iam-role-policy" {
|
|
|
|
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
|
|
|
|
role = aws_iam_role.ab-iam-role.name
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_backup_selection" "ab-selection-by-service-type" {
|
|
|
|
for_each = aws_backup_plan.ab-plan
|
|
|
|
iam_role_arn = aws_iam_role.ab-iam-role.arn
|
|
|
|
name = "SelectionByServiceType"
|
|
|
|
plan_id = each.value.id
|
2024-04-29 19:02:31 +08:00
|
|
|
resources = [lookup(local.backup-config, replace(each.value.name, "BackupPlan-", "")).arn-prefix]
|
2024-04-29 14:25:25 +08:00
|
|
|
}
|