#
Instance Scheduler
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
Disclaimer
This guide is correct and up to date only as of the publishing date. AWS have the rights to change features regarding this or change the pricing on their platform. I am not responsible for their actions, use the guide mainly as a reference but check their website and documentation for the updated details.
Special Thanks
THanks to DeshDeepakDhobi (DD) for his guide over on Medium which I used as a base to this guide. Some of the details seem a bit confusing and incomplete on his guide as well as the screenshots are a bit outdated, thus this new guide.
Setting up the Instance Scheduler in CloudFormation:
Head over to the AWS Instance Scheduler Page and click on the
Launch in the AWS Console
option.Region Check
Ensure you're on the correct region or you will end up creating the job in the wrong region.
- On that page, you could change any options you want, but we'll keep the defaults and click Next.
- On that page, you could change any options you want, but we'll keep the defaults and click Next.
In
Specify Stack Details
page, give your CloudFormation Stack a name likedev-server-scheduler
, and your Instance Scheduler a tag name likedev-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
andStopped tags
, this would help you differentiate between Manual starts of the instance vs Instance Scheduler's.
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.In the next page, just verify details and acknowledge the role creation for CloudFormation and it'll start creation of your CloudFormation Stack.
Wait till the status turns to
CREATE_COMPLETE
like in the image.
DynamoDB Setup:
- Head over to DynamoDB and click on the
Tables
option in the left sidebar. - In the list, find the table which as
ConfigTable
in the name. Open that table and click on theExplore table items
button. - In the table, you'll find multiple
period
andschedules
. We'll use theseattle-office-hours
schedule andoffice-hours
period for our purpose. - Change the options inside the
office-hours
period as per the image, you can edit the timings and weekdays as you need. - Change the options inside the
seattle-office-hours
schedule as per the image, while changing the name, description, and timezone as you need.
- Head over to DynamoDB and click on the
EC2 Tags:
- Once this is done, head over to EC2 and inside your instance, go to
Actions > Instance Settings > Manage Tags
. - 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
- Save the tags.
- Once this is done, head over to EC2 and inside your instance, go to
Check if it is working:
- Just check the instance state outside work hours to ensure it is stopped and during the next day, your instance starts up automatically.
- 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!