Automatically start and stop ec2 instance using bash Script
This tutorial explains how to automatically start and stop ec2 instance using Cronjob. This is one of the simplest method to schedule your EC2 Instance to stop and Start for cost saving.
Why Automatically start and stop ec2 instance ?
On demand EC2 Instance in AWS allow us pay as per use facility. So to avail this facility for cost saving purpose, a SysOps /DevOps Professional may be asked to schedule an instance to stop and start at a particular time.
Let say, a Test or UAT server is used between 10 AM to 6 PM , then the Project owner/ Business owner may ask you to make this instance available at this particular interval of time only.
How to Automatically start and stop ec2 instance ?
In this tutorial, we will use a method where you will need an another instance from where you can run bash script using Crontab.
Note : This tutorial works only if your both EC2 Instances are within the same VPC, same Region and same AWS account.
Step 1- Connect to another EC2 Instance
It can be any instance such as a Common Server /Script Server or your Production Server from there you need to run the script automatically.
Switch to root user
$ sudo su
Step 2 – Create two scripts files – Start and Stop scripts
Create two Script files , start and stop script at any location. Let say I am creating them in /root directory.
Use Vi or Nano editor to create or edit the files.
$ nano /root/ec2instance_start.sh
Add the following lines in the start script and save and exit using Ctrl+O and Ctrl+ x command.
aws ec2 start-instances -instance-ids i-f0c3678r3dba65876
$ nano /root/ec2instance_stop.sh
Add the following lines in the stop script and save and exit using Ctrl+O and Ctrl+ x command.
aws ec2 stop-instances -instance-ids i-f0c3678r3dba65876
Note : Here i-f0c3678r3dba65876 is the instance id of the Instance that you want to schedule for stop and start. You can see the instance id of an instance in description tab of that Instance in AWS Console.
Step 3 – Make the scripts executable
To make both the scripts executable run following commands.
$ chmod +x /root/ec2instance_start.sh
$ chmod +x /root/ec2instance_stop.sh
To Read more about CHMOD command read – CHOMD and CHOWN Commands
Step 4 – Schedule the Stop and Start using Cronjob.
Run following command to edit your Cron job.
$ crontab -e
Add the following line to start and stop the instance at 10 AM and 6 PM respectively.
00 10 * * * sh /root/ec2instance_start.sh > /tmp/ec2instance_start.log
00 18 * * * sh /root/ec2instance_stop.sh > /tmp/ec2instance_stop.log
To know more about Cronjob learn How to schedule Cronjob in Linux
Now your desired Instance is scheduled for Stop and Start at particular time.
Click to tweet this tip !
I hope you enjoyed this tutorial and learned about Automatically start and stop ec2 instance. If you think this is really helpful, please do share this article with others as well. Please also share your valuable feedback, comment, or any query in the comment box. I will really happy to resolve your all queries any.