-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudwatch.tf
86 lines (78 loc) · 3.01 KB
/
cloudwatch.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
// CREATE CLOUDWATCH LOG GROUP
resource "aws_cloudwatch_log_group" "cloudwatch_log_group" {
name = "/aws/ecs/${aws_ecs_cluster.ecs_cluster.name}"
retention_in_days = "7"
}
//// CREATE CLOUDWATCH LOG SUBSCRIPTION FILTER
//resource "aws_cloudwatch_log_subscription_filter" "cloudwatch_log_subscription_filter" {
// name = "${aws_ecs_cluster.ecs_cluster.name}-log-subscription-filter"
// log_group_name = "${aws_cloudwatch_log_group.cloudwatch_log_group.name}"
// filter_pattern = ""
// destination_arn = "${aws_lambda_function.lambda_function.arn}"
//
// depends_on = [
// "aws_cloudwatch_log_group.cloudwatch_log_group",
// "aws_lambda_permission.lambda_permission",
// ]
//}
// CREATE SCALE UP CLOUDWATCH METRIC ALARM
resource "aws_cloudwatch_metric_alarm" "scale_up_cloudwatch_metric_alarm" {
alarm_actions = ["${aws_autoscaling_policy.scale_up_autoscaling_policy.arn}"]
alarm_description = "This metric monitors ${aws_ecs_cluster.ecs_cluster.name} ECS CPU reservation usage"
alarm_name = "${aws_ecs_cluster.ecs_cluster.name}-cpu-reservation-usage-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "CPUReservation"
namespace = "AWS/ECS"
period = 60
statistic = "Average"
threshold = 70
dimensions {
ClusterName = "${aws_ecs_cluster.ecs_cluster.name}"
}
depends_on = [
"aws_autoscaling_policy.scale_up_autoscaling_policy",
]
}
// CREATE SCALE DOWN CLOUDWATCH METRIC ALARM
resource "aws_cloudwatch_metric_alarm" "scale_down_cloudwatch_metric_alarm" {
alarm_actions = ["${aws_autoscaling_policy.scale_down_autoscaling_policy.arn}"]
alarm_description = "This metric monitors ${aws_ecs_cluster.ecs_cluster.name} ECS CPU reservation usage"
alarm_name = "${aws_ecs_cluster.ecs_cluster.name}-cpu-reservation-usage-low"
comparison_operator = "LessThanThreshold"
evaluation_periods = 1
metric_name = "CPUReservation"
namespace = "AWS/ECS"
period = 60
statistic = "Average"
threshold = 10
dimensions {
ClusterName = "${aws_ecs_cluster.ecs_cluster.name}"
}
depends_on = [
"aws_autoscaling_policy.scale_down_autoscaling_policy",
]
}
// MONITOR CLUSTER VIA CLOUDWATCH EVENTS
// - WORKED MANUALLY HAVING IAM ISSUES.
// - LOTS OF EVENTS.
//// CREATE CLOUDWATCH EVENT RULE
//resource "aws_cloudwatch_event_rule" "cloudwatch_event_rule" {
// name = "${aws_ecs_cluster.ecs_cluster.name}-events"
// description = "Capture each ${aws_ecs_cluster.ecs_cluster.name} event"
//
// event_pattern = <<PATTERN
//{
// "source": [
// "aws.ecs"
// ]
//}
//PATTERN
//}
//
//// CREATE CLOUDWATCH EVENT TARGET
//resource "aws_cloudwatch_event_target" "cloudwatch_event_target" {
// rule = "${aws_cloudwatch_event_rule.cloudwatch_event_rule.name}"
// target_id = "${aws_ecs_cluster.ecs_cluster.name}-event-stream"
// arn = "${aws_lambda_function.ecs_events_lambda_function.arn}"
//}