How to Automatically Turn Off your EC2 Instance in 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.

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.

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

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

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

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.

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!

Field Engineering Manager at Scale AI | At the intersection between Customers and Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store