How to fetch hosted zone names on AWS?


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>
  • test.<team>

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> (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.



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>

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 three current team available in your module somehow. For example, perhaps you have an input variable so you can get the current team name using

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.${}"
  # ...