-
Notifications
You must be signed in to change notification settings - Fork 2
/
snapshot.tf
159 lines (135 loc) · 4 KB
/
snapshot.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
provider "aws" {
region = "${var.region["${var.buildenv}"]}"
}
variable "org_name" {
description = "Org name"
default = "Local"
}
variable "owner" {
description = "Default Asset owner"
default = "ops"
}
variable "region" {
description = "AWS Region"
default {
"primary" = "us-west-2"
"secondary" = "us-east-1"
}
}
variable "buildenv" {
default = "primary"
}
variable "s3_bucket" {
description = "S3 Bucket to store lambda function"
default = "mylambdabucket"
}
data "aws_iam_policy_document" "instance-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "snapshot_lambda" {
policy_id = "${var.org_name}-Ops-Snapshot"
statement {
sid = "${var.org_name}OpsSnapshot"
effect = "Allow"
resources = [
"*",
]
actions = [
"ec2:CreateTags",
"ec2:CreateSnapshot",
"ec2:DeleteSnapshot",
"ec2:DeregisterImage",
"ec2:DescribeImages",
"ec2:DescribeInstances",
"ec2:DescribeSnapshots",
"ec2:DescribeTags",
"ec2:DescribeVolumes",
"cloudwatch:GetMetricStatistics",
]
}
}
resource "aws_iam_policy" "snapshot_lambda" {
name = "${var.org_name}.Ops.Snapshot"
description = "${var.org_name}.Ops.Snapshot"
path = "/${var.org_name}/Ops/"
policy = "${data.aws_iam_policy_document.snapshot_lambda.json}"
}
resource "aws_iam_role" "snapshot_lambda" {
name = "${var.org_name}.Ops.Snapshot"
assume_role_policy = "${data.aws_iam_policy_document.instance-assume-role-policy.json}"
path = "/${var.org_name}/"
depends_on = [
"aws_iam_policy.snapshot_lambda",
]
}
resource "aws_iam_role_policy_attachment" "snapshot_lambda" {
role = "${aws_iam_role.snapshot_lambda.name}"
policy_arn = "${aws_iam_policy.snapshot_lambda.arn}"
}
resource "aws_cloudwatch_event_rule" "snapshot_lambda" {
name = "${var.org_name}_Ops_EC2_EBS_Snapshot"
description = "Run Snapshots nightly"
schedule_expression = "cron(00 03 * * ? *)"
depends_on = [
"aws_lambda_function.snapshot_lambda",
]
}
resource "aws_cloudwatch_event_target" "snapshot_lambda" {
rule = "${aws_cloudwatch_event_rule.snapshot_lambda.name}"
target_id = "snapshot_lambda"
arn = "${aws_lambda_function.snapshot_lambda.arn}"
depends_on = [
"aws_cloudwatch_event_rule.snapshot_lambda",
]
}
resource "aws_lambda_function" "snapshot_lambda" {
s3_bucket = "${var.s3_bucket}"
s3_key = "ops-snapshot.zip"
function_name = "${var.org_name}_Ops_EC2_EBS_Snapshot"
role = "${aws_iam_role.snapshot_lambda.arn}"
handler = "main.handler"
# the following is predicated on a local copy of the zipped code.
# without it commented, terraform doesn't know the code has changed and won't update lambda function
# source_code_hash = "${base64sha256(file("snapshot.zip"))}"
runtime = "python2.7"
description = "Function to create EBS snapshot volumes of currently used volumes"
timeout = "300"
tags {
Name = "${var.org_name}.Ops.EC2.ebs_snapshotter"
Department = "${var.owner}"
Environment = ""
Stage = ""
Region = "${var.region["${var.buildenv}"]}"
Application = "shared"
Role = "lambda"
Service = "lambda"
Category = "lambda"
}
environment {
variables = {
TYPE = "all"
REGION = "us-west-2"
VERBOSITY = "INFO"
VOLUME = ""
INSTANCE_ID = ""
AMI_CREATION_METHOD = "Packer"
RETENTION = 7
ROTATION = 7
HOURLY = ""
PERSIST = ""
ENVIRONMENT = "*"
INCLUDE_AMI = "false"
DRY_RUN = "true"
#ACCOUNT_ID = ""
}
}
depends_on = [
"aws_iam_role_policy_attachment.snapshot_lambda",
]
}