-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.tf
155 lines (126 loc) · 5.21 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#####
# Backup Vault
#####
resource "aws_backup_vault" "main" {
count = var.vault_name != null ? 1 : 0
name = var.vault_name
kms_key_arn = var.vault_kms_key_arn
force_destroy = var.vault_force_destroy
tags = var.tags
}
#####
# Backup Plan
#####
resource "aws_backup_plan" "main" {
name = var.plan_name
dynamic "rule" {
for_each = var.rules
content {
rule_name = lookup(rule.value, "name")
target_vault_name = var.vault_name != null ? aws_backup_vault.main[0].name : lookup(rule.value, "target_vault_name", "Default")
schedule = lookup(rule.value, "schedule", null)
start_window = lookup(rule.value, "start_window", null)
completion_window = lookup(rule.value, "completion_window", null)
enable_continuous_backup = lookup(rule.value, "enable_continuous_backup", false)
recovery_point_tags = length(lookup(rule.value, "recovery_point_tags")) == 0 ? var.tags : lookup(rule.value, "recovery_point_tags")
dynamic "lifecycle" {
for_each = length(lookup(rule.value, "lifecycle")) == 0 ? [] : [lookup(rule.value, "lifecycle", {})]
content {
cold_storage_after = lookup(lifecycle.value, "cold_storage_after", 0)
delete_after = lookup(lifecycle.value, "delete_after", 90)
}
}
dynamic "copy_action" {
for_each = length(lookup(rule.value, "copy_action", {})) == 0 ? [] : [lookup(rule.value, "copy_action", {})]
content {
destination_vault_arn = lookup(copy_action.value, "destination_vault_arn", null)
dynamic "lifecycle" {
for_each = length(lookup(copy_action.value, "lifecycle", {})) == 0 ? [] : [lookup(copy_action.value, "lifecycle", {})]
content {
cold_storage_after = lookup(lifecycle.value, "copy_action_cold_storage_after", 0)
delete_after = lookup(lifecycle.value, "copy_action_delete_after", 90)
}
}
}
}
}
}
dynamic "advanced_backup_setting" {
for_each = var.advanced_backup_settings
content {
resource_type = lookup(advanced_backup_setting.value, "resource_type", "EC2")
backup_options = lookup(advanced_backup_setting.value, "backup_options", null)
}
}
tags = var.tags
}
#####
# Backup Selection
#####
resource "aws_backup_selection" "main" {
iam_role_arn = aws_iam_role.main.arn
name = var.selection_name
plan_id = aws_backup_plan.main.id
resources = var.selection_resources
not_resources = var.selection_not_resources
dynamic "selection_tag" {
for_each = var.selection_tags
content {
type = lookup(selection_tag.value, "type", null)
key = lookup(selection_tag.value, "key", null)
value = lookup(selection_tag.value, "value", null)
}
}
}
#######
# SNS Backup Notifications
#######
resource "aws_sns_topic" "main" {
count = var.create_sns_topic && var.enable_sns_notifications ? 1 : 0
name = var.vault_name != null ? "${aws_backup_vault.main[0].name}-events" : "backup-vault-events"
kms_master_key_id = var.vault_sns_kms_key_arn
}
resource "aws_sns_topic_policy" "main" {
count = var.enable_sns_notifications ? 1 : 0
arn = var.create_sns_topic ? aws_sns_topic.main[0].arn : var.sns_topic_arn
policy = data.aws_iam_policy_document.sns_policy[0].json
}
resource "aws_backup_vault_notifications" "main" {
count = var.enable_sns_notifications ? 1 : 0
backup_vault_name = var.vault_name != null ? aws_backup_vault.main[0].name : "Default"
sns_topic_arn = var.create_sns_topic ? aws_sns_topic.main[0].arn : var.sns_topic_arn
backup_vault_events = var.backup_vault_events
}
#####
# IAM Role
#####
resource "aws_iam_role" "main" {
name = var.iam_role_name != null ? var.iam_role_name : "aws-backup-plan-${var.plan_name}-role"
assume_role_policy = data.aws_iam_policy_document.main.json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "main_role_backup_policy_attach" {
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
role = aws_iam_role.main.name
}
resource "aws_iam_role_policy_attachment" "main_role_restore_policy_attach" {
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForRestores"
role = aws_iam_role.main.name
}
resource "aws_iam_role_policy_attachment" "main_role_s3_backup_policy_attach" {
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSBackupServiceRolePolicyForS3Backup"
role = aws_iam_role.main.name
}
resource "aws_iam_role_policy_attachment" "main_role_s3_restore_policy_attach" {
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSBackupServiceRolePolicyForS3Restore"
role = aws_iam_role.main.name
}
resource "aws_iam_policy" "main_custom_policy" {
description = "AWS Backup Tag policy"
policy = data.aws_iam_policy_document.main_custom_policy.json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "main_custom_policy_attach" {
policy_arn = aws_iam_policy.main_custom_policy.arn
role = aws_iam_role.main.name
}