In recent times, there is a clear tendency for companies to migrate their resources to “cloud computing”.
The fundamental reason is that this new model offers companies access to more scalable and versatile IT services. This allows customers to choose specific levels of service according to their needs, instead of having to invest in the purchase of hardware for work demands that, in many cases, can be punctual or cyclical.
However, due to the way suppliers invoice cloud, companies can incur high costs, but they are careful in the way they use these services.
One of the most common ways to reduce the cost of cloud services is to turn off environments when they are not being used. Generally, this usually happens with development and preproduction environments that are usually used only during working hours.
The most reasonable thing is to automate the switching on and off from these environments according to the schedule defined by the company.
This task can be done in several ways. In this case we will explain how you could automate the sequential shutdown and startup of virtual machines in Azure, using Azure automation.
One of the simplest ways to use this service would be a solution offered by Azure from the market place.
- Once logged in the Azure portal and being in the chosen subscription, we created a group of resources, in this case rg_automation
- Once inside the group of resources, click on add:
- We look for the solution: Start / Stop VMs during off hours and click on create:
This solution directly displays an automation and log analytics account to visualize the correct execution of the start and stop jobs of machines.
- Next, we display the option to create a new OMS workspace, which is the service that allows us to use log analytics. In this case, it allows us to create a new one or link the solution to an existing one. In our case we are going to create a new workspace and fill in the requested fields that are:
- Name of the new workspace (it must be unique)
- Resource group where we want the workspace to be displayed
- Location where we are going to display the workspace
- And the price plan
- Secondly, we display the automation account option, where the automation accounts that we have in the subscription will appear. In this case, we will create a new one, so we click on create automation account and fill in the requested data:
- Name of the automation account
- Resource group where we want it to be deployed
- Location where we want it to be displayed
- Click on create a Run As account. In this way, the process creates a main service with the role of a collaborator of the subscription, whose function will be to provide authentication to the jobs executed from the automation account so that they can administer Azure resources.
- Third, we display the “Parameters” option in which we can select:
- The resource group in which the machines we want to automate their stop and start are located.
- List of existing machines in that group of resources, which we do not want to include in automation
- Daily programming of automatic start and stop of the machines
- If we want to receive a notification email at the end of the stop and start jobs.
- List of email addresses where the notification emails will be sent.
This time we leave the parameters as they are and click on create, to be able to configure the automation later.
Once the solution is deployed, we go to the resource group and click on the automation account:
This solution provides us with several options:
- Shut down the machines if they exceed a% of CPU usage
- Start and shut down the machines according to a schedule
- Start and shut down the machines according to a schedule and in a certain order.
In our case we are going to apply this last solution, which is useful when we have a two-layer environment, frontend with web servers or applications and backend with one or several databases. In these cases it is advisable to turn off the servers in the order frontend-> backend and turn them in reverse order.
The first thing we must do is go to the variables and configure the group of resources where the machines are located and if there is a machine that we do not want to include in the automation of power off and on. In this case:
Next, we go to the programming and deactivate those that are activated by default:
We activate sequential start programming, in this case we want the machines to start from Monday to Friday at 8:00:
We activate the programming of sequential stop, so that they stop from Monday to Friday at 3:00 p.m:
Once this is done, we only need to configure the order to stop and start the machines. For this we use the following labels:
The value of the labels must be between (1,2,3) that corresponds to the order in which we want the machines to start or stop. For both actions, the order is ascending.
Thus, for a frontend machine, the labels would be:
In this way, it would start second and shut down before the backend.
In the case of a backend machine, it would be fair, on the contrary:
In conclusion, we could say that through this solution offered by Azure in the market place, we can automate the switching off and on of machines easily in less than 15 minutes, allowing us to save costs easily and quickly.