Terraform doesn’t have any built-in features to automatically select different approaches in response to errors. Terraform’s philosophy is to aim to do what is asked of it or produce an error explaining why not so that a human operator can select an appropriate course of action.
If you have a requirement to dynamically select an instance type based on availability at a particular time then this will require the addition of some system outside of Terraform.
Although Terraform can manage individual EC2 instances directly, in most cases we recommend to use Terraform to create an
aws_autoscaling_group and then let AWS autoscaling manage the individual instances. Because autoscaling is a long-running service rather than a one-shot command line tool, it is able to respond automatically to changes in the running infrastructure and, in particular for this use-case, to changes in availability of instances of particular types.
You could use prioritized instance types in a launch template to instruct AWS autoscaling to choose from one of a prioritized list of instance types. By doing this with autoscaling, you also get the benefit of its automatic handling of failed instances, so that a failed instance can potentially be replaced quickly by another instance (possibly of a different type, depending on availability) rather than waiting until the next Terraform run.