Skip to content

Commit 9e81140

Browse files
authored
Merge pull request #30 from Aswin-Vijayan/TEC-56
[TEC-56][Add] - Added scaling policy to asg
2 parents 36bc006 + 290314f commit 9e81140

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

environments/dev/alb-asg/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ module "asg" {
127127
environment = var.environment
128128
cost_center = var.cost_center
129129
application = var.application
130+
instance_warmup_time = var.instance_warmup_time
131+
target_value = var.target_value
130132
alb_target_group_arn = module.alb.alb_target_group_arn
131133
iam_role = module.iam-policy.iam_role
132134
security_group_ids = module.instance-sg.security_group_ids

environments/dev/alb-asg/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,13 @@ variable "alb_create_egress_sg" {
362362
type = bool
363363
description = "Enable or disable CIDR block egress rules."
364364
}
365+
366+
variable "instance_warmup_time" {
367+
description = "Time required to warm up a new instance"
368+
type = number
369+
}
370+
371+
variable "target_value" {
372+
description = "Threshold value of asg to start scaling"
373+
type = number
374+
}

modules/asg/main.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ resource "aws_autoscaling_group" "application_asg" {
5858

5959
}
6060

61+
resource "aws_autoscaling_policy" "cpu_scaling_policy" {
62+
name = "${var.environment}-${var.application}-cpu-scaling-policy"
63+
policy_type = "TargetTrackingScaling"
64+
estimated_instance_warmup = var.instance_warmup_time
65+
autoscaling_group_name = aws_autoscaling_group.application_asg.name
66+
67+
target_tracking_configuration {
68+
predefined_metric_specification {
69+
predefined_metric_type = "ASGAverageCPUUtilization"
70+
}
71+
72+
target_value = var.target_value
73+
}
74+
}
75+
76+
6177
resource "aws_autoscaling_attachment" "application_asg_attachment" {
6278
autoscaling_group_name = aws_autoscaling_group.application_asg.name
6379
lb_target_group_arn = var.alb_target_group_arn

modules/asg/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,12 @@ variable "alb_target_group_arn" {
9494
type = string
9595
}
9696

97+
variable "instance_warmup_time" {
98+
description = "Time required to warm up a new instance"
99+
type = number
100+
}
101+
102+
variable "target_value" {
103+
description = "Threshold value of asg to start scaling"
104+
type = number
105+
}

vars/dev/alb-asg.tfvars

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ alb_egress_sg_protocol = ["-1"]
2929
alb_create_egress_sg = false
3030

3131
# instance sg
32-
ingress_cidr_from_port = [80]
33-
ingress_cidr_to_port = [80]
32+
ingress_cidr_from_port = [22]
33+
ingress_cidr_to_port = [22]
3434
ingress_cidr_protocol = ["tcp"]
3535
ingress_cidr_block = ["0.0.0.0/0"]
3636
create_ingress_cidr = true
@@ -86,10 +86,12 @@ user_data = <<-EOF
8686
EOF
8787

8888
#autoscaling_group
89-
max_size = 1
89+
max_size = 2
9090
min_size = 1
9191
desired_capacity = 1
9292
propagate_at_launch = true
93+
instance_warmup_time = 30
94+
target_value = 50
9395

9496
#tags
9597
owner = "techiescamp"

0 commit comments

Comments
 (0)