generated from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c501ed
commit 0cc5072
Showing
5 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Terraform module: vault | ||
|
||
## Description | ||
|
||
This is a simple module to create a AWS Backup vault set up to act as a destination for | ||
remote off-account AWS backup copy jobs. The vault can be "locked" which prevents | ||
pre-mature backup snapshot deletion. | ||
|
||
The vault should be located in an isolated AWS account | ||
|
||
**WARNING** Once a vault is locked you have 8 days to reverse the setting. Once this | ||
cool-off period has been passed vault locking can not be removed. | ||
|
||
## Module parameters | ||
|
||
|Name|Description|Type|Default setting| | ||
|client_name|The name of the client being served|string|-| | ||
|client_account|The AWS Account ID number being served|string|-| | ||
|lock_vault|Whether to lock the vault|bool|false| | ||
|
||
## Sample usage | ||
|
||
This snippet creates a locked vault for RSS prod backup called rss-prod. The AWS account | ||
number "123456789012" is the only account which can copy backup snapshots into this | ||
vault. (Only one account is allowed to copy into each vault so as to ensure data | ||
segregation). | ||
|
||
``` | ||
module "rss_prod_prod_backup_vault" { | ||
source = "../modules/vault" | ||
client_name = "rss-prod" | ||
client_account = "123456789012" | ||
lock_vault = true | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
data "aws_caller_identity" "current" { | ||
} | ||
|
||
locals { | ||
service_account = data.aws_caller_identity.current.account_id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
resource "aws_backup_vault" "backup" { | ||
name = "${replace(var.client_name, "-", "_")}_backup" | ||
kms_key_arn = aws_kms_key.backup.arn | ||
} | ||
|
||
resource "aws_backup_vault_lock_configuration" "backup" { | ||
count = var.lock_vault ? 1 : 0 | ||
backup_vault_name = aws_backup_vault.backup.name | ||
changeable_for_days = 8 | ||
} | ||
|
||
resource "aws_backup_vault_policy" "backup" { | ||
backup_vault_name = aws_backup_vault.backup.name | ||
|
||
policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": "backup:CopyIntoBackupVault", | ||
"Resource": "*", | ||
"Principal": { | ||
"AWS": [ | ||
"arn:aws:iam::${var.client_account}:root" | ||
] | ||
} | ||
} | ||
] | ||
} | ||
POLICY | ||
} | ||
|
||
resource "aws_kms_alias" "backup" { | ||
name = "alias/backup-vault-key/${var.client_name}" | ||
target_key_id = aws_kms_key.backup.key_id | ||
} | ||
|
||
resource "aws_kms_key" "backup" { | ||
description = "${var.client_name} Backup Vault Key" | ||
|
||
policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Id": "key-default-plus", | ||
"Statement": [ | ||
{ | ||
"Sid": "Enable IAM User Permissions", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "arn:aws:iam::${local.service_account}:root" | ||
}, | ||
"Action": "kms:*", | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow access from remote backup account", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "arn:aws:iam::${var.client_account}:root" | ||
}, | ||
"Action": [ | ||
"kms:Encrypt", | ||
"kms:Decrypt", | ||
"kms:ReEncrypt*", | ||
"kms:GenerateDataKey*", | ||
"kms:DescribeKey" | ||
], | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow attachment of persistant resources", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "arn:aws:iam::${var.client_account}:root" | ||
}, | ||
"Action": [ | ||
"kms:CreateGrant", | ||
"kms:ListGrants", | ||
"kms:RevokeGrant" | ||
], | ||
"Resource": "*", | ||
"Condition": { | ||
"Bool": { | ||
"kms:GrantIsForAWSResource": "true" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
POLICY | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Output variable definitions | ||
|
||
output "vault_arn" { | ||
description = "The vault ARN" | ||
value = aws_backup_vault.backup.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Input variable definitions | ||
|
||
variable "client_name" { | ||
description = "The name of the client being served" | ||
type = string | ||
} | ||
|
||
variable "client_account" { | ||
description = "The AWS Account ID number being served" | ||
type = string | ||
} | ||
|
||
variable "lock_vault" { | ||
description = "Whether to lock the vault" | ||
type = bool | ||
default = false | ||
} |