-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.tf
59 lines (49 loc) · 1.47 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
data "aws_iam_policy_document" "assume_backup" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["backup.amazonaws.com"]
}
}
}
resource "aws_iam_role" "backup" {
count = var.enable_efs_backups ? 1 : 0
name_prefix = var.name
assume_role_policy = data.aws_iam_policy_document.assume_backup.json
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_iam_role_policy_attachment" "backup" {
count = var.enable_efs_backups ? 1 : 0
role = aws_iam_role.backup[0].name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
}
resource "aws_backup_vault" "this" {
count = var.enable_efs_backups ? 1 : 0
name = var.efs_backup_vault_name
tags = var.tags
}
# AWS Backup plan
resource "aws_backup_plan" "this" {
count = var.enable_efs_backups ? 1 : 0
name = "${var.name}-backup"
tags = var.tags
rule {
rule_name = "${var.name}-EFS-backup"
schedule = var.efs_backup_schedule
target_vault_name = var.efs_backup_vault_name
lifecycle {
delete_after = var.efs_backup_retain_days
}
}
}
resource "aws_backup_selection" "this" {
count = var.enable_efs_backups ? 1 : 0
name = "${var.name}-EFS-backup"
iam_role_arn = aws_iam_role.backup[0].arn
plan_id = aws_backup_plan.this[0].id
resources = [aws_efs_file_system.this[0].arn]
}