# Instance Scheduler

By
In 
aws
ec2
Published 2022-10-19


Setting up AWS Instance Scheduler to save EC2 instance costs for your dev/uat servers.

# 1. Use Case

When you have EC2 instances which are running when not needed (outside of work hours, on holidays, etc.) and billing you for it, Instance Scheduler will help you cut down on these costs by reducing the uptime of the server instance by shutting it down with a schedule but also conveniently bringing it back when needed (as scheduled).

# 2. Instructions

  • Setting up the Instance Scheduler in CloudFormation:

    1. Head over to the AWS Instance Scheduler Page and click on the Launch in the AWS Console option.

      • On that page, you could change any options you want, but we'll keep the defaults and click Next.

    2. In Specify Stack Details page, give your CloudFormation Stack a name like dev-server-scheduler, and your Instance Scheduler a tag name like dev-scheduler. Set the default time zone to your time zone, this would help you avoid converting times later.

      • Frequency depends on what you would schedule your timtings as. If you're doing something like 08:00 to 18:00, then I think a 60 minute frequency is good enough.
      • You could enable CloudWatch logs and metrics if you need. Do add Started tags and Stopped tags, this would help you differentiate between Manual starts of the instance vs Instance Scheduler's.

    3. In Configure stack options page, I decided to keep everything default, you could add a role, if you already have or need something specific, for CloudFormation to use, but it'll create it's own role if you keep it empty.

    4. In the next page, just verify details and acknowledge the role creation for CloudFormation and it'll start creation of your CloudFormation Stack.

    5. Wait till the status turns to CREATE_COMPLETE like in the image.


  • DynamoDB Setup:

    1. Head over to DynamoDB and click on the Tables option in the left sidebar.
    2. In the list, find the table which as ConfigTable in the name. Open that table and click on the Explore table items button.
    3. In the table, you'll find multiple period and schedules. We'll use the seattle-office-hours schedule and office-hours period for our purpose.
    4. Change the options inside the office-hours period as per the image, you can edit the timings and weekdays as you need.
    5. Change the options inside the seattle-office-hours schedule as per the image, while changing the name, description, and timezone as you need.

  • EC2 Tags:

    1. Once this is done, head over to EC2 and inside your instance, go to Actions > Instance Settings > Manage Tags.
    2. You'll need to add a couple of tags here as per the image, but ensure to change the names as per your setup from previous steps. Note: The tag key should be the same name as the CloudFormation Stack for the instance scheduler. The tag key for my setup is dev-scheduler
    3. Save the tags.
  • Check if it is working:

    1. Just check the instance state outside work hours to ensure it is stopped and during the next day, your instance starts up automatically.
    2. As the instance is stopping, some of your services might not boot up automatically upon restart of the instance, add them to your startup script. This would depend for each of the service you want to start. Some services might give you a startup script/command, so do check them out.

Enjoy saving $$$, cheers!