By: Koen Verbeeck | Updated: 2023-09-01 | Comments (12) | Related: 1 | 2 | 3 | 4 | 5 | > Microsoft Fabric
Problem
I created a Microsoft Fabric capacity in Azure and assigned it to a workspace. I understand that if the capacity is paused, this will save me on cost since the capacity is billed for the duration it is running. However, it's not feasible to manually start or pause this capacity manually each time. Is there a way to automate this?
Solution
For an introduction to Microsoft Fabric and its capacities, check out the following tips:
Unfortunately, at the time of writing, there are no dedicated Microsoft Fabric REST API endpoints to help us with this automation job. There is a Power BI REST API for capacities, but it's unrelated to Fabric capacities (at least not the Azure F-SKU you can pause/resume). Luckily, we still have the REST APIs of Azure itself. This tip will show how to use the Azure REST API to start a paused capacity. Hopefully, a dedicated Microsoft Fabric REST API will be available in the future.
We'll use Azure Data Factory (ADF) as the tool of choice to start the Fabric capacity. You can also use other services, such as Azure Functions or Azure Logic Apps, but the principles remain the same. You could also use a Fabric data pipeline (which is similar to using ADF). The problem is that you cannot use a Fabric pipeline to start a capacity if the pipeline belongs to a workspace with the same capacity assigned. This would mean you would need multiple capacities, where there's one capacity used to start/pause the other capacities. Having such a capacity continuously running comes with a cost. It's much cheaper to use ADF or any other services, but as a downside, not everything will be centralized in Microsoft Fabric.
Resume a Capacity with the Azure REST API
For an introduction to ADF, check out this tutorial. Create a new pipeline and add a Web activity to the canvas. Next, add three parameters to the pipeline:
- Subscription – This is the guide to the subscription hosting the capacity.
- Resourcegroup - This is the name of the resource group that contains the capacity.
- Capacity – This is the name of the capacity.
Having these values parameterized will be helpful if you want to pause/resume multiple capacities (and you want your pipeline being called from a For Each loop) or if you are deploying the pipeline from one environment to another.
As a first step, we will check the pipeline to see if the capacity is already running.
We can do this by calling the Microsoft.Fabric/capacities endpoint. For the URL property, configure the following expression:
https://management.azure.com/subscriptions/@{pipeline().parameters.subscription}/resourceGroups/@{pipeline().parameters.resourcegroup}/providers/Microsoft.Fabric/capacities/@{pipeline().parameters.capacity}?api-version=2022-07-01-preview
Make sure there are no typos in the URL. This can result in unexpected errors. For example, I made a small mistake in the API version, and the following error was returned:
The Web activity will use the GET HTTP method to retrieve the capacity status. As for the authentication, it's easiest to use the system-assigned management identity of the ADF instance. As the resource, we need to use https://management.azure.com.. The Web activity has the following configuration:
To start or pause a capacity, contributor permissions are required on the capacity level. In the Azure portal, go to the capacity and then to Access Control (IAM). Assign the ADF managed identity to the contributor role.
If the needed permissions are missing, the following error is returned:
For the moment, it's not possible to add this specific action to a custom role, so the contributor role is the bare minimum.
When we debug the pipeline, the following output will be returned by the Web activity:
The state property is the one we need. In the screenshot, the value is Active, which means the capacity is running. A state of Paused is returned when the capacity is not running. Other information is also returned, such as the capacity admin, the location, and the current SKU of the capacity.
Let's add an If condition to the canvas and connect it to the Web activity. We will use this condition to check if the capacity is running or not based on the output of the Web activity. You can use the following expression:
@equals(activity('Get Capacity Info').output.properties.state,'Paused')
If the expression returns true, the capacity is paused and needs to be started. In the True section of the condition, add another Web activity. This time we will use a POST message to the REST API to start the capacity.
In the Web activity, use the following expression for the URL:
https://management.azure.com/subscriptions/@{pipeline().parameters.subscription}/resourceGroups/@{pipeline().parameters.resourcegroup}/providers/Microsoft.Fabric/capacities/@{pipeline().parameters.capacity}/resume?api-version=2022-07-01-preview
This time we're calling the resume endpoint. The method is set to POST, and a dummy body is specified. The same settings are used for the authentication as in the other Web activity.
When we debug the pipeline, we see the Web activity calls the API to start the capacity.
If the capacity is already running, the Web activity inside the If condition is not executed:
Keep in mind that the API call is asynchronous, meaning ADF will not report if there was an error starting the capacity. A succeeded Web activity means the API call was successfully made but doesn't necessarily mean the capacity is started. You could extend the pipeline with a Wait activity and then another Web activity that checks if the capacity has actually started.
If you want to pause a capacity, you can use the suspend endpoint. A good use case for a pipeline that shuts down a capacity is one that runs at the end of every business day. This would prevent unnecessary costs if the capacity would run all night with no one to use it.
Conclusion
In this tip, we've shown you how to build a simple pipeline in ADF that will resume a paused Fabric capacity. A Fabric capacity cannot start itself, so it's not possible to build a Fabric pipeline where you start your one and only capacity. You would need another capacity or a P-SKU capacity; both incur extra costs.
The downside of this functionality in ADF (or another tool like Azure Logic Apps) is that not all logic is consolidated in Microsoft Fabric itself.
Next Steps
- If you haven't already, check out the tip, What is Microsoft Fabric?
- Keep your eye on MSSQLTips.com for more Fabric tips!
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2023-09-01