-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecs-cluster.tf
94 lines (83 loc) · 3.01 KB
/
ecs-cluster.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
# ECS cluster
resource "aws_ecs_cluster" "backend-cluster" {
name = var.ecs_cluster_name
}
#Compute
resource "aws_autoscaling_group" "asg-cluster" {
name = var.asg_name
vpc_zone_identifier = [
for s in aws_subnet.public :
s.id
]
min_size = var.min_cluster_size
max_size = var.max_cluster_size
launch_configuration = aws_launch_configuration.cluster-lc.name
health_check_grace_period = 120
termination_policies = ["OldestInstance"]
}
resource "aws_autoscaling_policy" "asg-policy" {
name = var.asg_policy_name
policy_type = "TargetTrackingScaling"
estimated_instance_warmup = "90"
adjustment_type = "ChangeInCapacity"
autoscaling_group_name = aws_autoscaling_group.asg-cluster.name
target_tracking_configuration {
predefined_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
target_value = 40.0
}
}
resource "aws_autoscaling_policy" "up-scale" {
name = "up-scale"
autoscaling_group_name = aws_autoscaling_group.asg-cluster.name
adjustment_type = "ChangeInCapacity"
policy_type = "SimpleScaling"
scaling_adjustment = 1
}
resource "aws_autoscaling_policy" "down-scale" {
name = "down-scale"
autoscaling_group_name = aws_autoscaling_group.asg-cluster.name
adjustment_type = "ChangeInCapacity"
policy_type = "SimpleScaling"
scaling_adjustment = -1
}
resource "aws_cloudwatch_metric_alarm" "alarm-cpu-down" {
alarm_name = "ecs-down"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "MemoryUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "40"
unit = "Percent"
alarm_description = "This metric monitors CPU utilization down"
alarm_actions = [aws_autoscaling_policy.down-scale.arn]
}
resource "aws_cloudwatch_metric_alarm" "alarm-cpu-up" {
alarm_name = "ecs-up"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "MemoryUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "40"
unit = "Percent"
alarm_description = "This metric monitors CPU utilization up"
alarm_actions = [aws_autoscaling_policy.up-scale.arn]
}
resource "aws_launch_configuration" "cluster-lc" {
name_prefix = "demo-cluster-lc"
security_groups = [aws_security_group.instance_sg.id]
key_name = var.key_name
image_id = var.ami_id
instance_type = var.instance_type
iam_instance_profile = aws_iam_instance_profile.ecs-ec2-role.id
user_data = data.template_file.ecs-cluster.rendered
associate_public_ip_address = true
lifecycle {
create_before_destroy = true
}
}