Skip to content

Commit c539b32

Browse files
Merge pull request #63 from Arzianghanchi/feat/autoscaling-var-control
Feat/autoscaling var control
2 parents 31fbb8b + 7fc711e commit c539b32

2 files changed

Lines changed: 94 additions & 13 deletions

File tree

autoscaling.tf

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ locals {
88
#Module : AUTOSCALING POLICY UP
99
#Description : Provides an AutoScaling Scaling Policy resource.
1010
resource "aws_autoscaling_policy" "scale_up" {
11-
count = local.autoscaling_enabled ? 1 : 0
11+
count = local.autoscaling_enabled && var.aws_autoscaling_policy_scale_up ? 1 : 0
1212
name = format("%s%sscale%sup", module.labels.id, var.delimiter, var.delimiter)
1313
scaling_adjustment = var.scale_up_scaling_adjustment
1414
adjustment_type = var.scale_up_adjustment_type
@@ -20,20 +20,20 @@ resource "aws_autoscaling_policy" "scale_up" {
2020
#Module : AUTOSCALING POLICY UP
2121
#Description : Provides an AutoScaling Scaling Policy resource.
2222
resource "aws_autoscaling_policy" "scale_up_spot" {
23-
count = local.spot_autoscaling_enabled ? 1 : 0
23+
count = local.spot_autoscaling_enabled && var.aws_autoscaling_policy_scale_up_spot ? 1 : 0
2424
name = format("%s%sscale%sup-spot", module.labels.id, var.delimiter, var.delimiter)
2525
scaling_adjustment = var.scale_up_scaling_adjustment
2626
adjustment_type = var.scale_up_adjustment_type
2727
policy_type = var.scale_up_policy_type
2828
cooldown = var.scale_up_cooldown_seconds
2929
autoscaling_group_name = join("", aws_autoscaling_group.spot[*].name)
30-
3130
}
3231

32+
3333
#Module : AUTOSCALING POLICY DOWN
3434
#Description : Provides an AutoScaling Scaling Policy resource.
3535
resource "aws_autoscaling_policy" "scale_down" {
36-
count = local.autoscaling_enabled ? 1 : 0
36+
count = local.autoscaling_enabled && var.aws_autoscaling_policy_scale_down ? 1 : 0
3737
name = format("%s%sscale%sdown", module.labels.id, var.delimiter, var.delimiter)
3838
scaling_adjustment = var.scale_down_scaling_adjustment
3939
adjustment_type = var.scale_down_adjustment_type
@@ -45,7 +45,7 @@ resource "aws_autoscaling_policy" "scale_down" {
4545
#Module : AUTOSCALING POLICY DOWN
4646
#Description : Provides an AutoScaling Scaling Policy resource.
4747
resource "aws_autoscaling_policy" "scale_down_spot" {
48-
count = local.spot_autoscaling_enabled ? 1 : 0
48+
count = local.spot_autoscaling_enabled && var.aws_autoscaling_policy_scale_down_spot ? 1 : 0
4949
name = format("%s%sscale%sdown-spot", module.labels.id, var.delimiter, var.delimiter)
5050
scaling_adjustment = var.scale_down_scaling_adjustment
5151
adjustment_type = var.scale_down_adjustment_type
@@ -57,7 +57,7 @@ resource "aws_autoscaling_policy" "scale_down_spot" {
5757
#Module : CLOUDWATCH METRIC ALARM CPU HIGH
5858
#Description : Provides a CloudWatch Metric Alarm resource.
5959
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
60-
count = local.autoscaling_enabled ? 1 : 0
60+
count = local.autoscaling_enabled && var.aws_cloudwatch_metric_alarm_enabled_cpu_high ? 1 : 0
6161
alarm_name = format("%s%scpu%sutilization%shigh", module.labels.id, var.delimiter, var.delimiter, var.delimiter)
6262

6363
comparison_operator = "GreaterThanOrEqualToThreshold"
@@ -79,7 +79,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu_high" {
7979
#Module : CLOUDWATCH METRIC ALARM CPU HIGH
8080
#Description : Provides a CloudWatch Metric Alarm resource.
8181
resource "aws_cloudwatch_metric_alarm" "cpu_high_spot" {
82-
count = local.spot_autoscaling_enabled ? 1 : 0
82+
count = local.spot_autoscaling_enabled && var.aws_cloudwatch_metric_alarm_enabled_cpu_high_spot ? 1 : 0
8383
alarm_name = format("%s%scpu%sutilization%shigh-spot", module.labels.id, var.delimiter, var.delimiter, var.delimiter)
8484

8585
comparison_operator = "GreaterThanOrEqualToThreshold"
@@ -102,7 +102,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu_high_spot" {
102102
#Module : CLOUDWATCH METRIC ALARM CPU LOW
103103
#Description : Provides a CloudWatch Metric Alarm resource.
104104
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
105-
count = local.autoscaling_enabled ? 1 : 0
105+
count = local.autoscaling_enabled && var.aws_cloudwatch_metric_alarm_enabled_cpu_low ? 1 : 0
106106
alarm_name = format("%s%scpu%sutilization%slow", module.labels.id, var.delimiter, var.delimiter, var.delimiter)
107107
comparison_operator = "LessThanOrEqualToThreshold"
108108
evaluation_periods = var.cpu_utilization_low_evaluation_periods
@@ -124,7 +124,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu_low" {
124124
#Module : CLOUDWATCH METRIC ALARM CPU LOW
125125
#Description : Provides a CloudWatch Metric Alarm resource.
126126
resource "aws_cloudwatch_metric_alarm" "cpu_low_spot" {
127-
count = local.spot_autoscaling_enabled ? 1 : 0
127+
count = local.spot_autoscaling_enabled && var.aws_cloudwatch_metric_alarm_enabled_cpu_low_spot ? 1 : 0
128128
alarm_name = format("%s%scpu%sutilization%slow-spot", module.labels.id, var.delimiter, var.delimiter, var.delimiter)
129129
comparison_operator = "LessThanOrEqualToThreshold"
130130
evaluation_periods = var.cpu_utilization_low_evaluation_periods
@@ -146,7 +146,7 @@ resource "aws_cloudwatch_metric_alarm" "cpu_low_spot" {
146146
#Module : AWS AUTOSCALING SCHEDULE
147147
#Description : Provides an AutoScaling Schedule resource.
148148
resource "aws_autoscaling_schedule" "scaledown" {
149-
count = local.autoscaling_enabled_schedule ? 1 : 0
149+
count = local.autoscaling_enabled_schedule && var.enable_autoscaling_schedule_scale_down ? 1 : 0
150150
autoscaling_group_name = join("", aws_autoscaling_group.on_demand[*].name)
151151
scheduled_action_name = format("%s-scheduler-down", module.labels.id)
152152
min_size = var.min_size_scaledown
@@ -159,7 +159,7 @@ resource "aws_autoscaling_schedule" "scaledown" {
159159
#Module : AWS AUTOSCALING SCHEDULE
160160
#Description : Provides an AutoScaling Schedule resource.
161161
resource "aws_autoscaling_schedule" "scaleup" {
162-
count = local.autoscaling_enabled_schedule ? 1 : 0
162+
count = local.autoscaling_enabled_schedule && var.enable_autoscaling_schedule_scale_up ? 1 : 0
163163
autoscaling_group_name = join("", aws_autoscaling_group.on_demand[*].name)
164164
scheduled_action_name = format("%s-scheduler-up", module.labels.id)
165165
max_size = var.max_size_scaleup
@@ -172,7 +172,7 @@ resource "aws_autoscaling_schedule" "scaleup" {
172172
#Module : AWS AUTOSCALING SCHEDULE
173173
#Description : Provides an AutoScaling Schedule resource.
174174
resource "aws_autoscaling_schedule" "spot_scaledown" {
175-
count = local.autoscaling_enabled_spot_schedule ? 1 : 0
175+
count = var.enabled && var.spot_enabled && var.enable_autoscaling_schedule_spot_scale_down ? 1 : 0
176176
autoscaling_group_name = join("", aws_autoscaling_group.spot[*].name)
177177
scheduled_action_name = format("spot-%s-scheduler-down", module.labels.id)
178178
min_size = var.spot_min_size_scaledown
@@ -185,7 +185,7 @@ resource "aws_autoscaling_schedule" "spot_scaledown" {
185185
#Module : AWS AUTOSCALING SCHEDULE
186186
#Description : Provides an AutoScaling Schedule resource.
187187
resource "aws_autoscaling_schedule" "spot_scaleup" {
188-
count = local.autoscaling_enabled_spot_schedule ? 1 : 0
188+
count = var.enabled && var.spot_enabled && var.enable_autoscaling_schedule_spot_scale_up ? 1 : 0
189189
autoscaling_group_name = join("", aws_autoscaling_group.spot[*].name)
190190
scheduled_action_name = format("spot-%s-scheduler-up", module.labels.id)
191191
max_size = var.spot_max_size

variables.tf

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,85 @@ variable "device_name" {
481481
type = string
482482
default = "/dev/sda1"
483483
description = "The name of the block device to be attached to the instance, typically representing the root volume."
484+
}
485+
486+
variable "aws_autoscaling_policy_scale_up" {
487+
type = bool
488+
default = true
489+
description = "Whether to create the scale up autoscaling policy."
490+
491+
}
492+
493+
variable "aws_autoscaling_policy_scale_up_spot" {
494+
type = bool
495+
default = true
496+
description = "Whether to create the scale up autoscaling policy for spot instances."
497+
498+
}
499+
500+
variable "aws_autoscaling_policy_scale_down" {
501+
type = bool
502+
default = true
503+
description = "Whether to create the scale down autoscaling policy."
504+
505+
}
506+
507+
variable "aws_autoscaling_policy_scale_down_spot" {
508+
type = bool
509+
default = true
510+
description = "Whether to create the scale down autoscaling policy for spot instances."
511+
512+
}
513+
514+
variable "aws_cloudwatch_metric_alarm_enabled_cpu_high" {
515+
type = bool
516+
default = true
517+
description = "Whether to create the CloudWatch metric alarm for high CPU utilization."
518+
519+
}
520+
521+
variable "aws_cloudwatch_metric_alarm_enabled_cpu_high_spot" {
522+
type = bool
523+
default = true
524+
description = "Whether to create the CloudWatch metric alarm for high CPU utilization for spot instances."
525+
526+
}
527+
528+
variable "aws_cloudwatch_metric_alarm_enabled_cpu_low" {
529+
type = bool
530+
default = true
531+
description = "Whether to create the CloudWatch metric alarm for low CPU utilization."
532+
533+
}
534+
535+
variable "aws_cloudwatch_metric_alarm_enabled_cpu_low_spot" {
536+
type = bool
537+
default = true
538+
description = "Whether to create the CloudWatch metric alarm for low CPU utilization for spot instances."
539+
540+
}
541+
542+
variable "enable_autoscaling_schedule_scale_down" {
543+
type = bool
544+
default = true
545+
description = "Whether to enable the autoscaling schedule for scale down operations."
546+
547+
}
548+
variable "enable_autoscaling_schedule_scale_up" {
549+
type = bool
550+
default = true
551+
description = "Whether to enable the autoscaling schedule for scale up operations."
552+
553+
}
554+
variable "enable_autoscaling_schedule_spot_scale_down" {
555+
type = bool
556+
default = true
557+
description = "Whether to enable the autoscaling schedule for scale down operations for spot instances."
558+
559+
}
560+
variable "enable_autoscaling_schedule_spot_scale_up" {
561+
type = bool
562+
default = true
563+
description = "Whether to enable the autoscaling schedule for scale up operations for spot instances."
564+
484565
}

0 commit comments

Comments
 (0)