Skip to content

Commit 47d897d

Browse files
committed
feature/PI-407-immutable_backups Enable backups on source account to the vault
1 parent 446545b commit 47d897d

File tree

3 files changed

+119
-7
lines changed

3 files changed

+119
-7
lines changed

infrastructure/terraform/per_account/backups/aws-backups.tf

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
data "aws_caller_identity" "current" {}
2-
31
data "aws_secretsmanager_secret" "source_account_id" {
42
name = "backups-source-account-id"
53
}
@@ -8,9 +6,6 @@ data "aws_secretsmanager_secret_version" "source_account_id" {
86
secret_id = data.aws_secretsmanager_secret.source_account_id.id
97
}
108

11-
output "account_id" {
12-
value = data.aws_caller_identity.current.account_id
13-
}
149

1510
# We need a key for the backup vaults. This key will be used to encrypt the backups themselves.
1611
# We need one per vault (on the assumption that each vault will be in a different account).
@@ -24,7 +19,7 @@ module "destination" {
2419
source = "../../modules/aws-backup-destination"
2520

2621
source_account_name = "test" # please note that the assigned value would be the prefix in aws_backup_vault.vault.name - change to dev/prod
27-
account_id = data.aws_caller_identity.current.account_id
22+
account_id = var.assume_account
2823
source_account_id = data.aws_secretsmanager_secret_version.source_account_id.secret_string
2924
kms_key = aws_kms_key.destination_backup_key.arn
3025
enable_vault_protection = false
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
data "aws_secretsmanager_secret" "destination_vault_arn" {
2+
name = "destination_vault_arn"
3+
}
4+
5+
data "aws_secretsmanager_secret_version" "destination_vault_arn" {
6+
secret_id = data.aws_secretsmanager_secret.destination_vault_arn.id
7+
}
8+
9+
data "aws_secretsmanager_secret" "destination_account_id" {
10+
name = "destination_account_id"
11+
}
12+
13+
data "aws_secretsmanager_secret_version" "destination_account_id" {
14+
secret_id = data.aws_secretsmanager_secret.destination_account_id.id
15+
}
16+
17+
# First, we create an S3 bucket for compliance reports. You may already have a module for creating
18+
# S3 buckets with more refined access rules, which you may prefer to use.
19+
20+
resource "aws_s3_bucket" "backup_reports" {
21+
bucket_prefix = "${local.project}-backup-reports"
22+
}
23+
24+
# Now we have to configure access to the report bucket.
25+
26+
resource "aws_s3_bucket_ownership_controls" "backup_reports" {
27+
bucket = aws_s3_bucket.backup_reports.id
28+
rule {
29+
object_ownership = "BucketOwnerPreferred"
30+
}
31+
}
32+
33+
resource "aws_s3_bucket_acl" "backup_reports" {
34+
depends_on = [aws_s3_bucket_ownership_controls.backup_reports]
35+
36+
bucket = aws_s3_bucket.backup_reports.id
37+
acl = "private"
38+
}
39+
40+
# We need a key for the SNS topic that will be used for notifications from AWS Backup. This key
41+
# will be used to encrypt the messages sent to the topic before they are sent to the subscribers,
42+
# but isn't needed by the recipients of the messages.
43+
44+
45+
# Now we can define the key itself
46+
resource "aws_kms_key" "backup_notifications" {
47+
description = "KMS key for AWS Backup notifications"
48+
deletion_window_in_days = 7
49+
enable_key_rotation = true
50+
policy = jsonencode({
51+
Version = "2012-10-17"
52+
Statement = [
53+
{
54+
Effect = "Allow"
55+
Sid = "Enable IAM User Permissions"
56+
Principal = {
57+
AWS = "arn:aws:iam::${var.assume_account}:root"
58+
}
59+
Action = "kms:*"
60+
Resource = "*"
61+
},
62+
{
63+
Effect = "Allow"
64+
Principal = {
65+
Service = "sns.amazonaws.com"
66+
}
67+
Action = ["kms:GenerateDataKey*", "kms:Decrypt"]
68+
Resource = "*"
69+
},
70+
]
71+
})
72+
}
73+
74+
# Now we can deploy the source and destination modules, referencing the resources we've created above.
75+
76+
module "source" {
77+
source = "../../modules/aws-backup-source"
78+
79+
backup_copy_vault_account_id = data.aws_secretsmanager_secret_version.destination_account_id.secret_string
80+
backup_copy_vault_arn = data.aws_secretsmanager_secret_version.destination_vault_arn.secret_string
81+
environment_name = var.environment
82+
bootstrap_kms_key_arn = aws_kms_key.backup_notifications.arn
83+
project_name = local.project
84+
reports_bucket = aws_s3_bucket.backup_reports.bucket
85+
terraform_role_arn = data.aws_caller_identity.current.arn
86+
87+
backup_plan_config = {
88+
"compliance_resource_types" : [
89+
"S3"
90+
],
91+
"rules" : [
92+
{
93+
"copy_action" : {
94+
"delete_after" : 4
95+
},
96+
"lifecycle" : {
97+
"delete_after" : 2
98+
},
99+
"name" : "daily_kept_for_2_days",
100+
"schedule" : "cron(0 0 * * ? *)"
101+
}
102+
],
103+
"selection_tag" : "NHSE-Enable-Backup"
104+
}
105+
# # Note here that we need to explicitly disable DynamoDB backups in the source account.
106+
# # The default config in the module enables backups for all resource types.
107+
# backup_plan_config_dynamodb = {
108+
# "compliance_resource_types" : [
109+
# "DynamoDB"
110+
# ],
111+
# "rules" : [
112+
# ],
113+
# "enable" : false,
114+
# "selection_tag" : "NHSE-Enable-Backup"
115+
# }
116+
}

infrastructure/terraform/per_workspace/modules/api_storage/dynamodb.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module "dynamodb_table" {
1616
point_in_time_recovery_enabled = true
1717

1818
tags = {
19-
Name = var.name
19+
Name = var.name
20+
"NHSE-Enable-Backup" = "True" # will this work? Only needed for 1 environment? only tag in one account
2021
}
2122

2223
}

0 commit comments

Comments
 (0)