I recently started using Terraform, and I’ve run into an issue with importing EC2 instances into Terraform. The basic issue is that importing an EC2 instance will include all attached volumes in an ebs_block_device attribute. The Terraform documentation recommends not doing this, so following that guidance, I set up my module to create the volumes and attachments separately (using aws_ebs_volume and aws_volume_attachment).
The volumes behave correctly when I build an instance with Terraform, but even if I import the volumes and attachments to the state file first, Terraform will always add the volumes to the ebs_block_device attribute for the instance. I couldn’t see a way to tell Terraform to NOT do this, and unmounting the drives during the import is not really something that I can do, so…
I wrote a script to remove the devices from the state file directly. The script works, and seems to correctly modify the JSON. Terraform accepts the state file as valid and a plan shows that everything is fine. However, after running a refresh or an apply, the devices get returned to the ebs_block_device attribute. Note that this NEVER happens for non-imported hosts. I’m not sure what Terraform is looking at for imported hosts that is different from created hosts that causes this behavior. As additional information, the hosts in question were created with the exact same module. I just “deleted” the state file, and then tried to replicate the state file using import.
Anyway, I’m somewhat blocked at this point. I can create hosts in Terraform, but I can’t import existing hosts into Terraform, which is pretty problematic. (This is overstated. Of course I can import them, but I’m not sure what the implications are of the duplication of the volumes in the ebs_block_device attribute as well as the aws_ebs_volume + aws_volume_attachment setup)
Is this user error in some way, or is this actually a bug?
Thanks for any guidance.