This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.tf
120 lines (98 loc) · 3.01 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
#####
# AWS provider
#####
# Retrieve AWS credentials from env variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
provider "aws" {
region = "${var.aws_region}"
}
#####
# IAM role
#####
data "template_file" "policy_json" {
template = "${file("${path.module}/template/policy.json.tpl")}"
vars {}
}
resource "aws_iam_policy" "iam_role_policy" {
name = "${var.lambda_name}-tagging-lambda"
path = "/"
description = "Policy for role ${var.lambda_name}-tagging-lambda"
policy = "${data.template_file.policy_json.rendered}"
}
resource "aws_iam_role" "iam_role" {
name = "${var.lambda_name}-tagging-lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy_attachment" "lambda-attach" {
name = "${var.lambda_name}-tagging-lambda-attachment"
roles = ["${aws_iam_role.iam_role.name}"]
policy_arn = "${aws_iam_policy.iam_role_policy.arn}"
}
#####
# Lambda Function
#####
# Generate ZIP archive with Lambda
data "template_file" "lambda" {
template = "${file("${path.module}/template/tagging_lambda.py")}"
vars {
aws_region = "${var.aws_region}"
name = "${var.lambda_name}"
search_tag_key = "${var.search_tag_key}"
search_tag_value = "${var.search_tag_value}"
tags = "${jsonencode(var.tags)}"
timestamp = "${timestamp()}"
}
}
resource "null_resource" "zip_lambda" {
triggers {
template_rendered = "${ data.template_file.lambda.rendered }"
}
provisioner "local-exec" {
command = "cat << EOF > /tmp/tagging_lambda.py\n${ data.template_file.lambda.rendered }\nEOF"
}
provisioner "local-exec" {
command = "zip -j /tmp/tagging_lambda /tmp/tagging_lambda.py"
}
}
# Create lambda
resource "aws_lambda_function" "tagging" {
depends_on = ["aws_iam_role.iam_role", "null_resource.zip_lambda"]
filename = "/tmp/tagging_lambda.zip"
function_name = "${var.lambda_name}-tagging-lambda"
role = "${aws_iam_role.iam_role.arn}"
handler = "tagging_lambda.lambda_handler"
runtime = "python2.7"
timeout = "60"
memory_size = "128"
tags = "${var.tags}"
}
resource "aws_cloudwatch_event_rule" "tagging" {
name = "${var.lambda_name}-tagging-lambda"
description = "Trigger tagging lambda in periodical intervals"
schedule_expression = "rate(5 minutes)"
}
resource "aws_lambda_permission" "tagging" {
statement_id = "${var.lambda_name}-AllowCloudWatchTrigger"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.tagging.function_name}"
principal = "events.amazonaws.com"
source_arn = "${aws_cloudwatch_event_rule.tagging.arn}"
}
resource "aws_cloudwatch_event_target" "tagging" {
rule = "${aws_cloudwatch_event_rule.tagging.name}"
target_id = "${var.lambda_name}-TriggerLambda"
arn = "${aws_lambda_function.tagging.arn}"
}