Search code examples
amazon-web-servicesterraformamazon-route53srv

How to create multi-value SRV DNS record with terraform in AWS Route53 service?


I am trying to create multi-value SRV DNS entry in AWS route53 service via terraform. Values should be taken from instances tags. Due to the fact, that this is only one record, approach with count is not applicable.

The trick is, that I have 10 instances but they need to be filtered first by finding specific tags. Based on resultlist, SRV record should be created by using the Name tag assigned to each instance.

Any idea how to approach this issue?

Thanks in advance for any tip.


Solution

  • I did it like this:

    resource "aws_instance" "myservers" {
        count = 3
        #.... other configuration....
    }
    
    resource "aws_route53_record" "srv" {
        zone_id = aws_route53_zone.myzone.zone_id
        name = "_service"
        type = "SRV"
        records = [for server in data.aws_instance.myservers : "0 10 5000 ${server.private_ip}."]
    }
    

    Terraform's for expression is being the key for the solution.

    Regarding the SRV record in AWS Route 53, it should have a line per server and each line in the following form: priority weight port target (space is the delimiter). For the example above: 0 is the priority, 10 is the weight, 5000 is the port and the last one is the server IP (or name)