Complex String manipulation for a local, including variable(s)

What I have
A complex string manipulation sprinkled throughout a module (domain variable) representing a top-level domain string:

module "http-rdir-A-records" {
  source = "./modules/aws/create-dns-record"
  depends_on = [module.http-rdir]
  count = var.amount

  domain = join(".",tolist([reverse(split(".",var.domain-mappings[count.index]))[1],reverse(split(".",var.domain-mappings[count.index]))[0]]))
  type = "A"
  records = {
    "${var.domain-mappings[count.index]}" = module.http-rdir[count.index].http-rdr-public-ip
  }
}

What I’ve tried
To abstract the variable with locals. For example:

locals{
    tld = join(".",tolist([reverse(split(".", "${var.domain-mappings[count.index]}"))[1],reverse(split(".","${var.domain-mappings[count.index]}"))[0]]))
}

And

locals{
    tld = join(".",tolist([reverse(split(".", "$${var.domain-mappings[count.index]}"))[1],reverse(split(".","$${var.domain-mappings[count.index]}"))[0]]))
}

What I Get Using locals
Unexpected string outputs, such as

“domain-mappings[count.index]}”

Am I interpolating the strings incorrectly? domain-mappings is defined as follows:

variable "domain-mappings" {
  type = list(string)
  default = [
    ""
  ]

Thank you!
Joe

Hi @joeminicucci,
why are you using double $$ in your local example?
Apart from that, I don’t get your question. Are you trying to prepare the records section ?
Do you have examples of the data structures?

@tbugfinder

The double $$ interpolation was a leftover of me trying to find a cause as to why the local was not working. The problem is that when I in-line the string manipulation it works, but with locals is does not. The intention is to use a single local rather than sprinkling this large line of code throughout the module.

The data it would be trying to manipulate is any domain string, example would be example.google.com and the manipulation should yield the TLD as google.com

I don’t think count.index is available within locals.
So any values in var.domain-mappings have to be processed before and then within the module call you could use it like local.tld[count.index]. However there’s a dependency on the order between both “variables” so it might be better to use a map.

With these two points:

So any values in var.domain-mappings have to be processed before and then within the module call you could use it like local.tld[count.index]

there’s a dependency on the order between both “variables”

are you indicating it is not possible? I am not sure I understand your analysis.

Thank you,
Joe

try this.

locals{
    tld = [ for k in var.domain-mappings : join(".",tolist([reverse(split(".", k ))[1],reverse(split(".",k ))[0]])) ]
}

While referencing tld[count.index] and var.domain-mappings[count.index] in the module call, it’s assumed that the order is the same in both lists.
Therefore a single map for both lists might be better.

1 Like

This is a great solution, and in the future I’ll be sure not to reference count in the locals. Thanks @tbugfinder!