-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda_sli_metrics.tf
155 lines (124 loc) · 4.46 KB
/
lambda_sli_metrics.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
#
# http status code
#
resource "aws_lambda_function" "sli-http-status-code" {
count = local.on_sli_monitoring ? 1 : 0
function_name = local.lambda_sli-http-status-code[count.index]["name"]
handler = "${local.lambda_sli-http-status-code[count.index]["name"]}.lambda_handler"
filename = "${data.archive_file.function_zip.output_path}"
layers = ["${aws_lambda_layer_version.lambda_layer.arn}"]
memory_size = 3008
timeout = 600
runtime = local.lambda_runtime
role = "${aws_iam_role.lambda_iam_role.arn}"
source_code_hash = "${data.archive_file.function_zip.output_base64sha256}"
environment {
variables = {
mackerel_api_key = local.mackerel_api_key
mackerel_host_id = local.sli_http_status_host_id
lb_hosting_platform_name = local.lb_hosting_platform_name
}
}
}
resource "aws_cloudwatch_event_target" "sli-http-status-code" {
count = local.on_sli_monitoring ? 1 : 0
target_id = local.lambda_sli-http-status-code[count.index]["name"]
arn = element(aws_lambda_function.sli-http-status-code.*.arn, count.index)
rule = local.lambda_sli-http-status-code[count.index]["rule"]
}
resource "aws_lambda_permission" "sli-http-status-code" {
count = local.on_sli_monitoring ? 1 : 0
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
principal = "events.amazonaws.com"
function_name = element(aws_lambda_function.sli-http-status-code.*.function_name, count.index)
source_arn = local.lambda_schedule_arns[local.lambda_sli-http-status-code[count.index]["rule"]]
}
resource "aws_cloudwatch_log_group" "sli-http-status-code" {
count = local.on_sli_monitoring ? 1 : 0
name = format("%s%s", local.lambda_log_group_prefix, local.lambda_sli-http-status-code[count.index]["name"])
retention_in_days = 7
}
#
# response latency
#
resource "aws_lambda_function" "sli-response-latency" {
count = local.on_sli_monitoring ? 1 : 0
function_name = local.lambda_sli-response-latency[count.index]["name"]
handler = "${local.lambda_sli-response-latency[count.index]["name"]}.lambda_handler"
filename = "${data.archive_file.function_zip.output_path}"
layers = ["${aws_lambda_layer_version.lambda_layer.arn}"]
memory_size = 128
timeout = 30
runtime = local.lambda_runtime
role = "${aws_iam_role.lambda_iam_role.arn}"
source_code_hash = "${data.archive_file.function_zip.output_base64sha256}"
environment {
variables = {
mackerel_api_key = local.mackerel_api_key
newrelic_query_key = local.newrelic_query_key
newrelic_account = local.newrelic_account
}
}
}
resource "aws_cloudwatch_event_target" "sli-response-latency" {
count = local.on_sli_monitoring ? 1 : 0
target_id = local.lambda_sli-response-latency[count.index]["name"]
arn = element(aws_lambda_function.sli-response-latency.*.arn, count.index)
rule = local.lambda_sli-response-latency[count.index]["rule"]
}
resource "aws_lambda_permission" "sli-response-latency" {
count = local.on_sli_monitoring ? 1 : 0
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
principal = "events.amazonaws.com"
function_name = element(aws_lambda_function.sli-response-latency.*.function_name, count.index)
source_arn = local.lambda_schedule_arns[local.lambda_sli-response-latency[count.index]["rule"]]
}
resource "aws_cloudwatch_log_group" "sli-response-latency" {
count = local.on_sli_monitoring ? 1 : 0
name = format("%s%s", local.lambda_log_group_prefix, local.lambda_sli-response-latency[count.index]["name"])
retention_in_days = 7
}
# Role
resource "aws_iam_role" "lambda_iam_role" {
count = local.on_sli_monitoring ? 1 : 0
name = var.system_name_iam_role
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}
# Policy
resource "aws_iam_role_policy" "lambda_access_policy" {
count = local.on_sli_monitoring ? 1 : 0
name = var.system_name_lambda_access_policy
role = aws_iam_role.lambda_iam_role.id
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
POLICY
}