Terraform 为每个创建的 aws_instance 创建 Route53 记录
Terraform Create Route53 record for each aws_instance crreated
我编写了以下 Terraform 代码:
resource "aws_instance" "agents" {
count = 100
key_name = var.Jenkins_Controller_KeyName
instance_type = "t2.micro"
ami = data.aws_ami.latest-amazonlinux2.id
}
我的目标是创建 100 个 Jenkins 代理 EC2 实例。我想为每一个创建 Route53 记录。所以如果我有 count = 100
,它会像这样创建 100 个 A 记录(在伪代码中):
for i in 0..100
create_a_name("worker" + i.to_string)
我如何在 Terraform 中执行此操作?可能吗?
最好使用 aws_autoscaling_group,desired_capacity
和 max_size
为 100,而不是使用 count
。这样可以确保 高可用性和容错 你的奴隶实例。也许需要考虑的事情。
但是无论如何,回答你关于 aws_route53_record
的问题。您可以按照以下方式做一些事情:
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]
}
我编写了以下 Terraform 代码:
resource "aws_instance" "agents" {
count = 100
key_name = var.Jenkins_Controller_KeyName
instance_type = "t2.micro"
ami = data.aws_ami.latest-amazonlinux2.id
}
我的目标是创建 100 个 Jenkins 代理 EC2 实例。我想为每一个创建 Route53 记录。所以如果我有 count = 100
,它会像这样创建 100 个 A 记录(在伪代码中):
for i in 0..100
create_a_name("worker" + i.to_string)
我如何在 Terraform 中执行此操作?可能吗?
最好使用 aws_autoscaling_group,desired_capacity
和 max_size
为 100,而不是使用 count
。这样可以确保 高可用性和容错 你的奴隶实例。也许需要考虑的事情。
但是无论如何,回答你关于 aws_route53_record
的问题。您可以按照以下方式做一些事情:
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]
}