Skip to content

Commit 84cbaf5

Browse files
feat: add instance requirements option for docker autoscaler asg (#1262)
## Description Add an option to specify instance requirements instead of a using a fixed list of instance types to provide greater flexibility and cost optimization. This option allows ASG to dynamically select the best instance types based on availability, price, and performance criteria rather than being restricted to a predefined list and ensures better spot instance allocation. You can either use types as before: ``` runner_worker_docker_autoscaler_asg = { subnet_ids = module.vpc.private_subnets types = ["t3a.micro", "t3a.small", "t3.micro", "t3.small"] enable_mixed_instances_policy = true on_demand_percentage_above_base_capacity = 0 spot_allocation_strategy = "price-capacity-optimized" } ``` Or you can use instance requirements: ``` runner_worker_docker_autoscaler_asg = { subnet_ids = module.vpc.private_subnets default_instance_type = "t3.small" enable_mixed_instances_policy = true on_demand_percentage_above_base_capacity = 0 spot_allocation_strategy = "price-capacity-optimized" instance_requirements = [{ allowed_instance_types = ["t*"] cpu_manufacturers = ["intel"] burstable_performance = "included" memory_mib = { max = 8192 min = 1024 } vcpu_count = { max = 4 min = 2 } }] } ``` --------- Co-authored-by: Matthias Kay <matthias.kay@hlag.com> Co-authored-by: Matthias Kay <github@matthiaskay.de>
1 parent 11895c8 commit 84cbaf5

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

.cspell.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"trivy",
6565
"usermod",
6666
"userns",
67+
"vcpu",
6768
"xanzy",
6869
"xvda"
6970
],
@@ -74,6 +75,7 @@
7475
"backports",
7576
"blockquotes",
7677
"bluegreen",
78+
"burstable",
7779
"codeowners",
7880
"cpu",
7981
"cpus",

docker_autoscaler.tf

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ resource "aws_launch_template" "this" {
1212
name = "${local.name_runner_agent_instance}-worker-launch-template"
1313
user_data = base64gzip(var.runner_worker_docker_autoscaler_instance.start_script)
1414
image_id = length(var.runner_worker_docker_autoscaler_ami_id) > 0 ? var.runner_worker_docker_autoscaler_ami_id : data.aws_ami.docker_autoscaler_by_filter[0].id
15-
instance_type = var.runner_worker_docker_autoscaler_asg.types[0]
15+
instance_type = length(var.runner_worker_docker_autoscaler_asg.types) > 0 ? var.runner_worker_docker_autoscaler_asg.types[0] : var.runner_worker_docker_autoscaler_asg.default_instance_type
1616
key_name = aws_key_pair.autoscaler[0].key_name
1717
ebs_optimized = var.runner_worker_docker_autoscaler_instance.ebs_optimized
1818

@@ -103,12 +103,39 @@ resource "aws_autoscaling_group" "autoscaler" {
103103
launch_template_id = aws_launch_template.this[0].id
104104
version = aws_launch_template.this[0].latest_version
105105
}
106+
106107
dynamic "override" {
107108
for_each = var.runner_worker_docker_autoscaler_asg.types
108109
content {
109110
instance_type = override.value
110111
}
111112
}
113+
114+
dynamic "override" {
115+
for_each = var.runner_worker_docker_autoscaler_asg.instance_requirements
116+
content {
117+
instance_requirements {
118+
allowed_instance_types = override.value.allowed_instance_types
119+
cpu_manufacturers = override.value.cpu_manufacturers
120+
instance_generations = override.value.instance_generations
121+
burstable_performance = override.value.burstable_performance
122+
dynamic "memory_mib" {
123+
for_each = [override.value.memory_mib]
124+
content {
125+
max = memory_mib.value.max
126+
min = memory_mib.value.min
127+
}
128+
}
129+
dynamic "vcpu_count" {
130+
for_each = [override.value.vcpu_count]
131+
content {
132+
max = vcpu_count.value.max
133+
min = vcpu_count.value.min
134+
}
135+
}
136+
}
137+
}
138+
}
112139
}
113140
}
114141
}

variables.tf

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,10 @@ variable "runner_worker_docker_autoscaler_asg" {
803803
spot_allocation_strategy = How to allocate capacity across the Spot pools. 'lowest-price' to optimize cost, 'capacity-optimized' to reduce interruptions.
804804
spot_instance_pools = Number of Spot pools per availability zone to allocate capacity. EC2 Auto Scaling selects the cheapest Spot pools and evenly allocates Spot capacity across the number of Spot pools that you specify.
805805
subnet_ids = The list of subnet IDs to use for the Runner Worker when the fleet mode is enabled.
806+
default_instance_type = Default instance type for the launch template
806807
types = The type of instance to use for the Runner Worker. In case of fleet mode, multiple instance types are supported.
807808
upgrade_strategy = Auto deploy new instances when launch template changes. Can be either 'bluegreen', 'rolling' or 'off'.
809+
instance_requirements = Override the instance type in the Launch Template with instance types that satisfy the requirements.
808810
EOT
809811
type = object({
810812
enabled_metrics = optional(list(string), [])
@@ -818,10 +820,28 @@ variable "runner_worker_docker_autoscaler_asg" {
818820
spot_allocation_strategy = optional(string, "lowest-price")
819821
spot_instance_pools = optional(number, 2)
820822
subnet_ids = optional(list(string), [])
821-
types = optional(list(string), ["m5.large"])
823+
default_instance_type = optional(string, "m5.large")
824+
types = optional(list(string), [])
822825
upgrade_strategy = optional(string, "rolling")
826+
instance_requirements = optional(list(object({
827+
allowed_instance_types = optional(list(string), [])
828+
cpu_manufacturers = optional(list(string), [])
829+
instance_generations = optional(list(string), [])
830+
burstable_performance = optional(string)
831+
memory_mib = optional(object({
832+
min = optional(number, null)
833+
max = optional(number, null) }), {})
834+
vcpu_count = optional(object({
835+
min = optional(number, null)
836+
max = optional(number, null) }), {})
837+
})), [])
823838
})
824839
default = {}
840+
841+
validation {
842+
condition = length(var.runner_worker_docker_autoscaler_asg.types) == 0 || length(var.runner_worker_docker_autoscaler_asg.instance_requirements) == 0
843+
error_message = "AWS does not allow setting both 'types' and 'instance_requirements' at the same time. Set only one."
844+
}
825845
}
826846

827847
variable "runner_worker_docker_machine_role" {

0 commit comments

Comments
 (0)