terraform.aws-baseline-infra/modules/storage/aws-backup/main.tf

177 lines
4.9 KiB
Terraform
Raw Normal View History

# build local data structure
locals {
backup-config = {
"Aurora" : {
enabled = var.service-opt-in.Aurora.enabled
arn-prefix = "arn:aws:rds:*:*:cluster:*"
}
"DynamoDB" : {
enabled = var.service-opt-in.DynamoDB.enabled
arn-prefix = "arn:aws:dynamodb:*:*:table/*"
}
"EBS" : {
enabled = var.service-opt-in.EBS.enabled
arn-prefix = "arn:aws:ec2:*:*:volume/*"
}
"EC2" : {
enabled = var.service-opt-in.EC2.enabled
arn-prefix = "arn:aws:ec2:*:*:instance/*"
}
"EFS" : {
enabled = var.service-opt-in.EFS.enabled
arn-prefix = "arn:aws:elasticfilesystem:*:*:file-system/*"
}
"FSx" : {
enabled = var.service-opt-in.FSx.enabled
arn-prefix = "arn:*:fsx:*"
}
"Redshift" : {
enabled = var.service-opt-in.Redshift.enabled
arn-prefix = "arn:aws:redshift:*:*:cluster:*"
}
"RDS" : {
enabled = var.service-opt-in.RDS.enabled
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" : {
enabled = var.service-opt-in.VirtualMachine.enabled
arn-prefix = "arn:aws:backup-gateway:*:*:vm/*"
}
"S3" : {
enabled = var.service-opt-in.S3.enabled
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 = {
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([
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"
}
}
# 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
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
resources = [lookup(local.backup-config, replace(each.value.name, "BackupPlan-", "")).arn-prefix]
2024-04-29 14:25:25 +08:00
}