Terraform is intended primarily for long-lived infrastructure which you create once and then maintain indefinitely, until requirements later change and you alter your architecture to respond to that.
Terraform is not so well suited to the sort of direct, imperative orchestration you need in order to work through the series of steps to create a new AMI as a snapshot of a temporary instance. While I expect you could make it work in principle, you’d be working against Terraform’s design and so the result is likely to be complicated and brittle.
There are other tools better suited to the sort of orchestration you are talking about. You can either script it directly using the AWS API/CLI, or you can use something like HashiCorp Packer for a higher-level approach. The Amazon AMI builders for Packer are aimed directly at the problem you are describing.
If you choose to try Packer and have questions about it, you can start a topic in Packer’s community forum, which is another discussion category here on this site.