'How can I name eks worker nodes provisioned with terraform?

I am using terraform 12.20.0 and I have provisioned an EKS cluster with 2 node groups.

How can I add name tags to EKS node workers according to their node group names?

I have tried adding "Name" tag in the additional tag sections of each node-group but the tags did not take and my EC2 instance names are empty, while other tags appear.

Here is the configuration - I have skipped the less relevant bits:

module "eks-cluster" {
...
  node_groups_defaults = {
    disk_size = 128
    key_name  = var.key_name
    subnets = [
      aws_subnet.{{}}.id,
      aws_subnet.{{}}.id,
    ]
    k8s_labels = {
      env = var.environment
    }

    additional_tags = {
      env                                             = var.environment
      "k8s.io/cluster-autoscaler/enabled"             = "true"
      "k8s.io/cluster-autoscaler/${var.cluster-name}" = "true"
    }
  }

  node_groups = {
    app = {
      name = "app"

      .....

      k8s_labels = {
        nodegroup = "app"
      }
      additional_tags = {
        nodegroup = "app"
        Name      = "${var.environment}-app-node"
      }
    }
    ml = {
      name = "ml"

     ...
      instance_type = "m5.xlarge"
      k8s_labels = {
        nodegroup = "ml"
      }
      additional_tags = {
        nodegroup = "ml"
        Name      = "${var.environment}-ml-node"
      }
    }
  }

  tags = {
    env = var.environment
  }

  map_roles = [{
    ......
  }]
}


Solution 1:[1]

As per documentation Resource: aws_eks_node_group doesn't allow for modifying tags on your instances.

There is a nice feature coming soon to EKS node groups which will allow you to pass a custom userdata script. Using that you will be able to modify programatically tags for your instances. Issues can be tracked -> https://github.com/aws/containers-roadmap/issues/596

UPDATE: As of 20/08/2020, you can now utilise launch_template with your node group. This will allow you to pass in Name tag. Example:

resource "aws_launch_template" "cluster" {
  image_id               = data.aws_ssm_parameter.cluster.value
  instance_type          = "t3.medium"
  name                   = "eks-launch-template-test"
  update_default_version = true

  tag_specifications {
    resource_type = "instance"

    tags = {
      Name                        = "eks-node-group-instance-name"
    }
  }
} 

Solution 2:[2]

I just noticed that

"k8s.io/cluster-autoscaler/${var.cluster-name}" = "true"

Might need to be

"k8s.io/cluster-autoscaler/${var.cluster-name}" = "owned"

Solution 3:[3]

There is an existing issue with node group to add the "Name" tag on ASG. https://github.com/aws/containers-roadmap/issues/608 (open) and this on terraform end https://github.com/terraform-aws-modules/terraform-aws-eks/issues/860 (closed) However, there is an alternative to use aws cli command to add tag explicitly.

Try below to use in terraform

resource "null_resource" "add_custom_tags_to_asg" {  
  for_each = module.eks-cluster.node_groups

  provisioner "local-exec" {
    command = <<EOF
        aws autoscaling create-or-update-tags \
          --tags ResourceId=${each.value.resources[0].autoscaling_groups[0].name},ResourceType=auto-scaling-group,Key=Name,Value=k8s-node-groups-${each.value.labels["env"]},PropagateAtLaunch=true
    EOF
  }
}

Solution 4:[4]

The following Terraform resource works.

resource "aws_autoscaling_group_tag" "your_group_tag" {    
  autoscaling_group_name = aws_eks_node_group.your_group.resources[0].autoscaling_groups[0].name

  tag {
    key   = "Name"
    value = "enter-your-name-here"

    propagate_at_launch = true
  }

  depends_on = [
    aws_eks_node_group.your_group
  ]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Lee Gaines
Solution 3 Mahattam
Solution 4 ericfossas