-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
373 lines (303 loc) · 13.4 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# STORAGE
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_storage_80" {
count = var.create_storage_80_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-storage-${var.database_identifier}-80"
alarm_description = "Free storage space on ${var.database_identifier} is less then 80% (${var.database_storage * 0.2}/${var.database_storage} GB) Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=lack-of-free-space"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "FreeStorageSpace"
namespace = "AWS/RDS"
period = 300
threshold = var.database_storage * 1073741824 * 0.2
statistic = "Average"
provider = aws
alarm_actions = [
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_storage_85" {
count = var.create_storage_85_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-storage-${var.database_identifier}-85"
alarm_description = "Free storage space on ${var.database_identifier} is less then 85% (${var.database_storage * 0.15}/${var.database_storage} GB) Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=lack-of-free-space"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "FreeStorageSpace"
namespace = "AWS/RDS"
period = 300
threshold = var.database_storage * 1073741824 * 0.15
statistic = "Average"
provider = aws
alarm_actions = [
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_storage_90" {
count = var.create_storage_90_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-storage-${var.database_identifier}-90"
alarm_description = "Free storage space on ${var.database_identifier} is less then 90% (${var.database_storage * 0.1}/${var.database_storage} GB) Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=lack-of-free-space"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "FreeStorageSpace"
namespace = "AWS/RDS"
period = 300
threshold = var.database_storage * 1073741824 * 0.1
statistic = "Average"
provider = aws
alarm_actions = [
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_storage_95" {
count = var.create_storage_95_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-storage-${var.database_identifier}-95"
alarm_description = "Free storage space on ${var.database_identifier} is less then 95% (${var.database_storage * 0.05}/${var.database_storage} GB) Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=lack-of-free-space"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "FreeStorageSpace"
namespace = "AWS/RDS"
period = 300
threshold = var.database_storage * 1073741824 * 0.05
statistic = "Average"
provider = aws
alarm_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
# CONNECTION COUNT
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_conn_count_80" {
count = var.create_connction_count_80_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-conn-count-${var.database_identifier}-80"
alarm_description = "Connections count on ${var.database_identifier} reached 80% of maximum ${var.max_connection_count} Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=high-connection-count"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "DatabaseConnections"
namespace = "AWS/RDS"
period = 300
threshold = floor(var.max_connection_count * 0.8)
statistic = "Maximum"
provider = aws
alarm_actions = [
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_conn_count_90" {
count = var.create_connction_count_90_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-conn-count-${var.database_identifier}-90"
alarm_description = "Connections count on ${var.database_identifier} reached 90% of maximum ${var.max_connection_count} Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=high-connection-count"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "DatabaseConnections"
namespace = "AWS/RDS"
period = 60
threshold = floor(var.max_connection_count * 0.9)
statistic = "Maximum"
provider = aws
alarm_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
# CPU
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_cpu_95" {
count = var.cpu_alarm_enable_pd == false ? (var.create_cpu_95_alert && var.create_alarms ? 1 : 0) : 0
alarm_name = "rds-alarm-cpu-${var.database_identifier}-95"
alarm_description = "CPU utilization on ${var.database_identifier} reached 95% Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=high-cpu-usage"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = 300
threshold = 95
statistic = "Average"
provider = aws
alarm_actions = [
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_cpu_95_PD_enabled" {
count = var.cpu_alarm_enable_pd && var.create_cpu_95_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-cpu-${var.database_identifier}-95-PD-enabled"
alarm_description = "CPU utilization on ${var.database_identifier} reached 95% Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=high-cpu-usage"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/RDS"
period = 300
threshold = 95
statistic = "Average"
provider = aws
alarm_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_cpucredit_balance_20" {
count = var.create_cpu_credit_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-cpucredit-balance-${var.database_identifier}-20"
alarm_description = "CPU credit balance on ${var.database_identifier} is less than 20% (${format("%.1f", (var.db_instance_max_cpu_credits * 0.2))}/${var.db_instance_max_cpu_credits}) Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=lack-of-available-credits-in-t2-instances"
comparison_operator = "LessThanThreshold"
evaluation_periods = 2
metric_name = "CPUCreditBalance"
namespace = "AWS/RDS"
period = 600
threshold = var.db_instance_max_cpu_credits * 0.2
statistic = "Average"
provider = aws
alarm_actions = [
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
# MEMORY
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_memory_80" {
count = var.create_memory_80_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-memory-${var.database_identifier}-80"
alarm_description = "The amount of used memory on ${var.database_identifier} is more than 80% (${var.db_instance_class_memory * 0.8}/${var.db_instance_class_memory} GB) Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=high-memory-usage"
comparison_operator = "LessThanThreshold"
evaluation_periods = 2
metric_name = "FreeableMemory"
namespace = "AWS/RDS"
period = 300
threshold = var.db_instance_class_memory * 1073741824 * 0.2
statistic = "Average"
provider = aws
alarm_actions = [
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_alarm_memory_90" {
count = var.create_memory_90_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-memory-${var.database_identifier}-90"
alarm_description = "The amount of used memory on ${var.database_identifier} is more than 90% (${var.db_instance_class_memory * 0.9}/${var.db_instance_class_memory} GB) Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=high-memory-usage"
comparison_operator = "LessThanThreshold"
evaluation_periods = 1
metric_name = "FreeableMemory"
namespace = "AWS/RDS"
period = 60
threshold = var.db_instance_class_memory * 1073741824 * 0.1
statistic = "Average"
provider = aws
alarm_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
# REPLICATION LAG
resource "aws_cloudwatch_metric_alarm" "rds_cloudwatch_replication_lag" {
count = var.create_replica_lag_alert && var.create_alarms ? 1 : 0
alarm_name = "rds-alarm-replication-lag-${var.database_identifier}"
alarm_description = "Replication lag on ${var.database_identifier} is more than ${var.replica_lag_threshold / 60} minutes Slack:${var.slack_channel} Runbook:https://kiwi.wiki/devops/wiki/#/operations/runbooks/rds_runbook/rds_runbook?id=high-replica-lag-on-read-replica"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "ReplicaLag"
namespace = "AWS/RDS"
period = 300
threshold = var.replica_lag_threshold
statistic = "Average"
provider = aws
alarm_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
ok_actions = [
var.pager_duty_sns_topic_arn,
var.slack_lambda_sns_topic_arn,
]
dimensions = {
DBInstanceIdentifier = var.database_identifier
}
}
data "template_file" "dd_message_rds_status" {
template = "{{#is_alert}}\nRDS {{ host }} is in state **{{ value }}** (see below)\n\n2: failed\n3: inaccessible-encryption-credentials\n4: incompatible-network\n5: incompatible-option-group\n6: incompatible-parameters\n7: incompatible-restore\n8: maintenance\n9: moving-to-vpc\n10: rebooting\n11: renaming\n12: stopping\n13: stopped\n14: storage-full\n15: upgrading\n{{/is_alert}}\n{{#is_recovery}}RDS {{ host }} is in **available** state{{/is_recovery}}\n${var.dd_prod_slack_channel}"
vars = {
database_identifier = var.database_identifier
dd_prod_slack_channel = var.dd_prod_slack_channel
}
}
# STATUS
resource "datadog_monitor" "rds_dd_status" {
count = var.create_rds_dd_status && var.create_alarms ? 1 : 0
message = data.template_file.dd_message_rds_status.rendered
name = "RDS ${var.database_identifier} is not in available state"
query = "max(last_1m):max:kw.rds.status.available{dbinstanceidentifier:martindb} by {host} >= 2"
type = "metric alert"
notify_no_data = false
tags = [
"service:devops"]
require_full_window = true
include_tags = false
thresholds = {
critical = 2
critical_recovery = 1.5
}
}