diff --git a/modules/node_groups/launch_template.tf b/modules/node_groups/launch_template.tf index f021e0e774..70c609c0cd 100644 --- a/modules/node_groups/launch_template.tf +++ b/modules/node_groups/launch_template.tf @@ -1,25 +1,16 @@ data "cloudinit_config" "workers_userdata" { - for_each = { for k, v in local.node_groups_expanded : k => v if v["create_launch_template"] } + for_each = { + for k, v in local.node_groups_expanded : k => v + if v["create_launch_template"] && contains(keys(v["user_data"]), "mime_type") + } gzip = false base64_encode = true boundary = "//" part { - content_type = lookup(each.value["user_data"], "mime_type", "text/x-shellscript") - content = templatefile(each.value["user_data"]["template_file"], - merge({ - kubelet_extra_args = each.value["kubelet_extra_args"] - pre_userdata = each.value["pre_userdata"] - ami_id = lookup(each.value, "ami_id", "") - ami_is_eks_optimized = each.value["ami_is_eks_optimized"] - cluster_name = var.cluster_name - cluster_endpoint = var.cluster_endpoint - cluster_auth_base64 = var.cluster_auth_base64 - capacity_type = lookup(each.value, "capacity_type", "ON_DEMAND") - append_labels = length(lookup(each.value, "k8s_labels", {})) > 0 ? ",${join(",", [for k, v in lookup(each.value, "k8s_labels", {}) : "${k}=${v}"])}" : "" - }, lookup(each.value["user_data"], "template_extra_args", {})) - ) + content_type = each.value["user_data"]["mime_type"] + content = local.node_groups_userdata[each.key] } } @@ -80,7 +71,11 @@ resource "aws_launch_template" "workers" { # # (optionally you can use https://registry.terraform.io/providers/hashicorp/cloudinit/latest/docs/data-sources/cloudinit_config to render the script, example: https://github.com/terraform-aws-modules/terraform-aws-eks/pull/997#issuecomment-705286151) - user_data = data.cloudinit_config.workers_userdata[each.key].rendered + user_data = ( + contains(keys(data.cloudinit_config.workers_userdata), each.key) + ? data.cloudinit_config.workers_userdata[each.key].rendered + : base64encode(local.node_groups_userdata[each.key]) + ) key_name = lookup(each.value, "key_name", null) diff --git a/modules/node_groups/locals.tf b/modules/node_groups/locals.tf index d2b49ef470..303acb09cd 100644 --- a/modules/node_groups/locals.tf +++ b/modules/node_groups/locals.tf @@ -52,4 +52,23 @@ locals { join("-", [var.cluster_name, k]) ) ) } + + node_groups_userdata = { for k, v in local.node_groups_expanded : k => templatefile( + v["user_data"]["template_file"], + merge( + { + kubelet_extra_args = v["kubelet_extra_args"] + pre_userdata = v["pre_userdata"] + ami_id = lookup(v, "ami_id", "") + ami_is_eks_optimized = v["ami_is_eks_optimized"] + cluster_name = var.cluster_name + cluster_endpoint = var.cluster_endpoint + cluster_auth_base64 = var.cluster_auth_base64 + capacity_type = lookup(v, "capacity_type", "ON_DEMAND") + append_labels = length(lookup(v, "k8s_labels", {})) > 0 ? ",${join(",", [for key, value in lookup(v, "k8s_labels", {}) : "${key}=${value}"])}" : "" + }, + lookup(v["user_data"], "template_extra_args", {}) + ) + ) if v["create_launch_template"] + } }