Skip to content

Commit

Permalink
add option to recreate ASG when LT or LC changes (terraform-aws-modul…
Browse files Browse the repository at this point in the history
  • Loading branch information
barryib authored and max-rocket-internet committed Aug 20, 2019
1 parent 5636447 commit d8ed7d0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions local.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
1 change: 1 addition & 0 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ terraform {
local = ">= 1.2"
null = ">= 2.1"
template = ">= 2.1"
random = ">= 2.1"
}
}
33 changes: 30 additions & 3 deletions workers.tf
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -390,4 +418,3 @@ data "aws_iam_policy_document" "worker_autoscaling" {
}
}
}

34 changes: 29 additions & 5 deletions workers_launch_template.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
35 changes: 30 additions & 5 deletions workers_launch_template_mixed.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d8ed7d0

Please sign in to comment.