我使用默认的CentOS 7 AMI创建实例。此AMI自动创建卷并附加到实例。是否可以使用terraform读取该卷ID?我使用下一个代码创建实例:
resource "aws_instance" "DCOS-master3" {
ami = "${var.aws_centos_ami}"
availability_zone = "eu-west-1b"
instance_type = "t2.medium"
key_name = "${var.aws_key_name}"
security_groups = ["${aws_security_group.bastion.id}"]
associate_public_ip_address = true
private_ip = "10.0.0.13"
source_dest_check = false
subnet_id = "${aws_subnet.eu-west-1b-public.id}"
tags {
Name = "master3"
}
}
答案 0 :(得分:2)
您将无法从aws_instance
中提取EBS详细信息,因为它是为资源提供EBS卷的AWS端。
但您可以使用一些过滤器定义EBS data source
。
data "aws_ebs_volume" "ebs_volume" {
most_recent = true
filter {
name = "attachment.instance-id"
values = ["${aws_instance.DCOS-master3.id}"]
}
}
output "ebs_volume_id" {
value = "${data.aws_ebs_volume.ebs_volume.id}"
}
您可以在此处参考EBS过滤器: http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-volumes.html
答案 1 :(得分:0)
您可以:aws_instance.DCOS-master3.root_block_device.0.volume_id
如Terraform docs中所述:
对于任何
root_block_device
和ebs_block_device
,volume_id
被导出。例如aws_instance.web.root_block_device.0.volume_id
答案 2 :(得分:0)
output "volume-id-C" {
description = "root volume-id"
#get the root volume id form the instance
value = element(tolist(data.aws_instance.DCOS-master3.root_block_device.*.volume_id),0)
}
output "volume-id-D" {
description = "ebs-volume-id"
#get the 1st esb volume id form the instance
value = element(tolist(data.aws_instance.DCOS-master3.ebs_block_device.*.volume_id),0)
}