Importing a volume with attachment

1,119 views
Skip to first unread message

Gil Dafnai

unread,
Apr 17, 2017, 5:09:18 PM4/17/17
to Terraform
Hi

TL;DR
If a configuration file contains an aws_instance, aws_ebs_volum and aws_volume_attachment, is there any way to terminate the instance and the volume, then importing a new instance and (an already attached) volume, and still prevent terraform from creating a new aws_ebs_attachment?
 
Longer version:
I am creating a small service for setting up and maintaining a cluster of ec2-instances, and i am using terraform for creating and updating the aws components.
To put things simply: a user can specify the number of instances and the type for each instance, and the volume size (ebs-backed). Note that each instance has a unique role, so i cant just specify (for example) count=3 in the config file, therefore each instance is named uniquely (fro example instance1, instance2 and instance3).

now lets assume, that instance3 has died, and should be replaced.
I want to be able to:
1. manually create a new instance,
2. manually attach it to a new ebs volume
3. manually install my very important software
4. import the new instance and ebs volume to my state file as instance4.
5. Verify programmatically that terraform isnt going to create or destroy anything.

the problem is that terraform insists on recreating the aws_ebs_attachement, even though the instance and the volume are already attached.
is there any way to let terraform "know" that the instance and the volume are already attached?

thanks
Message has been deleted

patrock

unread,
Apr 18, 2017, 2:52:58 PM4/18/17
to Terraform
Hi,

I am attempting to accomplish similar.  In my case I with too:

1.)  Create aws instances based on configurable number
2.)  Associate these instances to ebs storage
3.)  If instances are terminated I want to retain the storage
4.)  Desire that the terminated instance replaced and re-associate to pre-existing ebs volume In case of terraform apply after 3 occurs.

My first attempt to accomplish this I noticed the same behavior you describe when using the aws_instance ebs_volume.  I then used the ebs_volume resource in separate terraform.  Then used the datasource ebs_volume to query existing ebs_volume by tag and use the aws_volume_attachement.  Unfortunately,  I desire behavior similar to datasource availability_zones which returns a list that can be associated by count.  The ebs_volume datasource is designed to return only one similar to availability_zone.   Some discussion of similar issues here https://github.com/hashicorp/terraform/issues/10123

So as it stands I don't see a way to accomplish what we desire.  You must create a ebs_volume datasource query and make the association explicitly (without a count variable) as far as I can tell.

I'm looking into the source code now.  It seems to me that an enhancement to aws_instance would be ideal.   

 A way to say: If the ebs_volume doesn't exist create it but don't destroy it on termination.  Otherwise,  attempt to re associate it by a given name perhaps.

Alternatively,  a way to query existing ebs_volumes and make associates based on count.

Cheers


I am investigating the source for a possible solution

patrock

unread,
Apr 27, 2017, 2:19:22 PM4/27/17
to Terraform
After closer examination perhaps my desired case is slightly different then yours Gil.  We are similar in that I seem to be seeing aws_volume_attachment get recreated necessarily.   Here is a potential enhancement for what I'm attempting to achieve.  Interested in any feedback or alternatives out there.   After coding this tho,  I think what I really want is an enhanced aws_instance resource.

Turbo Fredriksson

unread,
Apr 30, 2017, 1:34:20 PM4/30/17
to Terraform
On Monday, April 17, 2017 at 10:09:18 PM UTC+1, Gil Dafnai wrote:
If a configuration file contains an aws_instance, aws_ebs_volum and aws_volume_attachment, is there any way to terminate the instance and the volume, then importing a new instance and (an already attached) volume, and still prevent terraform from creating a new aws_ebs_attachment? 

Sure. TF won't touch the ELB resource if it detects that it needs to (re)create the instance. It will then
update the attachment with the already existing ELB to that new instance.

You'll need to update that instance to know how to mount that. I use a user_data script that does this
(and many other things) when the instance(s) boots.
 
I want to be able to:
1. manually create a new instance,
2. manually attach it to a new ebs volume

Point two is automatic once point 1 is done. IF you use terraform to do this for you that is. You'll still need
to update the /etc/fstab on the instance somehow (I use the user_data script mentioned above).
 
3. manually install my very important software

I use Puppet for this, but this could just as well be done in the user_data script as well (if that's ALL you
need to do).
 
the problem is that terraform insists on recreating the aws_ebs_attachement, even though the instance and the volume are already attached. 

It is (was!) attached to the _old_ instance, so of course it will have to recreate the attachment. But the
ELB volume it self is untouched.
Reply all
Reply to author
Forward
0 new messages