Skip to content

Commit 393da7e

Browse files
authored
feat: Add support for tracking latest AMI release version on managed nodegroups (#2951)
1 parent f1bbfc4 commit 393da7e

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

examples/eks_managed_node_group/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ module "eks" {
9292
ami_type = "AL2023_x86_64_STANDARD"
9393
platform = "al2023"
9494

95+
use_latest_ami_release_version = true
96+
9597
cloudinit_pre_nodeadm = [
9698
{
9799
content_type = "application/node.eks.aws"
@@ -125,6 +127,8 @@ module "eks" {
125127
ami_type = "BOTTLEROCKET_x86_64"
126128
platform = "bottlerocket"
127129

130+
use_latest_ami_release_version = true
131+
128132
# This will get added to what AWS provides
129133
bootstrap_extra_args = <<-EOT
130134
# extra args added

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ module "eks_managed_node_group" {
9494
| [aws_ec2_instance_type_offerings.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ec2_instance_type_offerings) | data source |
9595
| [aws_iam_policy_document.assume_role_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
9696
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
97+
| [aws_ssm_parameter.ami](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
9798
| [aws_subnets.efa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
9899

99100
## Inputs
@@ -176,6 +177,7 @@ module "eks_managed_node_group" {
176177
| <a name="input_update_config"></a> [update\_config](#input\_update\_config) | Configuration block of settings for max unavailable resources during node group updates | `map(string)` | <pre>{<br> "max_unavailable_percentage": 33<br>}</pre> | no |
177178
| <a name="input_update_launch_template_default_version"></a> [update\_launch\_template\_default\_version](#input\_update\_launch\_template\_default\_version) | Whether to update the launch templates default version on each update. Conflicts with `launch_template_default_version` | `bool` | `true` | no |
178179
| <a name="input_use_custom_launch_template"></a> [use\_custom\_launch\_template](#input\_use\_custom\_launch\_template) | Determines whether to use a custom launch template or not. If set to `false`, EKS will use its own default launch template | `bool` | `true` | no |
180+
| <a name="input_use_latest_ami_release_version"></a> [use\_latest\_ami\_release\_version](#input\_use\_latest\_ami\_release\_version) | Determines whether to use the latest AMI release version for the given `ami_type` (except for `CUSTOM`). Note: `ami_type` and `cluster_version` must be supplied in order to enable this feature | `bool` | `false` | no |
179181
| <a name="input_use_name_prefix"></a> [use\_name\_prefix](#input\_use\_name\_prefix) | Determines whether to use `name` as is or create a unique name beginning with the `name` as the prefix | `bool` | `true` | no |
180182
| <a name="input_user_data_template_path"></a> [user\_data\_template\_path](#input\_user\_data\_template\_path) | Path to a local, custom user data template file to use when rendering user data | `string` | `""` | no |
181183
| <a name="input_vpc_security_group_ids"></a> [vpc\_security\_group\_ids](#input\_vpc\_security\_group\_ids) | A list of security group IDs to associate | `list(string)` | `[]` | no |

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,45 @@ resource "aws_launch_template" "this" {
329329
}
330330
}
331331

332+
################################################################################
333+
# AMI SSM Parameter
334+
################################################################################
335+
336+
locals {
337+
# Just to ensure templating doesn't fail when values are not provided
338+
ssm_cluster_version = var.cluster_version != null ? var.cluster_version : ""
339+
ssm_ami_type = var.ami_type != null ? var.ami_type : ""
340+
341+
# Map the AMI type to the respective SSM param path
342+
ssm_ami_type_to_ssm_param = {
343+
AL2_x86_64 = "/aws/service/eks/optimized-ami/${local.ssm_cluster_version}/amazon-linux-2/recommended/release_version"
344+
AL2_x86_64_GPU = "/aws/service/eks/optimized-ami/${local.ssm_cluster_version}/amazon-linux-2-gpu/recommended/release_version"
345+
AL2_ARM_64 = "/aws/service/eks/optimized-ami/${local.ssm_cluster_version}/amazon-linux-2-arm64/recommended/release_version"
346+
CUSTOM = "NONE"
347+
BOTTLEROCKET_ARM_64 = "/aws/service/bottlerocket/aws-k8s-${local.ssm_cluster_version}/arm64/latest/image_version"
348+
BOTTLEROCKET_x86_64 = "/aws/service/bottlerocket/aws-k8s-${local.ssm_cluster_version}/x86_64/latest/image_version"
349+
BOTTLEROCKET_ARM_64_NVIDIA = "/aws/service/bottlerocket/aws-k8s-${local.ssm_cluster_version}-nvidia/arm64/latest/image_version"
350+
BOTTLEROCKET_x86_64_NVIDIA = "/aws/service/bottlerocket/aws-k8s-${local.ssm_cluster_version}-nvidia/x86_64/latest/image_version"
351+
WINDOWS_CORE_2019_x86_64 = "/aws/service/ami-windows-latest/Windows_Server-2019-English-Full-EKS_Optimized-${local.ssm_cluster_version}"
352+
WINDOWS_FULL_2019_x86_64 = "/aws/service/ami-windows-latest/Windows_Server-2019-English-Core-EKS_Optimized-${local.ssm_cluster_version}"
353+
WINDOWS_CORE_2022_x86_64 = "/aws/service/ami-windows-latest/Windows_Server-2022-English-Full-EKS_Optimized-${local.ssm_cluster_version}"
354+
WINDOWS_FULL_2022_x86_64 = "/aws/service/ami-windows-latest/Windows_Server-2022-English-Core-EKS_Optimized-${local.ssm_cluster_version}"
355+
AL2023_x86_64_STANDARD = "/aws/service/eks/optimized-ami/${local.ssm_cluster_version}/amazon-linux-2023/x86_64/standard/recommended/release_version"
356+
AL2023_ARM_64_STANDARD = "/aws/service/eks/optimized-ami/${local.ssm_cluster_version}/amazon-linux-2023/arm64/standard/recommended/release_version"
357+
}
358+
359+
# The Windows SSM params currently do not have a release version, so we have to get the full output JSON blob and parse out the release version
360+
windows_latest_ami_release_version = var.create && var.use_latest_ami_release_version && startswith(local.ssm_ami_type, "WINDOWS") ? nonsensitive(jsondecode(data.aws_ssm_parameter.ami[0].value)["release_version"]) : null
361+
# Based on the steps above, try to get an AMI release version - if not, `null` is returned
362+
latest_ami_release_version = startswith(local.ssm_ami_type, "WINDOWS") ? local.windows_latest_ami_release_version : try(nonsensitive(data.aws_ssm_parameter.ami[0].value), null)
363+
}
364+
365+
data "aws_ssm_parameter" "ami" {
366+
count = var.create && var.use_latest_ami_release_version ? 1 : 0
367+
368+
name = local.ssm_ami_type_to_ssm_param[var.ami_type]
369+
}
370+
332371
################################################################################
333372
# Node Group
334373
################################################################################
@@ -359,7 +398,7 @@ resource "aws_eks_node_group" "this" {
359398

360399
# https://docs.aws.amazon.com/eks/latest/userguide/launch-templates.html#launch-template-custom-ami
361400
ami_type = var.ami_id != "" ? null : var.ami_type
362-
release_version = var.ami_id != "" ? null : var.ami_release_version
401+
release_version = var.ami_id != "" ? null : var.use_latest_ami_release_version ? local.latest_ami_release_version : var.ami_release_version
363402
version = var.ami_id != "" ? null : var.cluster_version
364403

365404
capacity_type = var.capacity_type

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ variable "ami_release_version" {
346346
default = null
347347
}
348348

349+
variable "use_latest_ami_release_version" {
350+
description = "Determines whether to use the latest AMI release version for the given `ami_type` (except for `CUSTOM`). Note: `ami_type` and `cluster_version` must be supplied in order to enable this feature"
351+
type = bool
352+
default = false
353+
}
354+
349355
variable "capacity_type" {
350356
description = "Type of capacity associated with the EKS Node Group. Valid values: `ON_DEMAND`, `SPOT`"
351357
type = string

node_groups.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,10 @@ module "eks_managed_node_group" {
310310
max_size = try(each.value.max_size, var.eks_managed_node_group_defaults.max_size, 3)
311311
desired_size = try(each.value.desired_size, var.eks_managed_node_group_defaults.desired_size, 1)
312312

313-
ami_id = try(each.value.ami_id, var.eks_managed_node_group_defaults.ami_id, "")
314-
ami_type = try(each.value.ami_type, var.eks_managed_node_group_defaults.ami_type, null)
315-
ami_release_version = try(each.value.ami_release_version, var.eks_managed_node_group_defaults.ami_release_version, null)
313+
ami_id = try(each.value.ami_id, var.eks_managed_node_group_defaults.ami_id, "")
314+
ami_type = try(each.value.ami_type, var.eks_managed_node_group_defaults.ami_type, null)
315+
ami_release_version = try(each.value.ami_release_version, var.eks_managed_node_group_defaults.ami_release_version, null)
316+
use_latest_ami_release_version = try(each.value.use_latest_ami_release_version, var.eks_managed_node_group_defaults.use_latest_ami_release_version, false)
316317

317318
capacity_type = try(each.value.capacity_type, var.eks_managed_node_group_defaults.capacity_type, null)
318319
disk_size = try(each.value.disk_size, var.eks_managed_node_group_defaults.disk_size, null)

0 commit comments

Comments
 (0)