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.
- Specific scheduled start/stop times - e.g. https://towardsaws.com/automating-an-ec2-schedule-with-python-lambda-and-cloudwatch-641746e2e65a
- 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: |
[Login]
IdleAction=poweroff
IdleActionSec=90min```
1. You may be able to set a CloudWatch alarm on CPU to trigger an instance stop. E.g. [https://successengineer.medium.com/how-to-automatically-turn-off-your-ec2-instance-in-2021-b73374e51090](https://successengineer.medium.com/how-to-automatically-turn-off-your-ec2-instance-in-2021-b73374e51090) (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
+1
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!