From 9ea37abe94dd4e5e777c4975e694aa4d5c25a27a Mon Sep 17 00:00:00 2001 From: Thierno BARRY Date: Wed, 7 Aug 2019 14:20:03 +0200 Subject: [PATCH] add option to recreate ASG when LT or LC changes --- CHANGELOG.md | 1 + local.tf | 1 + versions.tf | 1 + workers.tf | 33 +++++++++++++++++++++++++++--- workers_launch_template.tf | 34 ++++++++++++++++++++++++++----- workers_launch_template_mixed.tf | 35 +++++++++++++++++++++++++++----- 6 files changed, 92 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57f2de6a36..4cb9e47ade 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ project adheres to [Semantic Versioning](http://semver.org/). - Added support for workers iam role tag in `./workers.tf` (@lucas-giaco) - Added `required_providers` to enforce provider minimum versions (by @dpiddockcmp) - Updated `local.spot_allocation_strategy` docstring to indicate availability of new `capacity-optimized` option. (by @sc250024) + - Added option to recreate ASG when LT or LC changes (by @barryib) ### Changed diff --git a/local.tf b/local.tf index d43118c95d..98d3909494 100644 --- a/local.tf +++ b/local.tf @@ -21,6 +21,7 @@ locals { asg_max_size = "3" # Maximum worker capacity in the autoscaling group. asg_min_size = "1" # Minimum worker capacity in the autoscaling group. asg_force_delete = false # Enable forced deletion for the autoscaling group. + asg_recreate_on_change = false # Recreate the autoscaling group when LT or LC change. instance_type = "m4.large" # Size of the workers instances. spot_price = "" # Cost of spot instance. placement_tenancy = "" # The tenancy of the instance. Valid values are "default" or "dedicated". diff --git a/versions.tf b/versions.tf index affd035db0..aff0bf8375 100644 --- a/versions.tf +++ b/versions.tf @@ -6,5 +6,6 @@ terraform { local = ">= 1.2" null = ">= 2.1" template = ">= 2.1" + random = ">= 2.1" } } diff --git a/workers.tf b/workers.tf index 8a330d4a3f..7446855649 100644 --- a/workers.tf +++ b/workers.tf @@ -1,8 +1,17 @@ # Worker Groups using Launch Configurations resource "aws_autoscaling_group" "workers" { - count = local.worker_group_count - name_prefix = "${aws_eks_cluster.this.name}-${lookup(var.worker_groups[count.index], "name", count.index)}" + count = local.worker_group_count + name_prefix = join( + "-", + compact( + [ + aws_eks_cluster.this.name, + lookup(var.worker_groups[count.index], "name", count.index), + lookup(var.worker_groups[count.index], "asg_recreate_on_change", local.workers_group_defaults["asg_recreate_on_change"]) ? random_pet.workers[count.index].id : "" + ] + ) + ) desired_capacity = lookup( var.worker_groups[count.index], "asg_desired_capacity", @@ -210,6 +219,25 @@ resource "aws_launch_configuration" "workers" { } } +resource "random_pet" "workers" { + count = local.worker_group_count + + separator = "-" + length = 2 + + keepers = { + lt_name = join( + "-", + compact( + [ + aws_launch_configuration.workers[count.index].name, + aws_launch_configuration.workers[count.index].latest_version + ] + ) + ) + } +} + resource "aws_security_group" "workers" { count = var.worker_create_security_group ? 1 : 0 name_prefix = aws_eks_cluster.this.name @@ -390,4 +418,3 @@ data "aws_iam_policy_document" "worker_autoscaling" { } } } - diff --git a/workers_launch_template.tf b/workers_launch_template.tf index df4a231dd7..46eef4c2f0 100644 --- a/workers_launch_template.tf +++ b/workers_launch_template.tf @@ -2,11 +2,16 @@ resource "aws_autoscaling_group" "workers_launch_template" { count = local.worker_group_launch_template_count - name_prefix = "${aws_eks_cluster.this.name}-${lookup( - var.worker_groups_launch_template[count.index], - "name", - count.index, - )}" + name_prefix = join( + "-", + compact( + [ + aws_eks_cluster.this.name, + lookup(var.worker_groups_launch_template[count.index], "name", count.index), + lookup(var.worker_groups_launch_template[count.index], "asg_recreate_on_change", local.workers_group_defaults["asg_recreate_on_change"]) ? random_pet.workers_launch_template[count.index].id : "" + ] + ) + ) desired_capacity = lookup( var.worker_groups_launch_template[count.index], "asg_desired_capacity", @@ -296,6 +301,25 @@ resource "aws_launch_template" "workers_launch_template" { } } +resource "random_pet" "workers_launch_template" { + count = local.worker_group_launch_template_count + + separator = "-" + length = 2 + + keepers = { + lt_name = join( + "-", + compact( + [ + aws_launch_template.workers_launch_template[count.index].name, + aws_launch_template.workers_launch_template[count.index].latest_version + ] + ) + ) + } +} + resource "aws_iam_instance_profile" "workers_launch_template" { count = var.manage_worker_iam_resources ? local.worker_group_launch_template_count : 0 name_prefix = aws_eks_cluster.this.name diff --git a/workers_launch_template_mixed.tf b/workers_launch_template_mixed.tf index 61df13670d..75489e990f 100644 --- a/workers_launch_template_mixed.tf +++ b/workers_launch_template_mixed.tf @@ -2,11 +2,17 @@ resource "aws_autoscaling_group" "workers_launch_template_mixed" { count = local.worker_group_launch_template_mixed_count - name_prefix = "${aws_eks_cluster.this.name}-${lookup( - var.worker_groups_launch_template_mixed[count.index], - "name", - count.index, - )}" + name_prefix = join( + "-", + compact( + [ + aws_eks_cluster.this.name, + lookup(var.worker_groups_launch_template_mixed[count.index], "name", count.index), + lookup(var.worker_groups_launch_template_mixed[count.index], "asg_recreate_on_change", local.workers_group_defaults["asg_recreate_on_change"]) ? random_pet.workers_launch_template_mixed[count.index].id : "" + ] + ) + ) + desired_capacity = lookup( var.worker_groups_launch_template_mixed[count.index], "asg_desired_capacity", @@ -338,6 +344,25 @@ resource "aws_launch_template" "workers_launch_template_mixed" { } } +resource "random_pet" "workers_launch_template_mixed" { + count = local.worker_group_launch_template_mixed_count + + separator = "-" + length = 2 + + keepers = { + lt_name = join( + "-", + compact( + [ + aws_launch_template.workers_launch_template_mixed[count.index].name, + aws_launch_template.workers_launch_template_mixed[count.index].latest_version + ] + ) + ) + } +} + resource "aws_iam_instance_profile" "workers_launch_template_mixed" { count = var.manage_worker_iam_resources ? local.worker_group_launch_template_mixed_count : 0 name_prefix = aws_eks_cluster.this.name