Skip to content

Commit

Permalink
Backup remote vault initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-sproston committed Jun 25, 2024
1 parent 3c501ed commit 0cc5072
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
36 changes: 36 additions & 0 deletions modules/vault/README.md
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
}
```
6 changes: 6 additions & 0 deletions modules/vault/data.tf
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
}
92 changes: 92 additions & 0 deletions modules/vault/main.tf
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
}
6 changes: 6 additions & 0 deletions modules/vault/outputs.tf
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
}
17 changes: 17 additions & 0 deletions modules/vault/variables.tf
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
}

0 comments on commit 0cc5072

Please sign in to comment.