Is there a way to have a ec2 resource run a command before it is destroyed?

I am creating VM using terraform. Now I want to run some command using remote-exec on that VM before it destroyed.

Is there any way to this?

Thanks

Take a look at the Destroy-Time Provisioners section of the Provisioners documentation. It should accomplish what you’re asking.