Terraform: Add Route53 A record automatically for new aws_instance resource

Created on 7 Jun 2015  ยท  7Comments  ยท  Source: hashicorp/terraform

Hi

I used the aws_instance resource and created multiple instances of my app (with the count variable).
But when I tried to add A record for each of them (automatically) I found that I need to create a aws_route53_record resource for each of them manually and the count index couldn't help me.

My question is how can I add a count variable to the aws_route53_record resource (and then implement it on the records field) or maybe adding the aws_route53_record resource into the aws_instance resource like the provision resource so the aws_route53_record will use the count index of the aws_instance?

How can I scale my env without adding new aws_route53_record resource each time?

Following my example,

resource "aws_route53_record" "app1" {
   zone_id = "${aws_route53_zone.main.zone_id}"
   name = "app0${count.index}"
   type = "A"
   ttl = "300"
   records = ["${aws_instance.app.${count.index}.private_ip}"]
}

Thanks

question

Most helpful comment

Hi @panda87 - you can accomplish this by using a combination of the "splat" syntax and the element function:

variable "num_instances" {
  default = 3
}
resource "aws_instance" "app" {
  count = "${var.num_instances}"
  // ...
}
resource "aws_route53_record" "app" {
  // same number of records as instances
  count = "${var.num_instances}"
  zone_id = "${aws_route53_zone.main.zone_id}"
  name = "app0${count.index}"
  type = "A"
  ttl = "300"
  // matches up record N to instance N
  records = ["${element(aws_instance.app.*.private_ip, count.index)}"]
}

You can find a lot more details on interpolation syntax section of the docs.

All 7 comments

Hi @panda87 - you can accomplish this by using a combination of the "splat" syntax and the element function:

variable "num_instances" {
  default = 3
}
resource "aws_instance" "app" {
  count = "${var.num_instances}"
  // ...
}
resource "aws_route53_record" "app" {
  // same number of records as instances
  count = "${var.num_instances}"
  zone_id = "${aws_route53_zone.main.zone_id}"
  name = "app0${count.index}"
  type = "A"
  ttl = "300"
  // matches up record N to instance N
  records = ["${element(aws_instance.app.*.private_ip, count.index)}"]
}

You can find a lot more details on interpolation syntax section of the docs.

Thank you so much Paul!
This is what I look for!

Sorry for commenting on such an old issue, but the terraform provider docs for aws_route53_record don't show a count - is this a documentation oversight?

Hi @jfray - count is a metaparameter available on every resource. https://www.terraform.io/docs/configuration/resources.html#count :+1:

If you want to reformat your records somehow and use a list (e.g. you want to set a CNAME record) for DNS Round Robin you can use formatlist.

resource "aws_route53_record" "app" {
  zone_id = "${aws_route53_zone.main.zone_id}"
  name = "app"
  type = "CNAME"
  ttl = "300"
  records = ["${formatlist("%s.priv.cloud.scaleway.com", aws_instance.app.*.private_ip)}"]
}

Shouldn't something like this work?

resource "aws_route53_record" "app" {
  zone_id = "${aws_route53_zone.main.zone_id}"
  name = "app"
  type = "CNAME"
  ttl = "300"
  records = ["${aws_instance.app.private_ip}"]
}

Unfortunately I get the following error:

* Resource 'aws_instance.app' not found for variable 'aws_instance.app.private_ip'

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings