Map interpolation

I’ve a map(list(string))

domain_names = {
        "foo.com"          = ["*.foo.com","bar.com"]
            "sand.co.uk" = [*.sand.co.uk]
}

How to create a map something like this , just removing the element if it has “*.”

 domain_names = {
         "foo.com"      = ["foo.com","bar.com"]
          "sand.co.uk" = ["sand.co.uk"]
 }

want to do something inside the locals file. I tried

all_domains = zipmap(keys(var.domain_names), [for each in values(var.domain_name): trimprefix(each, "*.")])

Invalid value for “str” parameter: string required.

also

resource "aws_acm_certificate" "certificate" {

  for_each                  = var.domain_names
  domain_name               = each.value 
  subject_alternative_names = each.value # I want to get all my values here like ["*.foo.com","bar.com"]
  validation_method         = var.validation_method

  tags = {
    Name      = each.value
    owner     = "amp"
    terraform = "true"
  }

  lifecycle {
    create_before_destroy = true
  }
}

Hi @skadem07,

I’m not sure if this was a total coincidence or if this was you asking a similar question on Stack Overflow, but I answered a similar thing here yesterday and hopefully the answer is helpful for you:

If you have any follow-up questions, please let me know!

Thanks for the answer, but for some reason when i apply it for a map(list(string)). I’m getting this error. I’m not able to understand what this error mean.

Variables:

domain_names = {
“foo.dev” = [“foo.dev”,"*.foo.dev",“bar.com”]
}

tried both of them

locals {
  all_domains = zipmap(keys(var.domain_names), distinct([for each in values(var.domain_names) : trimprefix(each, "*.")]))
  #  all_domains = zipmap(keys(var.domain_names, [for domain_name in var.domain_names : domain_name if regexall("^\\*\\.", domain_name) == 0])
}

Error:

all_domains = zipmap(keys(var.domain_names), distinct([for each in values(var.domain_names) : trimprefix(each, “*.”)]))

Invalid value for “str” parameter: string required.

all_domains = zipmap(keys(var.domain_names), [for domain_name in var.domain_names : domain_name if regexall("^\*\.", domain_name) == 0])

Invalid value for “string” parameter: string required

I’m afraid I’m having trouble following what’s going on here with only little snippets, so I’m sorry if I’m getting this wrong. Could you please write out what value you’d like local.all_domains to have? I think an example of the output you’re expecting will help me write a working example.

My variable is this

domain_names = {
        "foo.com"          = ["*.foo.com","bar.com"]
         "sand.co.uk"    = [*.sand.co.uk]
}

locals.all_domains should create this, It just should remove *.

all_domains = {
     "foo.com"     = ["foo.com", bar.com"]
    "sand.co.uk" = [sand.co.uk]
}

Thanks for the extra context, @skadem07.

In that case, I think the following should work:

  all_domains = {
    for name, aliases in var.domain_names : name => [
      for alias in aliases : trimprefix(alias, "*.")
    ]
  }

The outer for expression transforms each element of the map, and then the inner for expression transforms each of the elements of the map element’s list value.