Hi ,
I have created an ec2 instance with a ebs volume and attached it using CDKTF. now i want to destroy the instance but it is showing following error in volume detachment:
Error waiting for Volume (vol-09c8cf55bbfd9606e) to detach from Instance (i-02705bb1ac35b4fdf): unexpected state 'busy', wanted target 'detached'. last error: %!s(<nil>)
Now, i added “stop_instance_before_detaching: true” in volume attachment config and updated the instance.
After this again i tried to destroy instance and got following error in volume detachment:
Failed to detach Volume (vol-09c8cf55bbfd9606e) from Instance (i-02705bb1ac35b4fdf): IncorrectState: Volume 'vol-09c8cf55bbfd9606e' is in the 'available' state.
│ status code: 400, request id: 5fda117a-0fa0-4415-87b3-d5c0807d8930
How to solve this ? can anyone help ?
I believe the last error means that the volume has already detached. Perhaps the first operation left things in a bad state. Perhaps try from a fresh state with the updated config and see if it happens again.
Which classes/resources are you using? How are they linked? Since there are a couple different ways to add volumes that may help see where the issue is.
I am using ec2.instance
to create instance. ec2.EbsVolume
to create ebs volume and ec2.VolumeAttachment
to attach created volume with created instance.
these 3 are wrapped in a class and i am calling that class.
Yeah i also observed that it left the volume in bad state. to resolve that i have used forceDetach: true
in volumeattachment config. But TF docs says it is last resort. do i have any other better alternative ?
Please let me know if we have any better alternative to handle these situation. My basic use case is to create an instance and after creation i should be able to add and delete external
volumes.
stop_instance_before_detaching
looks like it was added to solve the issue you initially described. I’m not sure why it’s still ending up in a bad state.
If you can manually, or by using a null resource with a remote-exec provisioner, unmount the volume from within the instance that may help.
Otherwise, you may want to try asking in the aws provider discuss forum or github.
how would i run null provider during deletion of an instance because to delete we just remove the ec2 Instance call from code. if there is not any ec2 instance call then i won’t be having instance details to run null provider on it ?
You might be able to use a provisioner instead. See here.
I need the IP of instance on which i want to run commands using remote exec. But when i am destroying the instance, how would i get privateIP because to destroy an instance i am just not calling ec2.Instance for that ?
You’ll probably need to set the instances count to 0 rather than directly removing in order to run commands when destroying. Provisioners | Terraform by HashiCorp has some additional information.
You could also store the ip somewhere else that will still be available.