Skip to content

Commit ec92645

Browse files
bashimsbarryib
authored andcommitted
fix: set an ASG's launch template version to an explicit version to automatically trigger instance refresh (terraform-aws-modules#1370)
NOTES: Set an ASG's launch template version to an explicit version automatically. This will ensure that an instance refresh will be triggered whenever the launch template changes. The default `launch_template_version` is now used to determine the latest or default version of the created launch template for self-managed worker groups. Signed-off-by: Benjamin Ash <[email protected]> Co-authored-by: Thierno IB. BARRY <[email protected]>
1 parent 4f327ae commit ec92645

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

examples/instance_refresh/main.tf

+5-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,9 @@ resource "helm_release" "aws_node_termination_handler" {
217217
# ensures that node termination does not require the lifecycle action to be completed,
218218
# and thus allows the ASG to be destroyed cleanly.
219219
resource "aws_autoscaling_lifecycle_hook" "aws_node_termination_handler" {
220-
for_each = toset(module.eks.workers_asg_names)
221-
220+
count = length(module.eks.workers_asg_names)
222221
name = "aws-node-termination-handler"
223-
autoscaling_group_name = each.value
222+
autoscaling_group_name = module.eks.workers_asg_names[count.index]
224223
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
225224
heartbeat_timeout = 300
226225
default_result = "CONTINUE"
@@ -239,9 +238,11 @@ module "eks" {
239238
asg_max_size = 2
240239
asg_desired_capacity = 2
241240
instance_refresh_enabled = true
242-
instance_refresh_triggers = ["tag"]
241+
instance_refresh_instance_warmup = 60
243242
public_ip = true
244243
metadata_http_put_response_hop_limit = 3
244+
update_default_version = true
245+
instance_refresh_triggers = ["tag"]
245246
tags = [
246247
{
247248
key = "aws-node-termination-handler/managed"

local.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ locals {
7575
root_block_device_name = data.aws_ami.eks_worker.root_device_name # Root device name for workers. If non is provided, will assume default AMI was used.
7676
root_kms_key_id = "" # The KMS key to use when encrypting the root storage device
7777
launch_template_id = null # The id of the launch template used for managed node_groups
78-
launch_template_version = "$Latest" # The lastest version of the launch template to use in the autoscaling group
78+
launch_template_version = "$Latest" # The latest version of the launch template to use in the autoscaling group
79+
update_default_version = false # Update the autoscaling group launch template's default version upon each update
7980
launch_template_placement_tenancy = "default" # The placement tenancy for instances
8081
launch_template_placement_group = null # The name of the placement group into which to launch the instances, if any.
8182
root_encrypted = false # Whether the volume should be encrypted or not

workers_launch_template.tf

+20-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ resource "aws_autoscaling_group" "workers_launch_template" {
141141
version = lookup(
142142
var.worker_groups_launch_template[count.index],
143143
"launch_template_version",
144-
local.workers_group_defaults["launch_template_version"],
144+
lookup(
145+
var.worker_groups_launch_template[count.index],
146+
"launch_template_version",
147+
local.workers_group_defaults["launch_template_version"]
148+
) == "$Latest"
149+
? aws_launch_template.workers_launch_template.*.latest_version[count.index]
150+
: aws_launch_template.workers_launch_template.*.default_version[count.index]
145151
)
146152
}
147153

@@ -169,7 +175,13 @@ resource "aws_autoscaling_group" "workers_launch_template" {
169175
version = lookup(
170176
var.worker_groups_launch_template[count.index],
171177
"launch_template_version",
172-
local.workers_group_defaults["launch_template_version"],
178+
lookup(
179+
var.worker_groups_launch_template[count.index],
180+
"launch_template_version",
181+
local.workers_group_defaults["launch_template_version"]
182+
) == "$Latest"
183+
? aws_launch_template.workers_launch_template.*.latest_version[count.index]
184+
: aws_launch_template.workers_launch_template.*.default_version[count.index]
173185
)
174186
}
175187
}
@@ -278,6 +290,12 @@ resource "aws_launch_template" "workers_launch_template" {
278290
count.index,
279291
)}"
280292

293+
update_default_version = lookup(
294+
var.worker_groups_launch_template[count.index],
295+
"update_default_version",
296+
local.workers_group_defaults["update_default_version"],
297+
)
298+
281299
network_interfaces {
282300
associate_public_ip_address = lookup(
283301
var.worker_groups_launch_template[count.index],

0 commit comments

Comments
 (0)