Microsoft Fabric: Capacity Cost Management Part 2, Automate Pause/Resume Capacity with Azure Logic Apps

Automate Pause Resume Suspend Fabric Capacity with Azure Logic Apps

In the previous blog post, I explained Microsoft Fabric capacities, shedding light on diverse capacity options and how they influence data projects. We delved into Capacity Units (CUs), pricing nuances, and practical cost control methods, including manually scaling and pausing Fabric capacity. Now, we’re taking the next step in our Microsoft Fabric journey by exploring the possibility of automating the pause and resume process. In this blog post, we’ll unlock the secrets to seamlessly managing your Fabric Capacity with automation that helps us save time and resources while optimising the usage of data and analytics workloads.

Right off the bat, this is a rather long blog, so I added a bonus section at the end for those who are reading from the beginning to the end. With that, let’s dive in!

The Problem

As we have learned in the previous blog post, one way to manage our Fabric capacity costs is to pause the capacity while not in use and resume it again when needed. While this can help with cost management, as it is a manual process, it is prone to human error, which makes it impractical in the long run.

The Solution

A more practical solution is to automate a daily process to pause and resume our Fabric capacity automatically. This can be done by running Azure Management APIs. Depending on our expertise, there are several ways to achieve the goal, such as running APIs on running the APIs via PowerShell (scheduling the runs separately), running the APIs via CloudShell, creating a flow in Power Automate, or creating the workflow in Azure Logic Apps. I prefer the latter, so it is the method that this blog post explains.

Automating Pause and Resume Fabric Capacity with Azure Logic Apps

Here is the scenario: we are going to create an Azure Logic Apps workflow that automatically does the following:

  • Check the time of the day
  • If it is between 8 am to 4 pm:
  • Check the status of the Fabric capacity
  • If the capacity is paused, then resume it, otherwise do nothing
  • If it is after 4 pm and before 8 am:
  • Check the status of the Fabric capacity
  • If the capacity is resumed, then pause it, otherwise do nothing

Follow these steps to implement the scenario in Azure Logic Apps:

  1. Login to Azure Portal and search for “Logic App
  2. Click the Logic App service
Finding Logic Apps on Azure Portal

This navigates us to the Logic App service. If you currently have existing Logic Apps workflows, they will appear here.

  1. Click the Add button
Creating a new Logic App workflow on Azure Portal
Creating a new Logic App workflow on Azure Portal
  1. On the Basics tab, select the desired Azure Subscription
  2. Select a desired Resource Group. If you do not have any resource groups, click Create new to create one
  3. Type in a name in the **Logic App name ** textbox
  4. Select the desired Region from the dropdown
  5. Select Consumption on the Plan type
  6. Keep the Zone redundancy Disabled
  7. If you want/need to add Tags, click the Next: Tags button, otherwise click the Preview + create button
Creating a new Logic App workflow on Azure Portal
  1. Click the Create button
Review configuration and create a new Logic App workflow on Azure Portal

This will create a new Logic App workflow. After the service is created a new Go to resources button.

  1. Click the Go to resources button
Go to the newly created Logic App workflow on Azure Portal
  1. Click the Logic app designer from the Development Tools section on the left pane or click the Edit button to open the Logic app designer
Opening Logic App designer in Azure Portal
  1. Click the Recurrence from the Start with a common trigger section. This opens the Logic app designer and adds the Recurrence trigger
Adding a Recurrence trigger to Logic Apps
  1. Change the Interval to 1
  2. Change the Frequency to Hour

So far, we defined a trigger to run the workflow every hour. The next step is to get the current time.

  1. Click the New step button
Configuring the Recurrence trigger to run every hour
  1. Search and select the Current time action

The Current time action gets the current time in UTC.

Using Current Time action to Logic Apps

If you live in a different timezone, you need to convert the output from UTC to your timezone.

  1. Click the New step button, search and select Convert time zone action
  2. Click the Base time textbox then select the Current time from the Dynamic content
  3. Select (UTC) Coordinated Universal Time from the Source time zone dropdown
  4. Select the Destination time zone from the dropdown
  5. Type in HH:mm in the Format string text box and click Use ‘HH:mm’ as custom value
Converting the output of the Current time action to local timezone

So far, we got the current time and converted it into our local timezone. Next, we need to get the status of the Fabric capacity. This can be done via the Azure Resource Manager operation by using its Read a resource action.

  1. Click the New step button, search and select Read a resource action (which is an Azure Resource Manager action)
Using Read a resource action of Azure Resource Manager
  1. Pass your credentials on the Azure Resource Manager operation then select the Subscription of your Fabric capacity from the dropdown
  2. Select the Resource Group containing your Fabric capacity
  3. Select Microsoft.Fabric from the Resource Provider dropdown
  4. Type in capacities/YOUR_FABRIC_CAPACITY_NAME on the Short Resource Id text box where the YOUR_FABRIC_CAPACITY_NAME is the name of your Fabric capacity which in my case is dv01fabric
  5. Type in 2023-11-01 on the Client Api Version text box
