Skip to content

Tagging on ASG resource in EKS cluster using Terraform

0

Hi,

I am currently looking to add tags to existing EKS resources within my cluster. Specifically, I want to add a Name tag, but I have encountered a few issues and would appreciate some guidance.

For context, I want to ensure that I do not remove any existing tags. Here is an example scenario:

• Some resources already have the Name tag, while others do not.

• I do not want to delete the existing Name tags where they are already present.

I referred to this article (Tag issue on aws_autoscaling_group resource – HashiCorp Help Center), which explains some related issues, but I still need further assistance.

When I use for_each to add tags, the existing resources seem to be destroyed and then recreated. Here is an example of the output I see:

'# module.eks_cluster.aws_autoscaling_group_tag.this will be destroyed '# (because resource uses count or for_each)

In my root module, the configuration is as follows:

module "eks_cluster" {
  cluster_name       = <eks-cluster-name>
  node_group_name    = <node group-name>
  eks_version        = "1.xx"
  tags               = merge(local.tags, { "Service" = local.service_name, "CreatedBy" = "terraform" })
  ...
}

The aws_autoscaling_group_tag module is configured similarly to the article. Instead of using count, I tried using for_each to apply the tags, but this results in the resource being removed and recreated, which I want to avoid.

Here is an example configuration for the aws_autoscaling_group_tag resource: (Following is not my child module.)

resource "aws_autoscaling_group_tag" "cluster_owned" {
  count = var.enable_windows_workers ? 1 : 0
  autoscaling_group_name = aws_autoscaling_group.worker_windows[0].name

  tag {
    key   = "kubernetes.io/cluster/${var.cluster_name}"
    value = "owned"
    propagate_at_launch = true
  }
}

Could you please advise on the best way to add tags to existing EKS resources without causing the existing tags to be removed or the resources to be recreated?

Thank you for your assistance.

asked 2 years ago1.5K views

1 Answer
0

Take a look at the notes here: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group_tag

I think you are specifically hitting some of the noted behaviors. Perhaps try to use some of the example code that uses for_each.

AWS
EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.