Terraform import volumes assigned to instances list

455 views
Skip to first unread message

Asier Gomez

unread,
Feb 9, 2018, 4:45:58 AM2/9/18
to Terraform

I have configured a resource for instances list:


resource "aws_instance" "masters" {
    count = "${var.masters_count}"
    ami = "${var.aws_centos_ami}"
    instance_type = "t2.micro"
    key_name = "${var.aws_key_name}"
    vpc_security_group_ids = ["${aws_security_group.bastion.id}"]
    associate_public_ip_address = true
    source_dest_check = false
    subnet_id = "${aws_subnet.eu-west-1b-public.id}"

    availability_zone = "eu-west-1b"

    tags {
            Name = "master-${count.index}"
        }
}

Then I attach volumes to that instances:


data "aws_ebs_volume" "masters_ebs_volume" {
    count = "${var.masters_count}"
    filter {
      name   = "attachment.instance-id"
      values = ["${element(aws_instance.masters.*.id, count.index)}"]
    }
}


Now I am trying to import the volumes to terraform. I just try using:


terraform import aws_ebs_volume.data[0] vol-1153438514541
terraform import aws_ebs_volume.masters_ebs_volume[0] vol-1153438514541
terraform import aws_ebs_volume.masters[0] vol-1153438514541
terraform import aws_ebs_volume.data vol-1153438514541
terraform import aws_ebs_volume.masters vol-1153438514541

But nothing works.. how can I import that volumes?


Stephen Eilert

unread,
Feb 9, 2018, 9:15:30 PM2/9/18
to Terraform
Hi,

data "aws_ebs_volume" "masters_ebs_volume" {
    count = "${var.masters_count}"
    filter {
      name   = "attachment.instance-id"
      values = ["${element(aws_instance.masters.*.id, count.index)}"]
    }
}

You are not attaching the volumes, this is only retrieving data about your instance volumes. What you are looking for is "aws_volume_attachment", here: https://www.terraform.io/docs/providers/aws/r/volume_attachment.html 

Anything under "data" is a data source, for retrieving information only.


Thanks,

— Stephen
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/59a8cda0-bab4-405e-900b-131eec7743f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Asier Gomez

unread,
Feb 12, 2018, 5:23:20 AM2/12/18
to Terraform
Hello,

Really thanks, but what I want is to import the volumes of the AWS instances, which data I can have the information when it is only one volume and one instance.
If I use https://www.terraform.io/docs/providers/aws/r/volume_attachment.html how can I attach one volume per instance, when I am using a list of instances with   count = "${var.masters_count}"?

Thanks!

Lowe Schmidt

unread,
Feb 12, 2018, 8:40:43 AM2/12/18
to terrafo...@googlegroups.com
You can't get the EBS root volume data out of an instance (see the attributes reference here https://www.terraform.io/docs/providers/aws/r/instance.html#attributes-reference )

If you want to attach a secondary EBS volume to the instance, you need to create an EBS volume in terraform and then attach it to the instance as described previously. 

--
Lowe Schmidt | +46 723 867 157

To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/896ead40-c5f3-4fb5-850f-9bc2f46ed998%40googlegroups.com.

Asier Gomez

unread,
Feb 13, 2018, 3:03:53 AM2/13/18
to Terraform
Really thanks,

I only want one volume per instance, but if the volume change outside Terraform, the delete-on-termination works?

Asier Gomez

unread,
Feb 13, 2018, 4:51:09 AM2/13/18
to Terraform
Thanks!! It works with the delete_on_terminaton = true, I thought  that only works if the volume doesn't change externally, but it works too.
So really thanks


El lunes, 12 de febrero de 2018, 14:40:43 (UTC+1), Lowe Schmidt escribió:
Reply all
Reply to author
Forward
0 new messages