Setting Read a resource action of Azure Resource Manager

So far, we have the current time for our timezone and the current status of our Fabric capacity. Next, we need to check the current time to see if it’s between 8 am and 4 pm, then resume the capacity if its status is Paused and do nothing if the capacity is already running. If it is after 4 pm and before 8 am, pause the capacity if its status is Resumed, and do nothing if it is already paused.

Let us continue…

  1. Click the New step button, search for Condition action and select it under the Control operation
  2. Click the first condition’s textbox to select the Converted time from the Dynamic content
Condition action of Control operation
  1. Change the condition to is greater than or equal to
  2. Type 08:00 for the value
  3. Click the Add button
  4. Select the Add row option
  5. Select the Converted time from the Dynamic content again
  6. Select is less than or equal to condition
  7. Type 16:00 for the value
Setting Condition action to get the output from a Convert time zone action

OK! Let’s take a moment to understand where we are at now. By adding the Condition action, we are checking if the current time is between 8 am and 4 pm.

Next, we need to check the status of the Fabric capacity. If we run the workflow now (click the Run Trigger button, then click Run) and look at the outputs of the Read a resource action containing the state of the capacity under the Properties. That is exactly what we are after. To the value of the state, we need to use the following expression:

body('Read_a_resource')?['properties']?['state']

With that, let’s continue.

  1. Click the Add an action button on the True output to add a new Condition action which by default is named Condition 2
  2. Within Condition 2 click the Choose a value setting
  3. Copy and paste the above expression into the Expression box
  4. Click OK
Using an expression in Condition action to read the State part of the Properties within the body of the Read a resource output
  1. Set the condition to is equal to
  2. Type Paused in the Value textbox
Checking if the State of the Read ā resource action is Pause
  1. Click the Add an action button on the True output of Condition 2 and add the Invoke resource operation
Using Invoke resource operation action from Azure Resource Manager operation
  1. On the Invoke resource operation action, select the desired Subscription
  2. Select the desired Resource Group
  3. On the Resource Provider select Microsoft.Fabric
  4. Type in capacities/YOUR_FABRIC_CAPACITY_NAME on the Short Resource Id text box where the YOUR_FABRIC_CAPACITY_NAME is the name of your Fabric capacity, which in my case is dv01fabric
  5. Type in 2023-11-01 on the Client Api Version textbox
  6. Type resume on the Action name textbox
Configuring Invoke resource operation action of Azure Resource Manager operation

So far, we have checked the current time of the day; if it is between 8 am and 4 pm, we check the status of our Fabric capacity. If it is Paused, we resume the capacity. The next step is to add a similar condition to what we’ve done, but this time for the False output of the first Condition action. We just need to repeat steps 39 to 51 with a minor change in step 51 where we type suspend in the Action name textbox.

The following image shows the entire solution. Just so you know, I renamed the actions with more proper names.

Azure Logic Apps workflow to pause and resume Fabric capacity

Here you go! You have it.

Since you have come thus far, the following bonus section would add more value. So continue reading…

Bonus Section

You can improve the solution to fit your requirements. For example, you can check if the current date is during the weekend, then do not run the workflow.

To do so, follow these steps:

  1. Click the ellipsis button of the Recurrence trigger
  2. Click Settings
  3. Click the Add button under the Trigger Conditions section
  4. Enter the @contains('12345', string(dayOfWeek(convertFromUtc(utcNow(), 'New Zealand Standard Time')))) expression
  5. Click the Done button
Conditional Recurrence Trigger

The above process forces the trigger to run the workflow only during the weekdays (not on the weekends).

The expression in point 4 gets the curent date and time in UTC, converts it into New Zealand timezone, gets the week day number of the week, and converts it to string. At last, it checks if the day number of the week is in week days. Sunday is 0, Monday is 1 etc…

If you live in a different timezone, you can find Microsoft’s default timezones here.

Conclusion

In this exploration of Microsoft Fabric capacities and Azure Logic Apps automation, we’ve advanced from manual pausing and resuming to an automated solution. By creating a Logic Apps workflow, we’ve demonstrated the practicality of managing Fabric capacity based on a set schedule, optimising resource utilisation effortlessly. In the bonus section, we looked at the solution’s adaptability to specific needs, like excluding weekends.

I hope you enjoyed reading this long blog and find it helpful.
As always, feel free to leave your comments and ask questions, follow me on LinkedIn and @_SoheilBakhshi on X (formerly Twitter).

2 thoughts on “Microsoft Fabric: Capacity Cost Management Part 2, Automate Pause/Resume Capacity with Azure Logic Apps

  1. Hello Soheil

    Thanks for that description – but I got a question – why in condition where you are using values 32,33,34… you are setting up “And” not “Or”? I did tested that, and as long as I stayed with “And” – app did not worked correctly

    1. Hi Piotr,
      I am selecting “AND” as I want the time frame to be between 8 AM and 4 PM, hence “AND”.
      Does that make sense?

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.