Skip to content

Commit 0f8b9da

Browse files
author
Benjamin Ash
committed
feat: add support for ASG instance refresh for workers
1 parent a26c9fd commit 0f8b9da

File tree

7 files changed

+43
-10
lines changed

7 files changed

+43
-10
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
145145
| Name | Version |
146146
|------|---------|
147147
| terraform | >= 0.12.9, != 0.13.0 |
148-
| aws | >= 3.22.0 |
148+
| aws | >= 3.26.0 |
149149
| kubernetes | >= 1.11.1 |
150150
| local | >= 1.4 |
151151
| null | >= 2.1 |
@@ -156,7 +156,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
156156

157157
| Name | Version |
158158
|------|---------|
159-
| aws | >= 3.22.0 |
159+
| aws | >= 3.26.0 |
160160
| kubernetes | >= 1.11.1 |
161161
| local | >= 1.4 |
162162
| null | >= 2.1 |
@@ -221,6 +221,7 @@ MIT Licensed. See [LICENSE](https://github.com/terraform-aws-modules/terraform-a
221221
| worker\_create\_cluster\_primary\_security\_group\_rules | Whether to create security group rules to allow communication between pods on workers and pods using the primary cluster security group. | `bool` | `false` | no |
222222
| worker\_create\_initial\_lifecycle\_hooks | Whether to create initial lifecycle hooks provided in worker groups. | `bool` | `false` | no |
223223
| worker\_create\_security\_group | Whether to create a security group for the workers or attach the workers to `worker_security_group_id`. | `bool` | `true` | no |
224+
| worker\_enable\_instance\_refresh | Enable instance refresh for the worker autoscaling group. Refresh preferences can be overridden in workers\_group\_defaults. All keys start with 'instance\_refresh\_' | `bool` | `false` | no |
224225
| worker\_groups | A list of maps defining worker group configurations to be defined using AWS Launch Configurations. See workers\_group\_defaults for valid keys. | `any` | `[]` | no |
225226
| worker\_groups\_launch\_template | A list of maps defining worker group configurations to be defined using AWS Launch Templates. See workers\_group\_defaults for valid keys. | `any` | `[]` | no |
226227
| worker\_security\_group\_id | If provided, all workers will be attached to this security group. If not given, a security group will be created with necessary ingress/egress to work with the EKS cluster. | `string` | `""` | no |

local.tf

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ locals {
9494
spot_instance_pools = 10 # "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."
9595
spot_max_price = "" # Maximum price per unit hour that the user is willing to pay for the Spot instances. Default is the on-demand price
9696
max_instance_lifetime = 0 # Maximum number of seconds instances can run in the ASG. 0 is unlimited.
97+
instance_refresh_strategy = "Rolling" # Strategy to use for instance refresh. Default is 'Rolling' which the only valid value.
98+
instance_refresh_min_healthy_percentage = 90 # The amount of capacity in the ASG that must remain healthy during an instance refresh, as a percentage of the ASG's desired capacity.
99+
instance_refresh_instance_warmup = null # The number of seconds until a newly launched instance is configured and ready to use. Defaults to the ASG's health check grace period.
100+
instance_refresh_triggers = [] # Set of additional property names that will trigger an Instance Refresh. A refresh will always be triggered by a change in any of launch_configuration, launch_template, or mixed_instances_policy.
97101
}
98102

99103
workers_group_defaults = merge(

modules/fargate/data.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ data "aws_iam_policy_document" "eks_fargate_pod_assume_role" {
1212
}
1313

1414
data "aws_iam_role" "custom_fargate_iam_role" {
15-
count = local.create_eks && !var.create_fargate_pod_execution_role ? 1 : 0
15+
count = local.create_eks && ! var.create_fargate_pod_execution_role ? 1 : 0
1616
name = var.fargate_pod_execution_role_name
1717
}

variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ variable "workers_additional_policies" {
163163
default = []
164164
}
165165

166+
variable "worker_enable_instance_refresh" {
167+
description = "Enable instance refresh for the worker autoscaling group. Refresh preferences can be overridden in workers_group_defaults. All keys start with 'instance_refresh_'"
168+
default = false
169+
}
170+
166171
variable "kubeconfig_aws_authenticator_command" {
167172
description = "Command to use to fetch AWS EKS credentials."
168173
type = string

versions.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_version = ">= 0.12.9, != 0.13.0"
33

44
required_providers {
5-
aws = ">= 3.22.0"
5+
aws = ">= 3.26.0"
66
local = ">= 1.4"
77
null = ">= 2.1"
88
template = ">= 2.1"

workers.tf

+14-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ resource "aws_autoscaling_group" "workers" {
138138
"value", tag_value,
139139
"propagate_at_launch", "true"
140140
)
141-
if tag_key != "Name" && !contains([for tag in lookup(var.worker_groups[count.index], "tags", local.workers_group_defaults["tags"]) : tag["key"]], tag_key)
141+
if tag_key != "Name" && ! contains([for tag in lookup(var.worker_groups[count.index], "tags", local.workers_group_defaults["tags"]) : tag["key"]], tag_key)
142142
],
143143
lookup(
144144
var.worker_groups[count.index],
@@ -153,6 +153,18 @@ resource "aws_autoscaling_group" "workers" {
153153
}
154154
}
155155

156+
dynamic "instance_refresh" {
157+
for_each = var.worker_enable_instance_refresh ? [1] : []
158+
content {
159+
strategy = local.workers_group_defaults["instance_refresh_strategy"]
160+
preferences {
161+
instance_warmup = local.workers_group_defaults["instance_refresh_instance_warmup"]
162+
min_healthy_percentage = local.workers_group_defaults["instance_refresh_min_healthy_percentage"]
163+
}
164+
triggers = local.workers_group_defaults["instance_refresh_triggers"]
165+
}
166+
}
167+
156168
lifecycle {
157169
create_before_destroy = true
158170
ignore_changes = [desired_capacity]
@@ -199,7 +211,7 @@ resource "aws_launch_configuration" "workers" {
199211
ebs_optimized = lookup(
200212
var.worker_groups[count.index],
201213
"ebs_optimized",
202-
!contains(
214+
! contains(
203215
local.ebs_optimized_not_supported,
204216
lookup(
205217
var.worker_groups[count.index],

workers_launch_template.tf

+15-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ resource "aws_autoscaling_group" "workers_launch_template" {
157157
instance_type = override.value
158158
}
159159
}
160-
161160
}
162161
}
163162
}
@@ -214,7 +213,7 @@ resource "aws_autoscaling_group" "workers_launch_template" {
214213
"value", tag_value,
215214
"propagate_at_launch", "true"
216215
)
217-
if tag_key != "Name" && !contains([for tag in lookup(var.worker_groups_launch_template[count.index], "tags", local.workers_group_defaults["tags"]) : tag["key"]], tag_key)
216+
if tag_key != "Name" && ! contains([for tag in lookup(var.worker_groups_launch_template[count.index], "tags", local.workers_group_defaults["tags"]) : tag["key"]], tag_key)
218217
],
219218
lookup(
220219
var.worker_groups_launch_template[count.index],
@@ -229,6 +228,18 @@ resource "aws_autoscaling_group" "workers_launch_template" {
229228
}
230229
}
231230

231+
dynamic "instance_refresh" {
232+
for_each = var.worker_enable_instance_refresh ? [1] : []
233+
content {
234+
strategy = local.workers_group_defaults["instance_refresh_strategy"]
235+
preferences {
236+
instance_warmup = local.workers_group_defaults["instance_refresh_instance_warmup"]
237+
min_healthy_percentage = local.workers_group_defaults["instance_refresh_min_healthy_percentage"]
238+
}
239+
triggers = local.workers_group_defaults["instance_refresh_triggers"]
240+
}
241+
}
242+
232243
lifecycle {
233244
create_before_destroy = true
234245
ignore_changes = [desired_capacity]
@@ -302,7 +313,7 @@ resource "aws_launch_template" "workers_launch_template" {
302313
ebs_optimized = lookup(
303314
var.worker_groups_launch_template[count.index],
304315
"ebs_optimized",
305-
!contains(
316+
! contains(
306317
local.ebs_optimized_not_supported,
307318
lookup(
308319
var.worker_groups_launch_template[count.index],
@@ -481,7 +492,7 @@ resource "aws_launch_template" "workers_launch_template" {
481492
},
482493
{ for tag_key, tag_value in var.tags :
483494
tag_key => tag_value
484-
if tag_key != "Name" && !contains([for tag in lookup(var.worker_groups_launch_template[count.index], "tags", local.workers_group_defaults["tags"]) : tag["key"]], tag_key)
495+
if tag_key != "Name" && ! contains([for tag in lookup(var.worker_groups_launch_template[count.index], "tags", local.workers_group_defaults["tags"]) : tag["key"]], tag_key)
485496
}
486497
)
487498
}

0 commit comments

Comments
 (0)