How do I use CloudFormation to stop and start my instances at predefined times?

Last updated: 2021-09-23

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 Instance Scheduler for EC2 instances with an encrypted Amazon Elastic Block Store (Amazon EBS), then your instances can't be started. To start your instances, you must grant the Instance Scheduler a key user role with a key policy to encrypt or decrypt EBS volumes. You must add the key policy to the AWS Key Management Service (AWS KMS) key to allow the key user role to use this key.

Resolution

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

Create a CloudFormation stack with the 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 CloudFormation with the 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 the N. Virginia Region 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 number of minutes that elapses before Amazon CloudWatch Events triggers the Lambda function again for the Instance Scheduler. If you have a large number of instances, use the highest frequency possible to avoid throttling. If the frequency isn't often enough for your needs, then you can adjust the Frequency property later.

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 the periods

The following example shows you how to create instances that:

  • Start at 9 AM and stop at 5 PM on Monday through Friday
  • Start at 9 AM and stop at 12 PM on Saturday

For this example, you must create two periods. For your own scenario, create the appropriate number of periods.

1.    Connect to the Instance Scheduler CLI, and then run the following commands:

$ scheduler-cli create-period --stack Ec2instanceScheduler --region us-west-2 --name mon-fri-9-5 --begintime 9:00 --endtime 16:59 --weekdays mon-fri
$ scheduler-cli create-period --stack Ec2instanceScheduler --region us-west-2 --name sat-9-12 --begintime 9:00 --endtime 11:59 --weekdays sat

Note: Replace us-west-2 with your own 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 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 mon-fri-9-5 and sat-9-12 periods appear in the configuration table.

Create a schedule

1.    To create a schedule that combines the two periods, connect to the Instance Scheduler CLI, and then run the following command:

$ scheduler-cli create-schedule --stack Ec2instanceScheduler --name m-f9-5-sat9-12 --region us-west-2 --periods mon-fri-9-5,sat-9-12 --timezone America/New_York

2.    Open the DynamoDB console.

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

4.    Choose the Items view, and then confirm that the m-f9-5-sat9-12 schedule appears in the configuration table.

Tag and test the schedule

When you use a 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.

The Instance Scheduler monitors tags on instances. If the instance tag key matches the defined scheduler tag, then the Instance Scheduler applies the schedule that's set for the instance tag value. For example, a tag's key is set to Schedule and the value is set to m-f9-5-sat9-12. In this example, the instances start at 9 AM and stop at 5 PM on Monday through Friday. The instances also start at 9 AM and stop at 12 PM on Saturday.

Note: Tag keys and values are case sensitive. The Instance Scheduler doesn't stop a running instance if it's manually started outside of the running period. The Instance Scheduler also doesn't start an instance if the instance stopped manually during the running period, unless the schedule is enforced. For more information, see Schedule definitions.

Using predefined schedules

In addition to custom schedules, you can also use any of the predefined schedules from the configuration table. For example, here's how you can test the predefined schedule named running:

1.    Open the Amazon EC2 console.

2.    Choose the stopped instances that you want to tag.

3.    Choose the Tags view, and then choose Manage Tags.

4.    Choose Add 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, then the Instance State displays as running, depending on the schedule that 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.

Important: You can be charged additional costs based on the frequency and duration of the Lambda function that you're using. You can also be charged additional costs for the DynamoDB tables or CloudWatch Events rules that you create.


Did this article help?


Do you need billing or technical support?