AWS doesn’t charge users for a stopped instance. When a stopped instance is started, you are automatically charged for a minute’s worth of usage. After one minute, AWS charges you by the second. So if you run an instance for 30 or 40 seconds, you are still charged for one minute but if you run it for 2 minutes and 15 seconds, you are charged for that exact time.
You can get a lot of benefits from deciding to automatically stop and start your EC2 instances.
You can make changes to the volume of the instance when it is in a stopped state. You have the potential to treat the root volume of your instance the same as any other volume. You can detach it and add it to another machine, modify it, etc.
The difference between a stopped instance and a terminated instance is that once you stop it, you can still be charged for storage but if you terminate it, you are freed of all charges.
You can modify the following attributes of an instance only when it is stopped:
Modifying these attributes while the instance is running gives the IncorrectInstanceState error.
What happens when you stop an Instance?
The Instance Scheduler will give you the option to shut down, start or restart the specifically tagged instances according to the time you deem appropriate.
To set up the Instance Scheduler, we will first install the stack by provisioning all the resources onto AWS CloudFormation.
Leave all the other options as it is and proceed.
Wait for the resources to be provisioned. You will see a success message when it is done.
With this process, you will have three resources set up.
Now we’ll define the shutdown period. You can make the necessary configurations on DynamoDB.
This defines the period to match the time we need the instances to shutdown.
This defines the schedule which will be used to match the instances according to tags. The tag used here is “ca-autostop”. The Schedule also contains information about the timezones so instances from other regions operate under the same scheduling hours.
Now, any instance with the tag you have defined will be manipulated by the Instance Scheduler.
You can schedule your Instances through a PowerShell class as well. You will need to use the AWS Tools for Windows Powershell.
You can install the AWS Power Shell module from the PowerShell Gallery.
Create a class named AWSInstance. This class will allow you to start and stop your machines.
Declare the class
Next, we define all the properties we need to manipulate the instance.
The hidden property is used to identify the right instance. The value that is passed to this class finds the Instance Tag or the Instance ID.
The AWSinstance constructor takes either the instanceid or the tagname and compares it with the machines using regexp in an if condition.
Each static method creates an instance of the class:
Once the static method has created an instance of the machine, it can call methods to change the running state.
You can also get the status of a machine using this command:
By changing the status method as shown below, you can start, stop or restart the instances.
This is an open-source tool run on Terraform. You can find it here.
To run this you need to install kitchen-terraform and awspec.
gem install bundler
This will install the dependencies.
Change the values according to your choice to initiate the schedule.
The Totalcloud tool allows you to perform these operations in an intuitive and user-friendly platform. For this, you will need to log in to the platform here.
Utilise the right start and stop method with whatever tools you deem most suitable. Once you are confident in your scheduling method, the next step is to make sure you are following the best scheduling practices to spend the least amount of money on your resources. Hope you can cut down your expenses and manage your instances with the techniques provided in this guide.