简体   繁体   English

terraform输出多个aws ebs_volumes

[英]terraform output multiple aws ebs_volumes

$ terraform --version
Terraform v0.11.7
+ provider.aws v1.28.0

I would like to find out if it's possible to output all the ebs volumes from an AWS instance. 我想找出是否可以从AWS实例输出所有ebs卷。

I have a main.tf with the below: 我有一个main.tf与以下内容:

data "aws_instance" "kafka_nodes" {
  filter {
    name = "tag:Name"
    values = ["mykas00*"]
  }
}

output "block_devs" {
  value = "${data.aws_instance.kafka_nodes.ebs_block_device}"
}

The above only prints one volume but I have verified with AWS CLI that the instance in question has multiple volumes 上面仅显示一个卷,但是我已经通过AWS CLI验证了所涉及的实例具有多个卷

It only prints one volume because you have only set up one volume in the data source . 它仅打印一个卷,因为您仅在数据源中设置了一个卷。 If you check the state file you'll probably see that it has only 1 device listed even though there are multiple devices connected. 如果检查状态文件,即使连接了多个设备,您也可能会看到它仅列出了1个设备。

You need to set up a data source for each volume. 您需要为每个卷设置一个数据源。 But, that implies that you already know about the number of volumes. 但是,这意味着您已经知道卷的数量。 So, that's probably not the way to go. 因此,这可能不是要走的路。

A way to get a list of all volumes attached to any given instance is to use the instance resource along with the instance id, ami, and instance type. 获取所有附加到给定实例的所有卷的列表的方法是使用实​​例资源以及实例ID,AMI和实例类型。 Use terraform import to import the instance into state. 使用terraform import将实例导入状态。 Then use terraform refresh to display all the volumes attached to the instance. 然后使用terraform刷新显示附加到实例的所有卷。

Change 更改

data "aws_instance" "kafka_nodes" {
  filter {
    name = "tag:Name"
    values = ["mykas00*"]
  }
}

output "block_devs" {
  value = "${data.aws_instance.kafka_nodes.ebs_block_device}"
}

to

resource "aws_instance" "kafka_nodes" {
  ami = "<INSTANCE_AMI>"
  instance_type = "<INSTANCE_TYPE>"
  tags {
    Name = "mykas00"
  }
}

output "block_devs" {
  value = ["${aws_instance.kafka_nodes.ebs_block_device}"]
}

Add the below if you also want the root device listed 如果您还希望列出根设备,请添加以下内容

output "root_dev" {
  value = ["${aws_instance.kafka_nodes.root_block_device}"]
}

Then do 然后做

terraform import aws_instance.kafka_nodes [instance_id]    
terraform refresh 

You should see a list of all devices connected to the instance similar to: 您应该看到与实例连接的所有设备的列表,类似于:

block_instance_devs = [
    {
        delete_on_termination = 0,
        device_name = /dev/sdc,
        encrypted = 0,
        iops = 100,
        snapshot_id = ,
        volume_id = vol-0ceea4f464a24d86a,
        volume_size = 8,
        volume_type = gp2
    },
    {
        delete_on_termination = 0,
        device_name = /dev/sdb,
        encrypted = 0,
        iops = 100,
        snapshot_id = ,
        volume_id = vol-0c0608cf0126f0b2b,
        volume_size = 8,
        volume_type = gp2
    },
    {
        delete_on_termination = 0,
        device_name = /dev/sdd,
        encrypted = 0,
        iops = 100,
        snapshot_id = ,
        volume_id = vol-0fe3c4c67bedf0e9e,
        volume_size = 8,
        volume_type = gp2
    }
]

AND even the root device if you added the section for it. 甚至根设备(如果您为其添加了部分)。

    root_dev = [
        {
            delete_on_termination = 1,
            iops = 100,
            volume_id = vol-0197cdd29d212c642,
            volume_size = 8,
            volume_type = gp2
        }
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM