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

Asi*_*mez 3 amazon-ec2 amazon-web-services 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"
        }
}
Run Code Online (Sandbox Code Playgroud)

Leo*_*eon 5

您将无法从中提取 EBS 详细信息,aws_instance因为是 AWS 端向资源提供 EBS 卷。

但是您可以使用一些过滤器来定义 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}"
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处参考 EBS 过滤器: http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-volumes.html