Terraform EC2 import bug?

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.

Ok, this gets murkier. My original post was about 0.13.0. In 0.13.0, if you create an aws_instance, aws_ebs_volume, and aws_volume_attachment, the resulting state file shows the ebs_block_devices attribute of the instance as [ ], which is what I’d expect. In 0.13.4, the ebs_block_devices attribute contains the attached volume.

I checked the changelog, and I don’t see anything between 0.13.0 and 0.13.4 that suggests that this should have changed. However, this is exactly the behavior I saw in 0.13.0 for imported hosts (the point of the original post).

Is this the expected behavior? What are the implications with respect to the Note in the documentation about drift with ebs_block_devices?

Thanks for any help.

Just for reference, I ultimately decided to submit this as a bug, and the link for that is here: