How to Automatically Turn Off your EC2 Instance in 2021

Shaun VanWeelden
4 min readFeb 28, 2021

Ever crash after a long night of coding only to wake up realizing you didn’t turn off your EC2 instances and now you owe AWS $10? This is surprisingly easy to avoid in 2021.

This guide will show you how to automatically turn off your instance after a period of inactivity.

1. Find your EC2 Instance and click “+” in the Alarm Status column

Our first step is to find the EC2 instance we want to automatically turn off.

Clicking the “+” icon in the Alarm Status column will open up the page to manage CloudWatch Alarms (the feature we’ll be using)

Navigate to the “Create Alarm” page

You should now be on Manage CloudWatch Alarms page.

2. Set the Alarm Action to Stop your Instance

When our EC2 instance becomes inactive, the behavior we want to happen is that our instance will be stopped. To specify the action to take, toggle the Alarm Action section to be on, then select Stop.

Specify the Alarm Action

3. Set the Alarm Thresholds as Needed

Like a lot of AWS in my opinion, this section is powerful, but not the most intuitive.

Below are the settings I recommend for an inactivity filter. Essentially, when the maximum CPU usage hasn’t been above 2% for an hour, turn off the instance. I also gave my alarm a more readable name.

Set up your Alarm Thresholds as needed

4. Click Create and get more Peace of Mind

It really is that easy. Whenever I create an EC2 instance, I’m now going to be doing these steps each time to ensure I don’t end up with a big bill at the end.

Click Create to save your new Alarm

5. Optional: Add an Email Alert when your instance shuts down

If you’d like to receive an email for extra peace of mind when your instance shuts down, there is an Alarm Notification section where it asks you to type in the name of a new SNS topic to create an email notification.

I actually couldn’t get this to create a new, usable SNS topic / notification. Feel free to comment about your own experience below.

If you do want to set this up, I found that after I created my alarm, I could go back to the EC2 instance overview and click on the Alarm Status cell for my EC2 instance, then go to the full edit mode.

Click on the text in the cell for Alarm Status
Open up the full edit page
Go to the Edit page for the Alarm

Once on the Edit page, we’ll want to scroll down and click Next to go to the Step 2: Configure Actions section.

Click Add Notification.

Click “Add Notification”

Lastly, we’re going to create a new topic by giving it a topic name and specifying your email address.

Click Create topic and click through the confirmation email you’ll receive.

You’re now all set to get email alerts the next time your instance shuts down.

Additional Reading

This is truly just the tip of the iceberg — For more information, check out the resources below:

AWS Guide on CloudWatch Alarm Creation for EC2 instances

AWS Overview on their Instance Scheduler — a way to automatically turn on and off your EC2 instance at set intervals

Have any other tips to more efficiently use your EC2 instances? Leave them in the comments below!

--

--

Shaun VanWeelden

Director of Computer Vision Engagements and Field Engineering at Scale AI | At the intersection between Customers and Code