-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
102 lines (88 loc) · 2.16 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
module "labels" {
source = "clouddrove/labels/aws"
version = "1.3.0"
name = var.name
repository = var.repository
environment = var.environment
managedby = var.managedby
attributes = var.attributes
label_order = var.label_order
}
resource "aws_iam_role" "dlm_lifecycle_role" {
count = var.create_lifecycle_policy == true ? 1 : 0
name = format("%s-role", module.labels.id)
tags = module.labels.tags
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "dlm.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "dlm_lifecycle" {
count = var.create_lifecycle_policy == true ? 1 : 0
name = module.labels.id
role = join("", aws_iam_role.dlm_lifecycle_role.*.id)
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:CreateSnapshot",
"ec2:CreateSnapshots",
"ec2:DeleteSnapshot",
"ec2:DescribeInstances",
"ec2:DescribeVolumes",
"ec2:DescribeSnapshots"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ec2:CreateTags"
],
"Resource": "arn:aws:ec2:*::snapshot/*"
}
]
}
EOF
}
resource "aws_dlm_lifecycle_policy" "main" {
count = var.create_lifecycle_policy == true ? 1 : 0
description = "DLM lifecycle policy"
execution_role_arn = join("", aws_iam_role.dlm_lifecycle_role.*.arn)
state = "ENABLED"
policy_details {
resource_types = var.resource_types
schedule {
name = module.labels.id
create_rule {
interval = var.interval
interval_unit = var.interval_unit
times = var.times
}
retain_rule {
count = var.count-number
}
tags_to_add = {
SnapshotCreator = var.SnapshotCreator
}
copy_tags = var.copy_tags
}
target_tags = var.target_tags
}
tags = module.labels.tags
tags_all = module.labels.tags
}