Using AWS cloudwatch to shut down unused EC2 Instances

Hi there,

Quick tip, you can use AWS CloudWatch alarms to detect and shut down unused EC2 instances automatically. I’ve done that to my instances.

Also, that should help us to save a few $$$  .

Overview – Creating Amazon CloudWatch Alarms

You can create a CloudWatch alarm that watches a single metric. The alarm performs one or more actions based on the value of the metric relative to a threshold over a number of time periods. The action can be an Amazon EC2 action, an Auto Scaling action, or a notification sent to an Amazon SNS topic.

Alarms invoke actions for sustained state changes only. CloudWatch alarms do not invoke actions simply because they are in a particular state, the state must have changed and been maintained for a specified number of periods.

After an alarm invokes an action due to a change in state, its subsequent behaviour depends on the type of action that you have associated with the alarm. For Amazon EC2 and Auto Scaling actions, the alarm continues to invoke the action for every period that the alarm remains in the new state. For Amazon SNS notifications, no additional actions are invoked.

You can also add alarms to dashboards. When an alarm is on a dashboard, it turns red when it is in the ALARM state, making it easier for you to monitor its status proactively.

Create Alarms That Stop, Terminate, Reboot, or Recover an Instance

Using Amazon CloudWatch alarm actions, you can create alarms that automatically stop, terminate, reboot, or recover your instances. You can use the stop or terminate actions to help you save money when you no longer need an instance to be running. You can use the reboot and recover actions to automatically reboot those instances or recover them onto new hardware if a system impairment occurs.

There are a number of scenarios in which you might want to automatically stop or terminate your instance. For example, you might have instances dedicated to batch payroll processing jobs or scientific computing tasks that run for a period of time and then complete their work. Rather than letting those instances sit idle (and accrue charges), you can stop or terminate them, which can help you to save money. The main difference between using the stop and the terminate alarm actions is that you can easily restart a stopped instance if you need to run it again later, and you can keep the same instance ID and root volume. However, you cannot restart a terminated instance. Instead, you must launch a new instance.

You can add the stop, terminate, reboot, or recover actions to any alarm that is set on an Amazon EC2 per-instance metric, including basic and detailed monitoring metrics provided by Amazon CloudWatch (in the AWS/EC2 namespace), as well as any custom metrics that include the InstanceId dimension, as long as its value refers to a valid running Amazon EC2 instance.

Adding Stop Actions to Amazon CloudWatch Alarms

You can create an alarm that stops an Amazon EC2 instance when a certain threshold has been met. For example, you may run development or test instances and occasionally forget to shut them off. You can create an alarm that is triggered when the average CPU utilization percentage has been lower than 10 percent for 24 hours, signalling that it is idle and no longer in use. You can adjust the threshold, duration, and period to suit your needs, plus you can add an Amazon Simple Notification Service (Amazon SNS) notification so that you receive an email when the alarm is triggered.

To create an alarm to stop an idle instance using the Amazon EC2 console

  1. Open the Amazon EC2 console at
  2. In the navigation pane, choose Instances.
  3. Select the instance. On the Monitoring tab, choose to Create Alarm.
  4. In the Create Alarm dialogue box, do the following:
    1. To receive an email when the alarm is triggered, for Send a notification to, choose an existing Amazon SNS topic, or choose to create topic to create a new one.To create a new topic, for Send a notification to, type a name for the topic, and then for With these recipients, type the email addresses of the recipients (separated by commas). After you create the alarm, you will receive a subscription confirmation email that you must accept before you can get notifications for this topic.
    2. Choose to Take the actionStop this instance.
    3. If prompted, choose to Create IAM role: EC2ActionsAccess to automatically create an IAM role so that AWS can automatically stop the instance on your behalf when the alarm is triggered.
    4. For Whenever, choose the statistic you want to use and then choose the metric. In this example, choose Average and CPU Utilization.
    5. For Is, specify the metric threshold. In this example, type 10 percent.
    6. For at least, specify the evaluation period for the alarm. In this example, type 24 consecutive period(s) of 1 Hour.
    7. To change the name of the alarm, for Name of alarm, type a new name. Alarm names must contain only ASCII characters.
    8. Choose to Create Alarm.

All set and ready to go  😎 

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at

Up ↑

%d bloggers like this: