How do I stop and start my instances using the AWS Instance Scheduler?

Last updated: 2019-03-13

I want to reduce my Amazon Elastic Compute Cloud (Amazon EC2) usage by stopping and starting my instances at predefined times. How can I do this?

Short Description

You can use the AWS Instance Scheduler and an automated AWS CloudFormation template to stop and start your instances based on a schedule. For more information, see Automated Deployment.

Important: If you use the AWS Instance Scheduler for EC2 instances that use an encrypted Amazon Elastic Block Store (Amazon EBS), your instances won’t start. To start your instances, you must grant the AWS Instance Scheduler a key user role with the key policy to encrypt or decrypt your Amazon EBS volumes. For more information, see Using IAM Policies with AWS KMS. You must also add the key policy on the AWS Key Management Service (Amazon KMS) key to allow the key user role to use this key. For more information, see Using Key Policies in AWS KMS.

Resolution

Before you start, set up the Instance Scheduler command line interface (CLI).

Create an AWS CloudFormation stack with the AWS Instance Scheduler template

The stack deploys an AWS Lambda function, an Amazon DynamoDB table, an Amazon CloudWatch event, and CloudWatch custom metrics.

1.    Sign in to the AWS Management Console.

2.    To open AWS CloudFormation with the AWS Instance Scheduler template, go to the Step 1. Launch the Instance Scheduler Stack page, and then choose Launch Solution.

3.    In the navigation bar, select the Region where you want to launch your stack with the template, and then choose Next.

4.    For Stack name, enter Ec2instanceScheduler.

5.    For Frequency, choose a frequency in minutes to run your scheduler (for example, 5 minutes).

Note: The frequency is the specified amount of time that you want Amazon CloudWatch Events to trigger the Lambda function for the AWS Instance Scheduler. If you have a large number of instances, use the highest frequency possible to avoid throttling. You can adjust the Frequency property later if the frequency is not fast enough for your needs.

6.    For Enable CloudWatch Logs, choose Yes.

7.    For Started tags, enter state=started.

8.    For Stopped tags, enter state=stopped.

9.    Choose Next.

10.    On the Options page, choose Next.

11.    Review your settings, and then select I acknowledge that AWS CloudFormation might create IAM resources.

12.    Choose Create.

Create a period to start instances based on a schedule

1.    To create a period, connect to the Instance Scheduler CLI, and then enter the following command:

$ scheduler-cli create-period --stack Ec2instanceScheduler --region us-west-2 --name firstdayofmonth --begintime 06:00 --endtime 07:00 --monthdays 1

Note: To create periods, you can also use the DynamoDB console or AWS CloudFormation custom resources. Replace us-west-2 with your own AWS Region.

2.    Open the DynamoDB console.

3.    Choose Tables, and then choose the configuration table.

Note: The AWS Instance Scheduler template automatically creates two DynamoDB tables: state and configuration. The state table stores the state of instances stopped and started by the template. The configuration table is where you specify the periods and schedules for your requirements.

4.    Choose the Items view, and then confirm that the firstdayofmonth period appears in the table.

Create a schedule for the period

1.    To create a schedule, connect to the Instance Scheduler CLI, and then enter the following command:

$ scheduler-cli create-schedule --stack Ec2instanceScheduler --name dayone --region us-west-2 --periods firstdayofmonth --timezone UTC

2.    Open the DynamoDB console.

3.    Choose Tables, and then choose the configuration table.

4.    Choose the Items view, and then confirm that the dayone schedule appears in the configuration table.

Tag and test the schedule

To track an instance for testing purposes, apply a custom tag to the instance.

Note: Stop the instance that you're testing so that you can test a predefined schedule (called running, for example).

1.    Open the Amazon EC2 console.

2.    Choose Running Instances, and then choose the instance that you want to tag.

3.    Choose the Tags view, and then choose Add/Edit Tags.

4.    Choose Create Tag.

5.    For Key, enter Schedule.

6.    For Value, enter running.

7.    Choose Save.

8.    Refresh the Amazon EC2 console, and then wait for the Lambda function to be triggered.

Note: When the Lambda function is triggered and runs without errors, the Instance State displays as running depending on the schedule you're testing. In the CloudWatch console, you can check Lambda metrics for invocations and errors.

9.    Open the DynamoDB console.

10.    Choose Tables, and then choose the state table.

11.    Choose the Items view, and then confirm that the tagged instance is started.

Note: You'll see that the state data gets stored in the state table.

You can be charged additional costs based on the frequency and duration of the Lambda function that you're using as well as any DynamoDB tables or AWS CloudWatch Events rules that you create.


Did this article help you?

Anything we could improve?


Need more help?