generated from rhythmictech/terraform-terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup.tf
132 lines (103 loc) · 3.38 KB
/
backup.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
locals {
create_backup_resources = var.enable_backups
create_backup_kms_key = local.create_backup_resources && var.backup_kms_key_id == null
kms_key_arn = local.create_backup_kms_key ? aws_kms_key.backup[0].arn : var.backup_kms_key_id
}
data "aws_iam_policy_document" "assume_backup" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["backup.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "backup" {
statement {
effect = "Allow"
actions = [
"elasticfilesystem:Backup",
"elasticfilesystem:Restore",
"elasticfilesystem:CreateFilesystem",
"elasticfilesystem:DescribeFilesystems",
"elasticfilesystem:DeleteFilesystem",
"elasticfilesystem:DescribeTags"
]
# This will not actually use the default file system, this just prevents various errors
# when backup is not enabled
resources = [
try(aws_efs_file_system.this[0].arn, "arn:aws:elasticfilesystem:*:*:file-system/*")
]
}
statement {
actions = ["tag:GetResources"]
effect = "Allow"
resources = ["*"]
}
statement {
effect = "Allow"
actions = [
"backup:DescribeBackupVault",
"backup:CopyIntoBackupVault"
]
# This will not actually use the default file system, this just prevents various errors
# when backup is not enabled
resources = [
try(aws_backup_vault.backup[0].arn, "arn:aws:backup:*:*:backup-vault:*")
]
}
}
resource "aws_iam_role" "backup" {
count = local.create_backup_resources ? 1 : 0
name_prefix = substr("${var.name}-backup", 0, 32)
assume_role_policy = data.aws_iam_policy_document.assume_backup.json
permissions_boundary = var.backup_role_permissions_boundary
tags = var.additional_tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_iam_role_policy" "backup" {
count = local.create_backup_resources ? 1 : 0
name_prefix = "${var.name}-backup"
role = aws_iam_role.backup[0].id
policy = data.aws_iam_policy_document.backup.json
}
resource "aws_kms_key" "backup" {
count = local.create_backup_kms_key ? 1 : 0
description = "KMS Key for EFS Backups (${aws_efs_file_system.this[0].id})"
enable_key_rotation = true
tags = var.additional_tags
}
resource "aws_backup_vault" "backup" {
count = local.create_backup_resources ? 1 : 0
name = "${var.name}-vault"
kms_key_arn = local.kms_key_arn
tags = var.additional_tags
}
resource "aws_backup_plan" "backup" {
count = local.create_backup_resources ? 1 : 0
name = "${var.name}-plan"
tags = var.additional_tags
rule {
rule_name = "${var.name}-cron-rule"
target_vault_name = aws_backup_vault.backup[0].name
schedule = var.backup_schedule
dynamic "lifecycle" {
for_each = (var.backup_lifecycle_delete_after != null || var.backup_lifecycle_cold_storage_after != null) ? ["true"] : []
content {
delete_after = var.backup_lifecycle_delete_after
cold_storage_after = var.backup_lifecycle_cold_storage_after
}
}
}
}
resource "aws_backup_selection" "backup" {
count = local.create_backup_resources ? 1 : 0
name = "${var.name}-selection"
iam_role_arn = aws_iam_role.backup[0].arn
plan_id = aws_backup_plan.backup[0].id
resources = [
aws_efs_file_system.this[0].arn
]
}