Terraform:从aws_instance资源获取volume_id

时间:2017-07-10 18:16:54

标签: amazon-web-services terraform

terraform是否可以从aws_ebs_volume ebs_block_device资源获取卷ID,还是需要显式调用aws_volume_attachment / resource "aws_instance" "ec2_app" { ... ebs_block_device { device_name = "${var.app_ebs_device_name}" volume_type = "${var.app_ebs_vol_type}" volume_size = "${var.app_ebs_vol_size}" delete_on_termination = "${var.app_ebs_delete_on_termination}" encrypted = "${var.app_ebs_encrypted}" } ... } 资源?

我目前拥有的是:

aws_ebs_volume

我知道我可以更改为aws_volume_attachment / setTimeout()资源,但我相信这会破坏并重新创建音量(我试图避免)。

2 个答案:

答案 0 :(得分:0)

文档在这一点上有点误导,但您可以像这样获得ebs_block_device的卷ID:

"${lookup(aws_instance.example.ebs_block_device[0], "volume_id")}"

答案 1 :(得分:-1)

假设您已创建了一个卷,如下所示:

resource "aws_ebs_volume" "ebs-volume-1" {
    availability_zone = "eu-west-1a"
    size = 8
    type = "gp2"
    tags {
        Name = "extra volume data"
    }

您可以通过指定

来获取卷ID
volume_id = "${aws_ebs_volume.ebs-volume-1.id}"