1

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.

Piotr
  • 173
  • 1
  • 2
  • 11
  • I would use data external to collect the data into a JSON witch will be translated into map by terraform and use count to loop through it and create the DNS records. – victor m Feb 08 '19 at 15:43
  • Can you share the code you have so far? Are you creating the SRV records at the same time as the instances or separately? Also have you considered using Route53 Service Discovery instead of trying to implement this yourself? – ydaetskcoR Feb 08 '19 at 16:31

1 Answers1

1

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)

Philip Patrick
  • 311
  • 4
  • 6