Terraform:如何读取一个实例的卷ID?

时间:2017-03-16 11:36:39

标签: amazon-web-services amazon-ec2 terraform

我使用默认的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"
        }
}

3 个答案:

答案 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_deviceebs_block_devicevolume_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)
}