Ryan Glenn
Ryan Glenn

Reputation: 1416

Terraform Create Route53 record for each aws_instance crreated

I have written the following Terraform code:

resource "aws_instance" "agents" {
  count = 100
  key_name = var.Jenkins_Controller_KeyName

  instance_type = "t2.micro"

  ami = data.aws_ami.latest-amazonlinux2.id
}

My goal is to create 100 Jenkins agent EC2 instances. I would like to create Route53 records for each one. So if I have count = 100, it would create 100 A records like this (in pseudo code):

for i in 0..100
   create_a_name("worker" + i.to_string)

How can I do this in Terraform? Is it possible?

Upvotes: 2

Views: 1603

Answers (1)

Marcin
Marcin

Reputation: 238209

Instead of using count it may be better to use aws_autoscaling_group with desired_capacity and max_size of 100. This way ensures high-availability and fault tolerance your slave instances. A something to consider perhaps.

But anyway, to answer your question regarding aws_route53_record. You can do something along these lines:

resource "aws_route53_record" "www" {
 
  count = length(aws_instance.agents)
 
  zone_id = aws_route53_zone.primary.zone_id
  name    = "worker${count.index}.example.com"
  type    = "A"
  ttl     = "300"

  # not clear from your question if you want to use public or private ip?
  records = [aws_instance.agents[count.index].public_ip]
}

Upvotes: 2

Related Questions