I want EC2 instances to auto-terminate after 24h.

I do this with a script that is executed at instance launch:

shutdown | at now + 24 hours

The instance shuts down and the EBS volumes are terminated when the instance is terminated, so that's fine.

In the console the instance is indicated as not-reachable for some time, until it is declared as terminated. I wonder if it is bad practice to shut down an instance this way, and if terminating it with AWS CLI would be better.

The docs say:

When an EC2 instance is terminated using the terminate-instances command, the following is registered at the OS level:

  • The API request will send a button press event to the guest.

  • Various system services will be stopped as a result of the button press event. systemd handles a graceful shutdown of the system. Graceful shutdown is triggered by the ACPI shutdown button press event from the hypervisor.

  • ACPI shutdown will be initiated.
  • The instance will shut down when the graceful shutdown process exits. There is no configurable OS shutdown time.

The instance is in an auto-scaling group that runs a REST web service, so there are most likely requests just being executed.

  • What happens to requests that are still being executed? (The REST service has a 30 seconds timeout, so requests are not running longer than that.)
  • Is a termination with shutdown less orderly than with the AWS CLI or a termination by the auto-scaling group?

1 Answer 1


Use the detach-instances api to remove the instance from the autoscale group 1-2 minutes before your shutdown. The detach-instances api will also remove the instance from a load balancer if attached, see https://docs.aws.amazon.com/en_pv/autoscaling/ec2/userguide/detach-instance-asg.html for more information.


aws autoscaling detach-instances --instance-ids i-05b4f7d5be44822a6 \ --auto-scaling-group-name my-asg --should-decrement-desired-capacity

If you don't remove the instance from the load balancer, any inflight requests running on the instance will get an error, as the load balancer will see a connection closed event when the tcp connection is closed by the instance's OS when the listening process is terminated.

So yes, termination by shutdown is less orderly than detaching from the ASG.

  • Very important, thanks for pointing this out. So if I detach the instance before terminating it, is it the same the auto-scaling "downscale" termination?
    – Manuel
    Oct 12, 2019 at 16:39
  • Yes. Please accept the answer so I get reputation credit. Oct 27, 2019 at 23:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .