Skip to content

Commit 75db486

Browse files
feat: Enable update in place for node groups with cluster placement group strategy (#3045)
* feat(eks):added subnet az filter for eks nodegroup placement groups * fix: Correct logice for restricting placement group to AZ * fix: Ensure node group args are passed from root module --------- Co-authored-by: Bryant Biggs <[email protected]>
1 parent bfa5821 commit 75db486

File tree

8 files changed

+97
-25
lines changed

8 files changed

+97
-25
lines changed

modules/eks-managed-node-group/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module "eks_managed_node_group" {
9797
| [aws_iam_policy_document.role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
9898
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
9999
| [aws_ssm_parameter.ami](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
100-
| [aws_subnets.efa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
100+
| [aws_subnets.placement_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
101101

102102
## Inputs
103103

@@ -169,6 +169,7 @@ module "eks_managed_node_group" {
169169
| <a name="input_name"></a> [name](#input\_name) | Name of the EKS managed node group | `string` | `""` | no |
170170
| <a name="input_network_interfaces"></a> [network\_interfaces](#input\_network\_interfaces) | Customize network interfaces to be attached at instance boot time | `list(any)` | `[]` | no |
171171
| <a name="input_placement"></a> [placement](#input\_placement) | The placement of the instance | `map(string)` | `{}` | no |
172+
| <a name="input_placement_group_az"></a> [placement\_group\_az](#input\_placement\_group\_az) | Availability zone where placement group is created (ex. `eu-west-1c`) | `string` | `null` | no |
172173
| <a name="input_placement_group_strategy"></a> [placement\_group\_strategy](#input\_placement\_group\_strategy) | The placement group strategy | `string` | `"cluster"` | no |
173174
| <a name="input_platform"></a> [platform](#input\_platform) | [DEPRECATED - use `ami_type` instead. Will be removed in `v21.0`] Identifies the OS platform as `bottlerocket`, `linux` (AL2), `al2023`, or `windows` | `string` | `"linux"` | no |
174175
| <a name="input_post_bootstrap_user_data"></a> [post\_bootstrap\_user\_data](#input\_post\_bootstrap\_user\_data) | User data that is appended to the user data script after of the EKS bootstrap script. Not used when `ami_type` = `BOTTLEROCKET_*` | `string` | `""` | no |

modules/eks-managed-node-group/main.tf

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ data "aws_ec2_instance_type" "this" {
3939
}
4040

4141
locals {
42+
enable_efa_support = var.create && var.enable_efa_support
43+
4244
efa_instance_type = try(element(var.instance_types, 0), "")
4345
num_network_cards = try(data.aws_ec2_instance_type.this[0].maximum_network_cards, 0)
4446

@@ -52,7 +54,7 @@ locals {
5254
}
5355
]
5456

55-
network_interfaces = var.enable_efa_support ? local.efa_network_interfaces : var.network_interfaces
57+
network_interfaces = local.enable_efa_support ? local.efa_network_interfaces : var.network_interfaces
5658
}
5759

5860
################################################################################
@@ -63,7 +65,7 @@ locals {
6365
launch_template_name = coalesce(var.launch_template_name, "${var.name}-eks-node-group")
6466
security_group_ids = compact(concat([var.cluster_primary_security_group_id], var.vpc_security_group_ids))
6567

66-
placement = var.create && (var.enable_efa_support || var.create_placement_group) ? { group_name = aws_placement_group.this[0].name } : var.placement
68+
placement = local.create_placement_group ? { group_name = aws_placement_group.this[0].name } : var.placement
6769
}
6870

6971
resource "aws_launch_template" "this" {
@@ -390,7 +392,7 @@ resource "aws_eks_node_group" "this" {
390392
# Required
391393
cluster_name = var.cluster_name
392394
node_role_arn = var.create_iam_role ? aws_iam_role.this[0].arn : var.iam_role_arn
393-
subnet_ids = var.enable_efa_support ? data.aws_subnets.efa[0].ids : var.subnet_ids
395+
subnet_ids = local.create_placement_group ? data.aws_subnets.placement_group[0].ids : var.subnet_ids
394396

395397
scaling_config {
396398
min_size = var.min_size
@@ -605,8 +607,12 @@ resource "aws_iam_role_policy" "this" {
605607
# Placement Group
606608
################################################################################
607609

610+
locals {
611+
create_placement_group = var.create && (local.enable_efa_support || var.create_placement_group)
612+
}
613+
608614
resource "aws_placement_group" "this" {
609-
count = var.create && (var.enable_efa_support || var.create_placement_group) ? 1 : 0
615+
count = local.create_placement_group ? 1 : 0
610616

611617
name = "${var.cluster_name}-${var.name}"
612618
strategy = var.placement_group_strategy
@@ -624,8 +630,11 @@ resource "aws_placement_group" "this" {
624630
################################################################################
625631

626632
# Find the availability zones supported by the instance type
633+
# TODO - remove at next breaking change
634+
# Force users to be explicit about which AZ to use when using placement groups,
635+
# with or without EFA support
627636
data "aws_ec2_instance_type_offerings" "this" {
628-
count = var.create && var.enable_efa_support ? 1 : 0
637+
count = local.enable_efa_support ? 1 : 0
629638

630639
filter {
631640
name = "instance-type"
@@ -637,17 +646,31 @@ data "aws_ec2_instance_type_offerings" "this" {
637646

638647
# Reverse the lookup to find one of the subnets provided based on the availability
639648
# availability zone ID of the queried instance type (supported)
640-
data "aws_subnets" "efa" {
641-
count = var.create && var.enable_efa_support ? 1 : 0
649+
data "aws_subnets" "placement_group" {
650+
count = local.create_placement_group ? 1 : 0
642651

643652
filter {
644653
name = "subnet-id"
645654
values = var.subnet_ids
646655
}
647656

648-
filter {
649-
name = "availability-zone-id"
650-
values = data.aws_ec2_instance_type_offerings.this[0].locations
657+
# The data source can lookup the first available AZ or you can specify an AZ (next filter)
658+
dynamic "filter" {
659+
for_each = var.enable_efa_support && var.placement_group_az == null ? [1] : []
660+
661+
content {
662+
name = "availability-zone-id"
663+
values = data.aws_ec2_instance_type_offerings.this[0].locations
664+
}
665+
}
666+
667+
dynamic "filter" {
668+
for_each = var.placement_group_az != null ? [var.placement_group_az] : []
669+
670+
content {
671+
name = "availability-zone"
672+
values = [filter.value]
673+
}
651674
}
652675
}
653676

modules/eks-managed-node-group/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ variable "create_placement_group" {
303303
default = false
304304
}
305305

306+
# TODO - remove at next breaking change
306307
variable "placement_group_strategy" {
307308
description = "The placement group strategy"
308309
type = string
@@ -337,6 +338,12 @@ variable "subnet_ids" {
337338
default = null
338339
}
339340

341+
variable "placement_group_az" {
342+
description = "Availability zone where placement group is created (ex. `eu-west-1c`)"
343+
type = string
344+
default = null
345+
}
346+
340347
variable "min_size" {
341348
description = "Minimum number of instances/nodes"
342349
type = number

modules/self-managed-node-group/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module "self_managed_node_group" {
7878
| [aws_iam_policy_document.role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
7979
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
8080
| [aws_ssm_parameter.ami](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
81-
| [aws_subnets.efa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
81+
| [aws_subnets.placement_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
8282

8383
## Inputs
8484

@@ -110,6 +110,7 @@ module "self_managed_node_group" {
110110
| <a name="input_create_iam_instance_profile"></a> [create\_iam\_instance\_profile](#input\_create\_iam\_instance\_profile) | Determines whether an IAM instance profile is created or to use an existing IAM instance profile | `bool` | `true` | no |
111111
| <a name="input_create_iam_role_policy"></a> [create\_iam\_role\_policy](#input\_create\_iam\_role\_policy) | Determines whether an IAM role policy is created or not | `bool` | `true` | no |
112112
| <a name="input_create_launch_template"></a> [create\_launch\_template](#input\_create\_launch\_template) | Determines whether to create launch template or not | `bool` | `true` | no |
113+
| <a name="input_create_placement_group"></a> [create\_placement\_group](#input\_create\_placement\_group) | Determines whether a placement group is created & used by the node group | `bool` | `false` | no |
113114
| <a name="input_create_schedule"></a> [create\_schedule](#input\_create\_schedule) | Determines whether to create autoscaling group schedule or not | `bool` | `true` | no |
114115
| <a name="input_credit_specification"></a> [credit\_specification](#input\_credit\_specification) | Customize the credit specification of the instance | `map(string)` | `{}` | no |
115116
| <a name="input_default_cooldown"></a> [default\_cooldown](#input\_default\_cooldown) | The amount of time, in seconds, after a scaling activity completes before another scaling activity can start | `number` | `null` | no |
@@ -170,6 +171,7 @@ module "self_managed_node_group" {
170171
| <a name="input_network_interfaces"></a> [network\_interfaces](#input\_network\_interfaces) | Customize network interfaces to be attached at instance boot time | `list(any)` | `[]` | no |
171172
| <a name="input_placement"></a> [placement](#input\_placement) | The placement of the instance | `map(string)` | `{}` | no |
172173
| <a name="input_placement_group"></a> [placement\_group](#input\_placement\_group) | The name of the placement group into which you'll launch your instances, if any | `string` | `null` | no |
174+
| <a name="input_placement_group_az"></a> [placement\_group\_az](#input\_placement\_group\_az) | Availability zone where placement group is created (ex. `eu-west-1c`) | `string` | `null` | no |
173175
| <a name="input_platform"></a> [platform](#input\_platform) | [DEPRECATED - must use `ami_type` instead. Will be removed in `v21.0`] | `string` | `null` | no |
174176
| <a name="input_post_bootstrap_user_data"></a> [post\_bootstrap\_user\_data](#input\_post\_bootstrap\_user\_data) | User data that is appended to the user data script after of the EKS bootstrap script. Not used when `ami_type` = `BOTTLEROCKET_*` | `string` | `""` | no |
175177
| <a name="input_pre_bootstrap_user_data"></a> [pre\_bootstrap\_user\_data](#input\_pre\_bootstrap\_user\_data) | User data that is injected into the user data script ahead of the EKS bootstrap script. Not used when `ami_type` = `BOTTLEROCKET_*` | `string` | `""` | no |

modules/self-managed-node-group/main.tf

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ resource "aws_autoscaling_group" "this" {
745745

746746
target_group_arns = var.target_group_arns
747747
termination_policies = var.termination_policies
748-
vpc_zone_identifier = local.enable_efa_support ? data.aws_subnets.efa[0].ids : var.subnet_ids
748+
vpc_zone_identifier = local.enable_efa_support ? data.aws_subnets.placement_group[0].ids : var.subnet_ids
749749
wait_for_capacity_timeout = var.wait_for_capacity_timeout
750750
wait_for_elb_capacity = var.wait_for_elb_capacity
751751

@@ -930,8 +930,12 @@ resource "aws_iam_role_policy" "this" {
930930
# Placement Group
931931
################################################################################
932932

933+
locals {
934+
create_placement_group = var.create && (local.enable_efa_support || var.create_placement_group)
935+
}
936+
933937
resource "aws_placement_group" "this" {
934-
count = local.enable_efa_support ? 1 : 0
938+
count = local.create_placement_group ? 1 : 0
935939

936940
name = "${var.cluster_name}-${var.name}"
937941
strategy = "cluster"
@@ -949,6 +953,9 @@ resource "aws_placement_group" "this" {
949953
################################################################################
950954

951955
# Find the availability zones supported by the instance type
956+
# TODO - remove at next breaking change
957+
# Force users to be explicit about which AZ to use when using placement groups,
958+
# with or without EFA support
952959
data "aws_ec2_instance_type_offerings" "this" {
953960
count = local.enable_efa_support ? 1 : 0
954961

@@ -962,17 +969,31 @@ data "aws_ec2_instance_type_offerings" "this" {
962969

963970
# Reverse the lookup to find one of the subnets provided based on the availability
964971
# availability zone ID of the queried instance type (supported)
965-
data "aws_subnets" "efa" {
966-
count = local.enable_efa_support ? 1 : 0
972+
data "aws_subnets" "placement_group" {
973+
count = local.create_placement_group ? 1 : 0
967974

968975
filter {
969976
name = "subnet-id"
970977
values = var.subnet_ids
971978
}
972979

973-
filter {
974-
name = "availability-zone-id"
975-
values = data.aws_ec2_instance_type_offerings.this[0].locations
980+
# The data source can lookup the first available AZ or you can specify an AZ (next filter)
981+
dynamic "filter" {
982+
for_each = local.create_placement_group && var.placement_group_az == null ? [1] : []
983+
984+
content {
985+
name = "availability-zone-id"
986+
values = data.aws_ec2_instance_type_offerings.this[0].locations
987+
}
988+
}
989+
990+
dynamic "filter" {
991+
for_each = var.placement_group_az != null ? [var.placement_group_az] : []
992+
993+
content {
994+
name = "availability-zone"
995+
values = [filter.value]
996+
}
976997
}
977998
}
978999

modules/self-managed-node-group/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ variable "placement" {
256256
default = {}
257257
}
258258

259+
variable "create_placement_group" {
260+
description = "Determines whether a placement group is created & used by the node group"
261+
type = bool
262+
default = false
263+
}
264+
259265
variable "private_dns_name_options" {
260266
description = "The options for the instance hostname. The default values are inherited from the subnet"
261267
type = map(string)
@@ -384,6 +390,12 @@ variable "availability_zones" {
384390
default = null
385391
}
386392

393+
variable "placement_group_az" {
394+
description = "Availability zone where placement group is created (ex. `eu-west-1c`)"
395+
type = string
396+
default = null
397+
}
398+
387399
variable "subnet_ids" {
388400
description = "A list of subnet IDs to launch resources in. Subnets automatically determine which availability zones the group will reside. Conflicts with `availability_zones`"
389401
type = list(string)

node_groups.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,10 @@ module "eks_managed_node_group" {
376376
enable_monitoring = try(each.value.enable_monitoring, var.eks_managed_node_group_defaults.enable_monitoring, true)
377377
enable_efa_support = try(each.value.enable_efa_support, var.eks_managed_node_group_defaults.enable_efa_support, false)
378378
create_placement_group = try(each.value.create_placement_group, var.eks_managed_node_group_defaults.create_placement_group, false)
379+
placement = try(each.value.placement, var.eks_managed_node_group_defaults.placement, {})
380+
placement_group_az = try(each.value.placement_group_az, var.eks_managed_node_group_defaults.placement_group_az, null)
379381
placement_group_strategy = try(each.value.placement_group_strategy, var.eks_managed_node_group_defaults.placement_group_strategy, "cluster")
380382
network_interfaces = try(each.value.network_interfaces, var.eks_managed_node_group_defaults.network_interfaces, [])
381-
placement = try(each.value.placement, var.eks_managed_node_group_defaults.placement, {})
382383
maintenance_options = try(each.value.maintenance_options, var.eks_managed_node_group_defaults.maintenance_options, {})
383384
private_dns_name_options = try(each.value.private_dns_name_options, var.eks_managed_node_group_defaults.private_dns_name_options, {})
384385

@@ -444,7 +445,9 @@ module "self_managed_node_group" {
444445
context = try(each.value.context, var.self_managed_node_group_defaults.context, null)
445446

446447
target_group_arns = try(each.value.target_group_arns, var.self_managed_node_group_defaults.target_group_arns, [])
448+
create_placement_group = try(each.value.create_placement_group, var.self_managed_node_group_defaults.create_placement_group, false)
447449
placement_group = try(each.value.placement_group, var.self_managed_node_group_defaults.placement_group, null)
450+
placement_group_az = try(each.value.placement_group_az, var.self_managed_node_group_defaults.placement_group_az, null)
448451
health_check_type = try(each.value.health_check_type, var.self_managed_node_group_defaults.health_check_type, null)
449452
health_check_grace_period = try(each.value.health_check_grace_period, var.self_managed_node_group_defaults.health_check_grace_period, null)
450453

tests/eks-managed-node-group/main.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,16 @@ module "eks" {
9090
}
9191
}
9292

93+
placement_group = {
94+
create_placement_group = true
95+
# forces the subnet lookup to be restricted to this availability zone
96+
placement_group_az = element(local.azs, 3)
97+
}
98+
9399
# AL2023 node group utilizing new user data format which utilizes nodeadm
94100
# to join nodes to the cluster (instead of /etc/eks/bootstrap.sh)
95101
al2023_nodeadm = {
96-
ami_type = "AL2023_x86_64_STANDARD"
97-
102+
ami_type = "AL2023_x86_64_STANDARD"
98103
use_latest_ami_release_version = true
99104

100105
cloudinit_pre_nodeadm = [
@@ -376,9 +381,7 @@ module "eks_managed_node_group" {
376381

377382
subnet_ids = module.vpc.private_subnets
378383
cluster_primary_security_group_id = module.eks.cluster_primary_security_group_id
379-
vpc_security_group_ids = [
380-
module.eks.node_security_group_id,
381-
]
384+
vpc_security_group_ids = [module.eks.node_security_group_id]
382385

383386
ami_type = "BOTTLEROCKET_x86_64"
384387

0 commit comments

Comments
 (0)