How to fetch hosted zone names on AWS?

Hello,

We have 2 Terraform managed hosted zones in each of our AWS accounts, and I would like to fetch a specific hosted zone name based on a query.

Hosted zones looks like:

  • dev.<team>.company.services
  • test.<team>.company.services

The <team> is interchangeable (which is why I’m trying to fetch the zone name rather than statically setting it somewhere).

I want to fetch the test.<team>.company.services (so my search query is test?).

I’m fairly new to Terraform and I’ve tried searching the web for a similar answer but couldn’t find anything useful to what I need to do.

Thanks,

Fadi

EDIT: Is this even possible, anyone? :frowning:

When you say “fetch” what are you meaning? Are you meaning you want to get some details about the Route53 zones? If so, take a look at the aws_route53_zone data source: Terraform Registry

Yes, basically I want to grab a hosted zone name from AWS if it contains the word “test”.

So something similar to listing hosted zone names on aws cli and then grepping for one that contains “test”.

As far as I can see, there’s no filtering option for aws_route53_zone data source, so not sure how I’d be able to filter for test.<team>.company.services?

That isn’t possible. The aws_route53_zone requires you to pass in either the zone_id or name. Do you not know the <team> value in your example, so you could construct the full name?

That’s the problem… Team name could be anything (depending on which AWS account they’re accessing, and we have A LOT). And our Terraform file needs to be generic when it comes to that, which is why I was trying to see if there’s a way to fetch the zone names from AWS directly just like how it can be done via their API.

I assume that you must have the current team available in your module somehow. For example, perhaps you have an input variable so you can get the current team name using var.team.

If so, you should be able to use that as part of your query to the data source:

data "aws_route53_zone" "example" {
  name = "dev.${var.team}.company.services"
  # ...
}

I am looking to solve this same issue. We have 100+ accounts to pull hosted zones from. We are creating cnames in modules for various services and do a data source to get hosted zones to input zone id into cname resource. All of the modules have variables in the data source for name that use lookup function to convert it to another value. This isn’t practical because each time we create a new account, we need to add the hosted zone for that VPC to the variable we do the lookup for. Having to update the variable each time we create a new account is not practical. Hosted zone data source requires either name or ID and neither are known in a module that is not account specific. The data source needs tag filters that don’t require name to be inserted.

Basically, we need a better way to retrieve the hosted zone without having to know zone name or zone id.