简体   繁体   中英

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.

I have a main.tf with the below:

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

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.

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. Use terraform import to import the instance into state. Then use terraform refresh to display all the volumes attached to the instance.

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
        }
    ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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