This Azure Function is an example how to retrieve subscriptions details from Azure Resource Manager REST API for multiple tenants and return merge data as JSON which can be later used e.g. in Power BI report.
What the functions does:
- Authenticates to each tenant.
- Retrieves subscriptions list from Azure Resource Manager REST API.
- Returns merged data for all tenants.
- Create Azure AD service principal
- Add Azure AD service principal to tenants
- Configured permissions
- Deployed and configured Azure Function App
Create a service principal in your primary tenant (e.g., where the Azure Function will be deployed). Service principal will be used to authenticate Azure Function in all tenants.
Follow these steps to create service principal in Azure AD:
- Sign-in to the Azure portal.
- Search for and Select
Azure Active Directory. - Select
App registrations, then selectNew registration. - Name the application, for example
example-app. - Select account type
Accounts in any organizational directory (Any Azure AD directory - Multitenant). - Select
Register.
You've created your Azure AD application and service principal. Note down the application (client) id (will be used later).
Now create client secret:
- Select
Certificates & secrets, then selectClient secretsandNew client secret - Enter secret description, for example
function-secretand selectAdd. - Note down the
Valueof newly created secret (it will be not visible anymore after you leave this screen).
In previous step Azure AD app registration and service principal was created in primary tenant. Additional service principal to app registration must be created in order to allow authentication for Azure Function to other tenants. This service principal must be created in tenant where Azure Function should have access.
Follow these steps to add service principal to other tenants:
- Using Azure CLI, login to tenant where service principal should be added:
az login --tenant "{tenantId}"- Create service principal for app registration from primary tenant:
az ad sp create --id "{applicationId}" \
--query "{servicePrincipalId:id,appId:appId,displayName:displayName}"Response example is:
{
"appId": "4c3e3be1-b735-41b1-a842-f095b9a45849",
"displayName": "App in primary tenant",
"servicePrincipalId": "2ae09b6c-6b2d-4ce0-984c-d52eb3a9a406"
}- Note down the service principal id (will be used later).
Run above steps for each tenant where Azure Function should have access.
Access to subscriptions within tenants must be granted to allow Azure Function read subscriptions data. Access is granted adding by assigning service principal to Reader role on subscription level.
Follow these steps to assign service principal to Reader role on subscription level:
- Using Azure CLI, login to tenant where service principal should be assigned to Reader role:
az login --tenant "{tenantId}"- Create assignment:
az role assignment create --assignee "{servicePrincipalId}" \
--role "Reader" \
--scope "/subscriptions/{subscriptionNameOrId}"Run above steps for each tenant where Azure Function should have access.
- Using Azure CLI, login to tenant where Azure Function App should be created:
az login --tenant "{tenantId}"- Create a resource group
az group create --name "{resourceGroup}" --location "{location}"- Deploy Azure Function App from Bicep template located in repository
az deployment group create --resource-group {resourceGroup} \
--template-file ./bicep/main.bicep \
--parameters location='{location}' prefix='{resourcesPrefix}' \
--query {functionAppName:properties.outputs.functionAppName.value}- Note down the Azure Function App name (will be used later).
- Create a deployment zip file in the repository directory with following commands:
dotnet publish -o ./artifacts
cd artifacts
zip -r -X artifacts.zip *- Deploy zip file to Azure Function App
az functionapp deployment source config-zip \
-g {resourceGroup} \
-n {functionAppName} \
--src {zipFilePath}- Sign-in to the Azure portal.
- Search for and Select given Azure Function App.
- Select
App registrations, then selectNew application setting. - Add following application settings:
| Name | Value |
|---|---|
| TenantIds | Comma-separated list of tenant ids |
| ClientId | Application (client) id created in Create Azure AD service principal step |
| ClientSecret | Client secret created in Create Azure AD service principal step |
- Save your changes
- Sign-in to the Azure portal.
- Search for and Select given Azure Function App.
- Select
Functions, then select deployed Azure Function. - Select
Get Function Urland copy the Azure Function (including function key) - Paste url in browser and the Azure Function should return subscriptions data in JSON format. Azure Function can be used e.g. as data source for Power BI report.
By default Azure Function is publicly available and can be called by anyone who has the function key. It is recommended to secure access to Azure Function e.g. with access restrictions and only allow calls e.g. from PowerBI service (which is using Azure Function to generate report).
Following steps will restrict access to Azure Function:
- Sign-in to the Azure portal.
- Search for and Select given Azure Function App.
- Select
Functions, then selectNetworking. - Select
Access Restrictions, then selectAdd. - Define new rule with following values and add it:
| Field | Value |
|---|---|
| Name | AllowPowerBIService |
| Priority | 100 |
| Type | Service Tag |
| Service Tag | PowerBI |
- Change
Unmatched rule actiontoDeny. - Save your changes.
Now Azure Function app will be available only for requests from Power BI service.
List subscriptions - Azure Resource Manager REST API
Distributed under MIT License. See LICENSE for more details.