forked from chef-boneyard/lambda_ebs_snapshot
-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.tf
192 lines (165 loc) · 5.38 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
locals {
module_relpath = substr(path.module, length(path.cwd) + 1, -1)
}
data "aws_iam_policy_document" "default" {
statement {
sid = ""
principals {
type = "Service"
identifiers = [
"lambda.amazonaws.com",
]
}
actions = [
"sts:AssumeRole",
]
}
}
data "aws_iam_policy_document" "ami_backup" {
statement {
actions = [
"logs:*",
]
resources = [
"arn:aws:logs:*:*:*",
]
}
statement {
actions = [
"ec2:DescribeInstances",
"ec2:CreateImage",
"ec2:DescribeImages",
"ec2:DeregisterImage",
"ec2:DescribeSnapshots",
"ec2:DeleteSnapshot",
"ec2:CreateTags",
]
resources = [
"*",
]
}
}
data "archive_file" "ami_backup" {
type = "zip"
source_file = "${local.module_relpath}/ami_backup.py"
output_path = "${local.module_relpath}/ami_backup.zip"
}
data "archive_file" "ami_cleanup" {
type = "zip"
source_file = "${local.module_relpath}/ami_cleanup.py"
output_path = "${local.module_relpath}/ami_cleanup.zip"
}
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.7"
namespace = var.namespace
stage = var.stage
name = var.name
}
module "label_backup" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.7"
namespace = var.namespace
stage = var.stage
name = "${var.name}-backup-${var.instance_id}"
}
module "label_cleanup" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.7"
namespace = var.namespace
stage = var.stage
name = "${var.name}-cleanup-${var.instance_id}"
}
module "label_role" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.7"
namespace = var.namespace
stage = var.stage
name = "${var.name}-${var.instance_id}"
}
resource "aws_iam_role" "ami_backup" {
name = module.label_role.id
assume_role_policy = data.aws_iam_policy_document.default.json
}
resource "aws_iam_role_policy" "ami_backup" {
name = module.label_role.id
role = aws_iam_role.ami_backup.id
policy = data.aws_iam_policy_document.ami_backup.json
}
resource "aws_lambda_function" "ami_backup" {
filename = data.archive_file.ami_backup.output_path
function_name = module.label_backup.id
description = "Automatically backup EC2 instance (create AMI)"
role = aws_iam_role.ami_backup.arn
timeout = 60
handler = "ami_backup.lambda_handler"
runtime = "python2.7"
source_code_hash = data.archive_file.ami_backup.output_base64sha256
environment = {
variables = {
region = "${var.region}"
ami_owner = "${var.ami_owner}"
instance_id = "${var.instance_id}"
retention = "${var.retention_days}"
label_id = "${module.label.id}"
reboot = "${var.reboot ? "1" : "0"}"
block_device_mappings = "${jsonencode(var.block_device_mappings)}"
}
}
}
resource "aws_lambda_function" "ami_cleanup" {
filename = data.archive_file.ami_cleanup.output_path
function_name = module.label_cleanup.id
description = "Automatically remove AMIs that have expired (delete AMI)"
role = aws_iam_role.ami_backup.arn
timeout = 60
handler = "ami_cleanup.lambda_handler"
runtime = "python2.7"
source_code_hash = data.archive_file.ami_cleanup.output_base64sha256
environment = {
variables = {
region = "${var.region}"
ami_owner = "${var.ami_owner}"
instance_id = "${var.instance_id}"
label_id = "${module.label.id}"
}
}
}
resource "null_resource" "schedule" {
triggers = {
backup = "${var.backup_schedule}"
cleanup = "${var.cleanup_schedule}"
}
}
resource "aws_cloudwatch_event_rule" "ami_backup" {
name = module.label_backup.id
description = "Schedule for AMI snapshot backups"
schedule_expression = null_resource.schedule.triggers.backup
depends_on = ["null_resource.schedule"]
}
resource "aws_cloudwatch_event_rule" "ami_cleanup" {
name = module.label_cleanup.id
description = "Schedule for AMI snapshot cleanup"
schedule_expression = null_resource.schedule.triggers.cleanup
depends_on = ["null_resource.schedule"]
}
resource "aws_cloudwatch_event_target" "ami_backup" {
rule = aws_cloudwatch_event_rule.ami_backup.name
target_id = module.label_backup.id
arn = aws_lambda_function.ami_backup.arn
}
resource "aws_cloudwatch_event_target" "ami_cleanup" {
rule = aws_cloudwatch_event_rule.ami_cleanup.name
target_id = module.label_cleanup.id
arn = aws_lambda_function.ami_cleanup.arn
}
resource "aws_lambda_permission" "ami_backup" {
statement_id = module.label_backup.id
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ami_backup.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.ami_backup.arn
}
resource "aws_lambda_permission" "ami_cleanup" {
statement_id = module.label_cleanup.id
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.ami_cleanup.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.ami_cleanup.arn
}