Reducing your Azure bill with Azure Logic Apps

Nov 19, 2021 10:04 AM

Personal Blog
Microsoft
Azure
Logic Apps
Cost management

Unnecessary Azure costs

If you are using Azure for some time now and get used to most of the services within your solution, you will also notice that some services can become very expensive quite quickly. Of course for the most common services such as a Virtual Machine (VM), there is already a scheduler build in, allowing you to deallocate your VM after a specified time. After this, you only pay for your storage and the networking services, but not the computer resources from the VM itself. This can already save a lot of money, but this is just for VMs. What about other services?

Stopping services with Azure Logic Apps.

While VM's might have a scheduler built-in, which you can use to deallocate the VM, this is not always the case for all Azure services. In this example, we will look at both the Azure VM, which can also be deallocated via a Logic Apps, as well as an Azure Container Instance (ACI).

To make the Logic App possible, we need to start with a Recurrence Trigger. This trigger will allow us to run the Logic App at a specific time.

Select the Recurrence Trigger and set it to the required interval, time zone, hours and minutes. I went for 7 AM and 7 PM which should be reasonable time period for office hours.

Next, we need an Action-in this case a Condition, also known as an IF-statement.

What we want to accomplish here is to check if it is 7 PM. We can do this with the following code if you are from Western Europe. If you need to know the naming for your timezone check it here. NOTE: Sometimes it will accept an integer such as 19 (7 PM) in this case, but other times it needs to be a String, for this you can use string(19).

The following expression code was used: formatDateTime(convertFromUtc(utcNow(),'W. Europe Standard Time'),'HH')

With the creation of the the Condition you should also have gotten a True and False flow in which you can define new actions.

For the VM, look for the category Azure VM. You will see the option for the Deallocation.

As stated, this option is already built-in within the VM itself, but it is good to see you can also do it from inside an Azure Logic App.

Now, if you have an ACI running a docker or other kind of container, which can be stopped, search for the Azure Container Instance category and you will see the option to stop it.

Now you can do the same for the False option, but use the Start option for both, starting your VM and ACI when it is 7 AM.

This example is solely for the Azure Services that have a connector inside Azure Logic Apps, but what about services that don't?

Scaling Azure Services with Logic Apps

Not all services will save costs when they are stopped. This is for example true for the Azure Web App Service. To combat this, we can scale a service up and down, which will also reduce cost but still keeps the service running.

WARNING: If you scale down across different Tiers in services, loss of configuration and settings can occur. An example would be a Azure SQL Database that loses the option for In-Memory tables or the use of Columnstore Indexes. Another could be an Azure Web App that loses the option for certificates and deployment slots. All options that will not be available after scaling down will be deleted and will be required to be re-configured when scaling up.

Getting started

In the example, I'm using a Managed Service Identity (MSI). To start using this, it needs to get configured.

Go to the Identity Tab within the blade of the Logic App.

Stay in the System assigned tab and slide the Status option to ON. Don't forget to hit the Save button to finalize the change.

Continue by clicking on Azure role assignments and choose your scope (Subscription, resource group, etc.), followed by the proper role. I went for the contributor role which works for all services. After doing this, you are done for now.

HTTP Connector

Back in the Logic App Designer, add a HTTP action. This will require some configuration to get up and running. In this example, I will use the HTTP action to scale down an Azure SQL Database via the Azure Rest API.

The following settings were used:

Methode: PUT URL: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/{databaseName}?api-version=2021-02-01-preview NOTE: Change the SubscriptionId, ResourceGroupName, ServerName and DatabaseName to what you might be using. Headers: Content-Type & application/json Body:

{
  "location": "West Europe",
  "sku": {
    "name": "S0",
    "tier": "Standard"
  }
}

Authentication: (For this you need to click the Add parameter option first and select it) Managed Identity

Do the same again for the False statement in which you can change the Body with "name":"S1", or another SKU.

If you now trigger the Logic App manually, you will scale the database. I used an Azure SQL Database, but this can be done for most Azure Services. Check out the Azure REST API documentation here.

What's next?

Let's see what comes to mind next week, stay tuned!