UPDATE: if you are looking for the easy, custom instance scheduler based on Lambda function, take a look at my other post: Simple AWS EC2 instance scheduler
We’ve got an office server which does not have to be up and running 24/7. It will be good enough if it will start each day at 6 a.m. and will be turned off at 10 p.m. This gives us 2/3 of the day up and running and the remaining 8 hours with no payment for the EC2 instance. You can build scripts to do this on your own – using CLI AWS tools, but you can also use ready-made Instance Scheduler.
Overview – what will be involved
There AWS Instance Scheduler is built on top of various AWS services. CloudWatch is used to trigger Lambda function, Lambda is using DynamoDB as configuration storage, instances are turned on and off by the Lambda function and logs are stored in CloudWatch. There is one additional item – CloudFormation stack which is built from the ready-made Instance Scheduler template. This is why you don’t have to build all scripts on your own – configuration will be the matter of few clicks and adjustments.
Create Instance Scheduler CloudFormation Stack
First, you will have to sign in to your AWS Console. In order to create a new CloudFormation stack from the Scheduler template, you simply need to follow this link. Please note that the region is already selected in this link – if you want to use different region, you can simply switch it in the console navigation bar once the page will be loaded. As you can see, the proper template is already selected so you can simply click Next:
Now you will have to specify details and parameters of your stack. Stack name is simply the name for your stack – I called it OfficeServer-InstanceScheduler. Instance Scheduler tag name is an important one – this tag should be defined for all instances you want to turn on and off automatically. I named it ‘OfficeSchedule’.
Service(s) to schedule – here you can select what types of services should your Scheduler take care of. You can select EC2, RDS or Both. Scheduling enabled – you can decide if the schedule is active or not. Region(s) – if you want to use it only in the current region – leave blank. Default time zone – in our case we wanted to use US/Eastern time zone to make sure that we will not confuse working hours.
Cross-account roles – leave blank if you are not using Scheduler across multiple accounts. This account – I left “Yes” because I will schedule instances only on this account. Frequency – if you are using only full hours to start and stop your instances, you can set to 60 minutes, otherwise, you should select the frequency which fits your periods the best. Memory size – this is memory allocated for the Lambda function, 128 is good enough for small sets of instances.
Options can be left unchanged if you don’t want to have detailed CloudWatch logs or metrics. Other parameters can also be left unchanged. I used Started tags and Stopped tags because I want to track Scheduler actions on our own for some time. Once you will be ready with all of above, click Next.
CloudFormation stack options and review
On the next page, you will find Options – Tags for your stack, permissions, triggers and advanced additional options. You can leave all of this empty if you don’t have special needs. Once you will click Next you will be presented with Review page – you should review your settings. As the last step with CloudFormation, you will have to confirm that acknowledgment at the bottom of the page.
Once you will click “Create” button, you will be done with the most complicated part of the work. Now you will have to wait for the stack to be created. You can switch to the CloudFormation management console and wait till the stack will have the status of CREATE_COMPLETE.
Adjust periods and schedules in DynamoDB
You can switch to the DynamoDB management console. In the Tables, you will find two new items – Config Table and State Table for your Scheduler Lambda function. The State Table should not be changed manually, we will take care of the Config Table. There are three types of entries in this table. Config is handling the main configuration of the Scheduler and most likely you will not need to adjust this. We will take care of the schedule and period type of entries. Initially, there are few example entries created.
Period entries are storing the information about start and end hours. Also, weekdays for given period are stored in the entry. This means that we will need to use only one period record – configured from 6 a.m. to 10 p.m. on weekdays only. Schedule records are defining what time zone given we are working in and what periods are used for the particular schedule. I will use only one period so the configuration will be rather simple.
Because you can not change the names of the records in this table, you will have to copy them and adjust for your needs. The easiest way is to select an item you want to copy and click Actions -> Duplicate. You can now change hours and days to fit your needs. You should choose proper name for your period – it will be used in the schedule. I named it ‘canada-office-hours‘.
The same you can do with schedule record – duplicate and adjust as per your needs. Please note that you should place proper period name in periods list – in my case ‘canada-office-hours‘. You should also set proper name for your schedule record – it will be used in tags of the instances you want to automate. I also named it ‘canada-office-hours‘.
Now it is time to let the Scheduler know which instances should be turned on and off. Remember, in the CloudFormation configuration I used ‘OfficeSchedule‘ in the Instance Scheduler tag configuration. This is the tag we will use for our instances. Let’s switch to the EC2 management console, select the proper instance and click Add/Edit Tags in the Tags tab. I simply added the tag named ‘OfficeSchedule‘ with the value of ‘canada-office-hours‘ – the name of the schedule record from the DynamoDB. That’s all. Lambda function is executed every 60 minutes (or every nn minutes as configured) and is matching EC2 instance tag with the records in the DynamoDB. The instance is started and stopped on selected hours automatically.
You should be aware that you will be charged for the DynamoDB usage, Lambda usage, and CloudWatch metrics if you decided to use them. The charges are not big but you are warned. Savings from the hours when the instance is not working should be much bigger.
The second thing you should bear in mind – you can still turn your instance on and off manually. If you will turn your instance on before the scheduled start time, it will stay turned on, the scheduler will do nothing. Also if you will turn your instance off before the scheduled time, it will remain off. On the other hand, if your instance is turned on and you are working on it, the scheduler will turn it off at the scheduled time no matter what – it is not monitoring instance usage. I warned you 🙂