Safeguards for EC2 against unnecessary usage?

Hi, I have an expensive EC2 instance that I need to run for brief periods of time, and I’m wondering, are there any safeguards I can put in place in case I accidentally leave the instance running unnecessarily? For example, some sort of limit on total monthly expenses before services are turned off (I don’t have any user-facing services on this account) or some sort of alarm I can set for if the instance is running for > X hours.

There are a few options, depending on your use case really.

  1. Specific scheduled start/stop times - e.g.
  2. If it’s a linux box that you log into, you can get systemd to shutdown your server when nobody is logged in. E.g. my dev server has a #cloud-init that includes the following to shutdown the server after 90m if I’m not logged in:
    permissions: 0644
    content: |
1. You may be able to set a CloudWatch alarm on CPU to trigger an instance stop. E.g. []( (first google result, not a big fan of click-through screenshot tutorials, but it should help get you started)

Oh, one more…

Implement a scheduled Lambda that checks the launch_time for your instance and triggers a shutdown if it’s been up for too long. E.g. this python code shows how to use boto3 to determine how long an instance has been up for.

We do something similar to this for EMR jobs, we tag jobs with a timeout or as long-running, and have a job that periodically scans active clusters and kills any that have timed out or aren’t tagged correctly


We can also use Billing Alarms and in combination with Create alarms to stop, terminate, reboot, or recover an EC2 instance likely you be able to achieve the desired result.

Thanks for all the ideas!