Create one latest ami of an ec2 instance before destroy

Create one latest ami of an ec2 instance before destroy . And that ami id should print in output . I will replace that latest ami id in the variable file . So that in the next terraform apply ec2 instance will create with latest ami id .How to achieve it .