Open In App

How to Create AWS Instance Scheduler ?

Last Updated : 27 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes the AWS EC2 instances are created unnecessarily, causing an unwanted bill where the resources were not used and we still have to pay for them. In such scenarios, an instance scheduler comes in handy, to avoid the hassle of redundant and extra instances and to help save money. In this article, we will learn how to create an AWS instance scheduler using the Command Line Interface.

What is an Instance Scheduler?

AWS's Instance Scheduler simplifies the process of managing Amazon EC2 and Amazon RDS instances. It automates the starting and stopping of these resources based on user-defined schedules, using tags and AWS Lambda functions. This helps reduce operational costs by only running instances when they're needed. The solution is flexible and can be deployed across multiple AWS regions.

Creating an Instance Scheduler Using AWS Management Console | Step-by-Step Guide

Step 1: Creating an instance scheduler using the command-line interface (CLI)

  • To use scheduler CLI, you must have credentials for the AWS Line Interface and the respective AWS identity and Access Management identity must have the permissions to run lambda:InvokeFunction and cloudformation:DescribeStackResource.
  • Create an IAM user (do not use your root account for the CLI interface), create the accesskeys Keys for the user and save the .csv file in your desktop. Configure AWS on your local machine using the command:
aws configure

aws configure

  • Enter the Access Key ID from the csv file downloaded
  • Enter the Secret Access Key from the csv file
  • Specify the region name(i used mumbai server (ap-south-1)
  • Leave the output format default.

region

  • Click on the link and Download and unzip the Scheduler CLI package.

Download and unzip

From the same directory in which you placed the CLI package, install the scheduler-cli to your environment:

pip install instance_scheduler_cli-1.5.3-py3-none-any.whl

Install python3 on your system (fedora systems)

sudo dnf install python3

Install python3 on your system (Ubuntu/Debian)

sudo apt-get install python3

Check if the Scheduler CLI command is functioning

scheduler-cli --version

scheduler-cli Step 2: Create an AWS CloudFormation Stack with the AWS Instance Schedule template

  • Open instance scheduler stack page.
  • Click on the Launch Solution button.
aws
  • Select the region for which you configured you CLI.

region

  • Select Next on the create stack page.

Screenshot-2024-04-18-203934

  • For stack name enter Ec2instanceScheduler.

enter Ec2instanceScheduler

  • For frequency choose a frequency in minutes to run on the scheduler.
  • For enable CloudWatch logs choose "Yes".
  • For started tags enter "state=started".
  • For stopped tags enter "state=stopped", keeping other parameters as default choose next.

Details

  • Choose Next on Configure stack options page.
  • Review your settings and mark "i acknowledge AWS CloudFormation might create IAM resources" under capabilities, choose Create Stack, and wait for the aws cloudformation stack creation.

Acknowledge

Step 3: Create a period to start Instances based on a Schedule

  • A period defines the duration when the instance should run.
  • The time zone can be specified during creation of schedule.
  • Open the CLI and run the following command.
scheduler-cli create-period --stack Ec2instanceScheduler --region ap-south-1 --name QuickPeriod --begintime 05:00 --endtime 05:05 --weekdays wed
  • Open the Amazon DynamoDB console.
  • Navigate to tables and choose configuration table.

DynamoDB console

  • Choose the items view and confirm that the QuickPeriod appears in the table.

Items

Step 4: Create a Schedule for the Period

  • Each schedule must contain at least one period that defines the time the instance should run.
  • A schedule can contain more than one period. (Create a schedule named "QuickSchedule" with time zone in UTC)
  • To create the schedule connect to the instance scheduler CLI and run the following command.
scheduler-cli create-schedule --stack Ec2instanceScheduler --name QuickSchedule --region ap-south-1 --periods QuickPeriod --timezone UTC
  • Open the DynamoDB console.

DynamoDB

  • Choose tables and choose configuration table.
  • Choose the item view and confirm that QuickSchedule appears at the configuration table.

Step 5: Tag the Schedule

  • Let's create the tag with key set to "Schedule" and the value set to "QuickSchedule" so that the "QuickSchedule" schedule is applied to the instance.
  • Open EC2 console
  • Choose the stopped instance that we want to tag
  • Go to tag menu and select manage tags
  • For tag key enter "Schedule", for value enter "QuickSchedule" and save.

Note: The solution is designed to automatically stop EC2 instances and assumes that instance shutdown behavior is set to Stop, not Terminate. When we use an AWS CloudFormation stack with the Instance Scheduler, we must define the Instance Scheduler Tag name parameter. The default value for this parameter is Schedule.

Conclusion

In this post, we learned about AWS Instance Scheduler, a handy tool for managing your virtual servers (EC2 instances) and databases (RDS instances) on Amazon Web Services. We explored how to set it up using the command line interface (CLI), taking each step one at a time.


Next Article
Article Tags :

Similar Reads