-
Notifications
You must be signed in to change notification settings - Fork 395
/
10-AutoScalingGroup.tf
74 lines (63 loc) · 2.15 KB
/
10-AutoScalingGroup.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
resource "aws_autoscaling_group" "app1_asg" {
name_prefix = "app1-auto-scaling-group-"
min_size = 3
max_size = 15
desired_capacity = 6
vpc_zone_identifier = [
aws_subnet.private-eu-west-1a.id,
aws_subnet.private-eu-west-1b.id,
aws_subnet.private-eu-west-1c.id
]
health_check_type = "ELB"
health_check_grace_period = 300
force_delete = true
target_group_arns = [aws_lb_target_group.app1_tg.arn]
launch_template {
id = aws_launch_template.app1_LT.id
version = "$Latest"
}
enabled_metrics = ["GroupMinSize", "GroupMaxSize", "GroupDesiredCapacity", "GroupInServiceInstances", "GroupTotalInstances"]
# Instance protection for launching
initial_lifecycle_hook {
name = "instance-protection-launch"
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
default_result = "CONTINUE"
heartbeat_timeout = 60
notification_metadata = "{\"key\":\"value\"}"
}
# Instance protection for terminating
initial_lifecycle_hook {
name = "scale-in-protection"
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
default_result = "CONTINUE"
heartbeat_timeout = 300
}
tag {
key = "Name"
value = "app1-instance"
propagate_at_launch = true
}
tag {
key = "Environment"
value = "Production"
propagate_at_launch = true
}
}
# Auto Scaling Policy
resource "aws_autoscaling_policy" "app1_scaling_policy" {
name = "app1-cpu-target"
autoscaling_group_name = aws_autoscaling_group.app1_asg.name
policy_type = "TargetTrackingScaling"
estimated_instance_warmup = 120
target_tracking_configuration {
predefined_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
target_value = 75.0
}
}
# Enabling instance scale-in protection
resource "aws_autoscaling_attachment" "app1_asg_attachment" {
autoscaling_group_name = aws_autoscaling_group.app1_asg.name
alb_target_group_arn = aws_lb_target_group.app1_tg.arn
}