The root volume for an instance is normally created automatically based on an EBS snapshot attached to the selected AMI. Swapping to a different root volume while the instance is already running sounds like a strange requirement that would in effect be like unplugging the main hard drive of your server while it’s running. In principle you can set things up to make that work (making sure everything is loaded into RAM already, for example) but it’s not a common or straightforward operation.
Perhaps instead you could consider creating an AMI that has a very simple root image which is set up to poll until a second volume becomes attached dynamically and then, once it is attached, use
pivot_root to swap to that new volume as the root filesystem while leaving the original volume still attached but now idle.
The above process is similar to how a Linux system might boot from an
initrd, aside from the extra requirement of waiting for the secondary volume to be dynamically attached. It’s quite an advanced system configuration that will require some detailed understanding of the Linux boot process to pull off, so I can’t give specific guidance on it here, but I suspect it’s the closest you could get to your intended goal on EC2. Note that this is now more of a Linux configuration question than a Terraform question; you can use Terraform to attach the volume but what happens after that is up to the software running in the EC2 instance.
If you think about your underlying goal rather than this specific strategy to achieve it then you might find a less complicated solution. If you can find a solution that doesn’t require swapping out the root filesystem at all (e.g. one that can use a secondary filesystem in a more “normal” way) then I expect you’ll be able to achieve that more easily with existing components, rather than having to develop a new base AMI yourself.