-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
100 lines (85 loc) · 2.78 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
data "aws_iam_policy_document" "ebs_tagger_assume_role_policy" {
statement {
sid = ""
effect = "Allow"
actions = [ "sts:AssumeRole" ]
principals {
type = "Service"
identifiers = [ "lambda.amazonaws.com" ]
}
}
}
resource "aws_iam_role" "ebs_tagger" {
name = "ebs_tagger"
assume_role_policy = "${data.aws_iam_policy_document.ebs_tagger_assume_role_policy.json}"
}
data "aws_iam_policy_document" "ebs_tagger_policy" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = [ "arn:aws:logs:*:*:*" ]
}
statement {
effect = "Allow"
actions = [
"ec2:DescribeVolumes",
"ec2:CreateTags",
"ec2:DescribeTags"
]
resources = [ "*" ]
}
}
resource "aws_iam_policy" "ebs_tagger" {
name = "ebs_tagger"
description = "Policy for ebs_tagger lambda"
policy = "${data.aws_iam_policy_document.ebs_tagger_policy.json}"
}
resource "aws_iam_role_policy_attachment" "ebs_tagger_role_policy_attachment" {
role = "${aws_iam_role.ebs_tagger.name}"
policy_arn = "${aws_iam_policy.ebs_tagger.arn}"
}
# lambda functions have to be zip'ed up
data "archive_file" "ebs_tagger_zip" {
type = "zip"
source_file = "${path.module}/lambda.py"
output_path = "/tmp/ebs_tagger.zip"
}
# create the lambda function
resource "aws_lambda_function" "ebs_tagger" {
function_name = "ebs_tagger"
runtime = "python2.7"
handler = "lambda.ebs_tagger_handler"
role = "${aws_iam_role.ebs_tagger.arn}"
timeout = 20
filename = "/tmp/ebs_tagger.zip"
source_code_hash = "${data.archive_file.ebs_tagger_zip.output_base64sha256}"
environment {
variables = {
LAMBDA_TAGS = "${join(",",formatlist("%s=%s",keys(var.tags),values(var.tags)))}"
}
}
}
# create cloudwatch event rule
resource "aws_cloudwatch_event_rule" "ebs_tagger" {
name = "ec2-volumes-events-to-ebs_tagger"
description = "Trigger lambda when EC2 Volume receives notification"
event_pattern = "${jsonencode( {"source":[ "aws.ec2" ], "detail-type":[ "EBS Volume Notification" ]} )}"
}
# invoke lambda when the cloudwatch event rule triggers
resource "aws_cloudwatch_event_target" "invoke-lambda" {
rule = "${aws_cloudwatch_event_rule.ebs_tagger.name}"
target_id = "InvokeLambda"
arn = "${aws_lambda_function.ebs_tagger.arn}"
}
# permit cloudwatch event rule to invoke lambda
resource "aws_lambda_permission" "lambda_permissions" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.ebs_tagger.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.ebs_tagger.arn}"
}