Amazon web services Terraform将卷分配给AWS实例列表
我已经使用Terraform创建了AWS实例列表:Amazon web services Terraform将卷分配给AWS实例列表,amazon-web-services,terraform,volumes,Amazon Web Services,Terraform,Volumes,我已经使用Terraform创建了AWS实例列表: resource "aws_instance" "masters" { count = 2 ami = "${var.aws_centos_ami}" instance_type = "t2.micro" availability_zone = "eu-west-1b" tags { Name = "master-${count.index}" } } 如何像在
resource "aws_instance" "masters" {
count = 2
ami = "${var.aws_centos_ami}"
instance_type = "t2.micro"
availability_zone = "eu-west-1b"
tags {
Name = "master-${count.index}"
}
}
如何像在循环中一样将卷分配给该实例?
我只是尝试使用下一个:
data "aws_ebs_volume" "masters_ebs_volume" {
most_recent = true
filter {
name = "attachment.instance-id"
values = ["${aws_instance.masters.*.id}"]
}
}
但我不认为它工作得很好,因为当我尝试在文件中写入AWS卷时,它只写入一个卷名
provisioner "local-exec" {
command = "echo \"${join("\n", data.aws_ebs_volume.masters_ebs_volume.*.id)}\" >> volumes"
}
我尝试过这样定义音量:
data "aws_ebs_volume" "masters_ebs_volume" {
count = 2
# most_recent = true
filter {
name = "attachment.instance-id"
values = ["${aws_instance.masters.*.id}"]
}
}
但它抛出了下一个错误:
data.aws_ebs_volume.masters_ebs_volume.0: Your query returned more than one result. Please try a more specific search criteria, or set `most_recent` attribute to true.
您需要告诉它哪个实例具体映射到哪个卷。您可以通过以下方式实现:
data "aws_ebs_volume" "masters_ebs_volume" {
count = 2
filter {
name = "attachment.instance-id"
values = ["${element(aws_instance.masters.*.id, count.index)}"]
}
}