-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2_scale.tf
More file actions
78 lines (66 loc) · 2.24 KB
/
ec2_scale.tf
File metadata and controls
78 lines (66 loc) · 2.24 KB
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
## Creating Launch Configuration
resource "aws_launch_configuration" "ec2_scale_temp" {
name = "ec2_scale_temp-${local.production_name}"
#Pull data for latest ami
image_id = data.aws_ami.ecs.id
iam_instance_profile = aws_iam_instance_profile.ecs-instance-profile.name
instance_type = "t3.small"
security_groups = [aws_security_group.webservers.id]
#SSH Key
key_name = aws_key_pair.generated_key.key_name
#Pull data and run as a script during boot
user_data = data.template_file.user_data.rendered
lifecycle {
create_before_destroy = true
}
}
#Pull file data and save it as a template
data "template_file" "user_data" {
template = file("${path.module}/conf/user_data.tpl")
vars = {
name = local.production_name
}
}
# Creating AutoScaling Group
resource "aws_autoscaling_group" "ec2_scale" {
name = "ec2_scale-${local.production_name}"
launch_configuration = aws_launch_configuration.ec2_scale_temp.id
vpc_zone_identifier = [aws_subnet.sub-a.id, aws_subnet.sub-b.id, aws_subnet.sub-c.id]
desired_capacity = 1
min_size = 1
max_size = 10
default_cooldown = 30
health_check_grace_period = 30
tag {
key = "Name"
value = "Webserver-Worker-${local.production_name}"
propagate_at_launch = true
}
lifecycle {
ignore_changes = [desired_capacity]
}
}
#Create and attach the policy to our scale group
resource "aws_autoscaling_policy" "ecs_cluster_scale_policy" {
name = "ecs_cluster_scale_policy-${local.production_name}"
policy_type = "TargetTrackingScaling"
adjustment_type = "ChangeInCapacity"
lifecycle {
ignore_changes = [adjustment_type]
}
autoscaling_group_name = aws_autoscaling_group.ec2_scale.name
#Monitor for memory restriction, once a task is using 70% of
#resource memory we'll create a new one to stay under 70%
target_tracking_configuration {
customized_metric_specification {
metric_dimension {
name = "ClusterName"
value = aws_ecs_cluster.cluster.name
}
metric_name = "MemoryReservation"
namespace = "AWS/ECS"
statistic = "Average"
}
target_value = 70.0
}
}