Skip to content

Commit 280cd79

Browse files
adding Backup EFS
1 parent 1148e2d commit 280cd79

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

_variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ variable "alarm_sns_topics" {
7979
default = []
8080
description = "Alarm topics to create and alert on ECS instance metrics"
8181
}
82+
83+
84+
variable "expire_backup_efs" {
85+
default = 0
86+
description = "Number of days the backup will be expired"
87+
}

efs-backup.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
resource "aws_backup_vault" "nfs" {
2+
count = "${var.expire_backup_efs > 0 ? 1 : 0}"
3+
name = "vault-${var.name}"
4+
}
5+
6+
resource "aws_backup_plan" "bkp_efs_plan" {
7+
count = "${var.expire_backup_efs > 0 ? 1 : 0}"
8+
name = "Backup-${var.name}"
9+
10+
rule {
11+
rule_name = "Daily-Backup-${var.name}"
12+
target_vault_name = "${aws_backup_vault.nfs.*.name[count.index]}"
13+
schedule = "cron(0 22 * * ? *)"
14+
lifecycle{
15+
delete_after = "${var.expire_backup_efs}"
16+
}
17+
18+
}
19+
}
20+
21+
resource "aws_iam_role" "efs_backup_role" {
22+
count = "${var.expire_backup_efs > 0 ? 1 : 0}"
23+
24+
name = "Backup-EFS-${var.name}"
25+
assume_role_policy = <<POLICY
26+
{
27+
"Version": "2012-10-17",
28+
"Statement": [
29+
{
30+
"Action": ["sts:AssumeRole"],
31+
"Effect": "allow",
32+
"Principal": {
33+
"Service": ["backup.amazonaws.com"]
34+
}
35+
}
36+
]
37+
}
38+
POLICY
39+
}
40+
41+
resource "aws_backup_selection" "example" {
42+
count = "${var.expire_backup_efs > 0 ? 1 : 0}"
43+
44+
iam_role_arn = "${aws_iam_role.efs_backup_role.*.arn[count.index]}"
45+
name = "Backup-efs-${var.name}"
46+
plan_id = "${aws_backup_plan.bkp_efs_plan.*.id[count.index]}"
47+
resources = [
48+
"${aws_efs_file_system.ecs.arn}",
49+
]
50+
}

0 commit comments

Comments
 (0)