It is a Azure burn rate calculation on logic app.
Everyday at UTC 6:00 am, it grabs the daily cost of all resources used on the day before yesterday (TDBY) and also three days before (ThreeDayBefore) by using Azure Consumption API. Afterwards, it sums up each day's cost. The burn rate is calculated as an increase rate:
Burn Rate = (TDBY - ThreeDayBefore)/ThreeDayBefore
Follow only steps in the section "Create your logic app" here https://docs.microsoft.com/en-us/azure/logic-apps/quickstart-create-first-logic-app-workflow.
Now you will have an empty logic app. Open it and click "blank logic app" in the templates.
Pick "code view", delete the content and copy paste the whole logicapptemplate.txt here https://github.com/wcex1994/burnrate/blob/master/logicapptemplate into the window. Now go back to the designer view.
In the "Sublist" variable, add in a list of subscription IDs that you would like to calculate the burn rate.
You can grab related information with the following PowerShell commands:
Login-AzureRmAccount
Get-AzureRmSubscription | Export-Csv "C:\SOME_PATH\Azure Subscription.csv"
In order to be authenticated and aurthorized to call the Azure Comsumption API, we use Managed Identity and Access Control (IAM). Notice there might be better methods depends on your scenario.
-
Turn on the Managed Identity in the logic app:
Go to your logic app --> identity --> click "On"
-
Grant your logic app "Contributor" role in the subscriptions that you would like to calculate the burn rate.
Subscription --> IAM --> Add Role
Choose "Contributor" and search for your logic app name --> "Save"
If you have many subscriptons, you can also grant authorization by PowerShell, see https://docs.microsoft.com/en-us/azure/role-based-access-control/tutorial-role-assignments-user-powershell#grant-access.
In "condition 3" in the logic app designer view, edit threshold and actions. For example, you can send an email in outlook.
-
Error control:
E.g. If the cost of previous day is 0, then how does it work to calculate the burn rate, since we cannot divide something by 0.
-
First day of month:
The Consumption.UsageDetails.List API only returns current billing period's usage. Azure counts one month as a billing period. If you are on Feb 1st, then to calculate Jan 30th vs. Jan 31st burn rate, you would need to call Consumption.UsageDetails.ListByBillingPeriod API with '201901' as the billing period. See here https://docs.microsoft.com/en-us/rest/api/consumption/usagedetails/listbybillingperiod for more details.
-
Recurrence:
I would recommend to have recurrence three times a day (morning, afternoon and night) just to double check.
- Consumption.UsageDetails.List API: https://docs.microsoft.com/en-us/rest/api/consumption/usagedetails/list
- Consumption.Budget API: https://docs.microsoft.com/en-us/rest/api/consumption/budgets
- Managed Identity: https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview
- IAM: https://azure.microsoft.com/en-ca/product-categories/identity/