diff --git a/factory/aws/src/aws.rs b/factory/aws/src/aws.rs index ab8011a..40834d0 100644 --- a/factory/aws/src/aws.rs +++ b/factory/aws/src/aws.rs @@ -94,21 +94,28 @@ async fn destroy_instance( id: &str, force_stop: bool, ) -> Result<()> { - /* - * Before terminating an instance, attempt to initiate a forced shutdown. - * There is regrettably no force flag for termination (!) and if we don't do - * this first, AWS will sometimes wait rather a long (and billable) time - * before actually terminating a guest. It is difficult, as the saying - * goes, to get a man to understand something, when his salary depends upon - * his not understanding it. - */ - if force_stop { - info!(log, "forcing stop of instance {id}..."); - ec2.stop_instances().instance_ids(id).force(true).send().await?; - } - info!(log, "terminating instance {id}..."); - ec2.terminate_instances().instance_ids(id).send().await?; + ec2.terminate_instances() + .instance_ids(id) + /* + * If requested, perform a forced shutdown of the VM before terminating + * it, otherwise AWS waits until the instance stops on its own (still + * billing you for it!). Note that both `force` and `skip_os_shutdown` + * must be enabled for the instance to reliably shut down immediately: + * + * - `force` ensure the instance gets terminated if there is a problem + * with the underlying compute host. + * + * - `skip_os_shutdown` avoids sending an ACPI shutdown signal to the VM + * and immediately stops it, instead of waiting for the instance to + * shut itself down. + * + * https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-stop-methods.html + */ + .set_force(Some(force_stop)) + .set_skip_os_shutdown(Some(force_stop)) + .send() + .await?; Ok(()) }