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

Last updated: 2020-11-12

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

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 Amazon 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 a key policy to encrypt or decrypt your Amazon EBS volumes. You must also add the key policy to the AWS Key Management Service (AWS 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 Events event, and Amazon CloudWatch custom metrics.

1.    Open the AWS Management Console.

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

Note: The template is launched in N. Virginia by default.

3.    In the navigation bar, select the AWS 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 isn't 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 run the following command:

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

Note: Replace us-west-2 with your own AWS Region. To create periods, you can also use the DynamoDB console or custom resources. For more information on time periods, see Start and Stop Times.

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 that are 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 run 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

When you use an AWS CloudFormation stack with the Instance Scheduler, you must define the Instance Scheduler Tagname parameter. The default value for this parameter is Schedule. You can use the TagName parameter in your stack to find the value of Schedule.

Note: The Instance Scheduler monitors tags on instances. If the key on the instance tag matches the defined scheduler tag, then the Instance Scheduler applies the schedule that's set for the value of the instance tag. For example, if a tag has the key set to Schedule and the value set to firstdayofmonth, then the firstdayofmonth schedule is applied to the instance.

In following steps, you test a predefined schedule that's running on a stopped instance. You can find a predefined running schedule in the configuration table. The Instance Scheduler starts the instance when you apply a running schedule on stopped instance.

Note: Stop the instance that you're testing so that you can test a predefined running schedule.

1.    Open the Amazon EC2 console.

2.    Choose the stopped instances 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: The state data is 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, or based on DynamoDB tables or CloudWatch Events rules that you create.


Did this article help?


Do you need billing or technical support?