1
votes

I create VPC and subnets which I add tags.

Later, I create EKS cluster which appends its own tags and if I apply again, the tags are overwritten.

I need any method to read the current tags and then merge with my custom tags. The problem is if the VPC resources are being created for the first time I can't query if some tags exists.

Here is my subnets definition

resource "aws_subnet" "k8s" {
  count = "${var.create_vpc && length(var.k8s_subnets) > 0 ? length(var.k8s_subnets) : 0}"

  vpc_id            = "${local.vpc_id}"
  cidr_block        = "${var.k8s_subnets[count.index]}"
  availability_zone = "${element(var.azs, count.index)}"

  tags = "${merge(map("Name", format("subnet-%s-${var.k8s_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.tags, var.k8s_subnet_tags)}"
}

This is the tag EKS adds:

kubernetes.io/cluster/eks-cluster : shared

I'm stuck with this kind of... which comes first, the chicken or the egg? Any idea or suggestion?

-- Edited

something like self.tags could be the solution but unfortunately is not possible:

self.ATTRIBUTE syntax is only allowed and valid within provisioners.

and it shows an error:

Error: resource 'aws_subnet.k8s' config: cannot contain self-reference self.tags
1
If the tag that gets added by EKS Is deterministic, I'd simply include it within Terraform.bkconrad
I'm creating some kind of infrastructure bootstrap for new projects. I have completely separated modules (in fact each one is in a different git repository) so I create all the necessary network infrastructure and maybe a month later I create the EKS cluster so I don't know previously even the name of the cluster. That's the reason it could be great the possibility to append tags :)RuBiCK

1 Answers

1
votes

This is what I do:

  1. I define the common tags in env.sh which wraps terraform
  2. When creating another component, I do this: tags = "${merge(var.default_tags, map("Name", format("%s-Jenkins-ELB", var.env)))}"
  3. You could write the VPC tags to SSM parameter store and retrieve them later to be use by the EKS cluster.