This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmonitoring.tf
197 lines (164 loc) · 6.34 KB
/
monitoring.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
193
194
195
196
197
# #########################################
# SNS Monitoring Topic
# #########################################
resource "aws_sns_topic" "monitoring" {
count = var.enable_monitoring ? 1 : 0
name = "${module.labels.id}-monitoring"
tags = module.labels.tags
}
# #########################################
# Lambda Slack Notifier
# #########################################
resource "aws_sns_topic_subscription" "notify_slack" {
count = var.enable_monitoring ? 1 : 0
topic_arn = aws_sns_topic.monitoring[0].arn
protocol = "lambda"
endpoint = aws_lambda_function.notify_slack[0].arn
}
resource "aws_lambda_permission" "notify_slack" {
count = var.enable_monitoring ? 1 : 0
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.notify_slack[0].function_name
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.monitoring[0].arn
}
data "archive_file" "notify_slack" {
type = "zip"
output_path = "${path.module}/.zip/${module.labels.id}_notify_slack.zip"
source_file = "${path.module}/templates/lambda-placeholder.js"
}
data "aws_iam_policy_document" "notify_slack_policy" {
statement {
actions = [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DetachNetworkInterface",
"ec2:DeleteNetworkInterface"
]
resources = ["*"]
}
}
data "aws_iam_policy_document" "notify_slack_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = [
"lambda.amazonaws.com",
]
}
}
}
resource "aws_iam_policy" "notify_slack_policy" {
count = var.enable_monitoring ? 1 : 0
name = "${module.labels.id}-lambda-notify-slack-policy"
path = "/"
policy = data.aws_iam_policy_document.notify_slack_policy.json
}
resource "aws_iam_role" "notify_slack" {
count = var.enable_monitoring ? 1 : 0
name = "${module.labels.id}-lambda-notify-slack"
assume_role_policy = data.aws_iam_policy_document.notify_slack_assume_role.json
tags = module.labels.tags
}
resource "aws_iam_role_policy_attachment" "verification_policy" {
count = var.enable_monitoring ? 1 : 0
role = aws_iam_role.notify_slack[0].name
policy_arn = aws_iam_policy.notify_slack_policy[0].arn
}
resource "aws_iam_role_policy_attachment" "verification_logs" {
count = var.enable_monitoring ? 1 : 0
role = aws_iam_role.notify_slack[0].name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_lambda_function" "notify_slack" {
count = var.enable_monitoring ? 1 : 0
function_name = "${module.labels.id}-notify-slack"
role = aws_iam_role.notify_slack[0].arn
handler = "notify_slack.handler"
filename = "${path.module}/.zip/${module.labels.id}_notify_slack.zip"
source_code_hash = data.archive_file.notify_slack.output_base64sha256
runtime = "nodejs10.x"
timeout = 15
memory_size = 128
environment {
variables = {
SLACK_WEBHOOK_URL = var.slack_webhook_url
SLACK_USERNAME = var.slack_username
NODE_ENV = "production"
}
}
lifecycle {
ignore_changes = [
source_code_hash,
]
}
tags = module.labels.tags
}
# #########################################
# API Gateway 5XX Rate error Alarm
# #########################################
resource "aws_cloudwatch_metric_alarm" "gw_5xx_count" {
count = var.enable_monitoring ? 1 : 0
alarm_name = "${module.labels.id}-5XX"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "5XXError"
namespace = "AWS/ApiGateway"
period = var.period_5xx
statistic = "Sum"
threshold = var.threshold_5xx
treat_missing_data = "notBreaching"
alarm_description = format("HTTPCode %v count for %v are more than %v in the last %d minute(s) over %v period(s)", "5XX", module.labels.id, var.threshold_5xx, var.period_5xx / 60, 1)
alarm_actions = [aws_sns_topic.monitoring[0].arn]
ok_actions = [aws_sns_topic.monitoring[0].arn]
insufficient_data_actions = []
dimensions = {
ApiName = "${module.labels.id}-gw"
}
}
# #########################################
# API Gateway 4XX Rate error Alarm
# #########################################
resource "aws_cloudwatch_metric_alarm" "gw_4xx_count" {
count = var.enable_monitoring ? 1 : 0
alarm_name = "${module.labels.id}-4XX"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "4XXError"
namespace = "AWS/ApiGateway"
period = var.period_4xx
statistic = "Sum"
threshold = var.threshold_4xx
treat_missing_data = "notBreaching"
alarm_description = format("HTTPCode %v count for %v are more than %v in the last %d minute(s) over %v period(s)", "4XX", module.labels.id, var.threshold_4xx, var.period_4xx / 60, 1)
alarm_actions = [aws_sns_topic.monitoring[0].arn]
ok_actions = [aws_sns_topic.monitoring[0].arn]
insufficient_data_actions = []
dimensions = {
ApiName = "${module.labels.id}-gw"
}
}
# #########################################
# API Gateway Latency Alarm
# #########################################
resource "aws_cloudwatch_metric_alarm" "gw_p95_latency" {
count = var.enable_monitoring ? 1 : 0
alarm_name = "${module.labels.id}-p95-latency"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "Latency"
namespace = "AWS/ApiGateway"
period = var.period_latency
extended_statistic = "p95"
threshold = var.threshold_latency
treat_missing_data = "notBreaching"
alarm_description = format("API Gateway p95 latency for %v is more than %v milliseconds for the last %d minute(s) over %v period(s)", module.labels.id, var.threshold_latency, var.period_latency / 60, 1)
alarm_actions = [aws_sns_topic.monitoring[0].arn]
ok_actions = [aws_sns_topic.monitoring[0].arn]
insufficient_data_actions = []
dimensions = {
ApiName = "${module.labels.id}-gw"
}
}