Getting domain name from string

Can I ask help with getting domain name from string please ?
For ex:
for x.domain.com I want domain.com
for x.x.domain.com I want domain.com
for *.x.domain.com I want domain.com

In short, regardless of how deep is the domain, i want to get domain.com.

Thanks.

Is it always going to be a domain.com type domain, or could it also be things like domain.co.uk?

The @stuart-c . Thanks for replying. It could be anything like domain.com, domain.us or domain.co.uk

Then, you cannot do this via purely algorithmic string processing.

You need to refer to the Public Suffix List: https://publicsuffix.org/

Data processing of this complexity is best performed in you own custom code before you invoke Terraform - then feed Terraform the processed data via variables or JSON/YAML files.

1 Like

A very simple code, which is working for Top-level domain’s and in case of the Second-level domain’s, should be updated

locals {
  input = "domain.com"
  # input = "x.domain.com"
  # input = "x.x.domain.com"
  # input = "*.x.domain.com"
  output = join(".", slice(split(".", local.input), length(split(".", local.input))-2, length(split(".", local.input))))
}

output "output" {
  value = local.output
}
Outputs:

output = "domain.com"
# output = "domain.com"
# output = "domain.com"
# output = "domain.com"