Automating Elasticity
AWS Whitepaper

Automating Time-Based Elasticity

Most non-production instances can and should be stopped when they are not being used. Although it is possible to manually shut down unused instances, this is impractical at larger scales. Let’s consider a few ways to automate time-based elasticity.

AWS Instance Scheduler

The AWS Instance Scheduler is a simple solution that allows you to create automatic start and stop schedules for your EC2 instances. The solution is deployed using an AWS CloudFormation template, which launches and configures the components necessary to automatically start and stop EC2 instances in all AWS Regions of your account.

During initial deployment, you simply define the AWS Instance Scheduler default start and stop parameters and the interval you want it to run. These values are stored in Amazon DynamoDB and can be overridden or modified as necessary. A custom resource tag identifies instances that should receive AWS Instance Scheduler actions. The solution's recurring AWS Lambda function automatically starts and stops appropriately tagged EC2 instances. You can review the solution's custom Amazon CloudWatch metric to see a history of AWS Instance Scheduler actions.

Amazon EC2 API tools

You can terminate instances programmatically using Amazon EC2 APIs, specifically the StopInstances and TerminateInstances actions. These APIs let you build your own schedules and automation tools. When you stop an instance, the root device and any other devices attached to the instance persist. When you terminate an instance, the root device and any other devices attached during the instance launch are automatically deleted. For more information about the differences between rebooting, stopping, and terminating instances, see Instance Lifecycle in the Amazon EC2 User Guide.

AWS Lambda

AWS Lambda serverless functions are another tool that you can use to shut down instances when they are not being used. You can configure a Lambda function to start and stop instances when triggered by Amazon CloudWatch Events, such as a specific time or utilization threshold. For more information, read this Knowledge Center topic.

AWS Data Pipeline

AWS Data Pipeline is a web service that helps you reliably process and move data between different AWS compute and storage services, as well as on-premises data sources, at specified intervals. It can be used to stop and start Amazon EC2 instances by running AWS Command Line Interface (CLI) file commands on a set schedule. AWS Data Pipeline runs as an AWS Identity and Access Management (IAM) role, which eliminates key management requirements. For instructions on using this approach, watch this tutorial.

Amazon CloudWatch

Amazon CloudWatch is a monitoring service for AWS cloud resources and the applications you run on AWS. You can use Amazon CloudWatch to collect and track metrics and log files, set alarms, and automatically react to changes in your AWS resources. You can use Amazon CloudWatch alarms to automatically stop or terminate EC2 instances that have gone unused or underutilized for too long. You can stop your instance if it has an Amazon Elastic Block Store (Amazon EBS) volume as its root device. A stopped instance retains its instance ID and can be restarted. A terminated instance is deleted. For more information on the difference between stopping and terminating instances, see the Stop and Start Your Instance in the Amazon EC2 User Guide.

For example, you can create a group of alarms that first sends an email notification to developers whose instance has been underutilized for 8 hours, and then terminates that instance if its utilization has not improved after 24 hours. For instructions on using this method, see the Amazon CloudWatch User Guide.