This repo was forked from the CSAAppServiceDeployments repo. Initial idea and credits goes to @bwatts64.
- Function App to Ingest Azure cost data into Log Analytics
- Deploy to Azure
- Post deployment steps (!important)
Many Azure users (including myself) would like to more easily see Cost Anamolies that occur in their Azure environment. Currently, that functionality isn't available in Azure Cost Management (ACM) but if we were to ingest this data into Log Analytics, we could leverage the built-in anomaly detection in the product. The deployment below will create a Function app to ingest Azure cost management data into Log Analytics each day.
The architecture is made of the following resources:
- Azure Function App w/ System managed identity
- Azure Log Analytics
- Azure Application Insights
- Azure Storage Account
- Azure Monitor Workbook
Make sure to read the post deployment steps!
Bicep in used for the infrastructure as code. Any recent version of Azure PowerShell or Azure CLI can deploy a bicep file.
Be sure to be running latest version of Bicep
Here is an invocation example:
az deployment group create -g poc-costanomalydetection -f .\Templates\main.bicep --parameters scopes=providers/Microsoft.Billing/billingAccounts/cf9d8426-6a39-43c3-9709-128a891ad2d5:5a0b4b8f-5fa6-49fc-8f4e-7743746d8f04_2019-05-31
After deploying this solution you have to give the App Service system assigned managed identity "Billing account reader" or "Cost Management Reader" role at the scope or scopes you are quering. The system assigned managed identity will have the same name as your function app.
If you want to load historical data into Log Analytics you can utilize the function named PreLoadLogAnalytics.
- Get the function url by opening the Function app and clicking on Get Function URL. Note that it may take a little bit to light up.
- Use a tool like PostMan or curl to send the request. Below is an example using curl.
Use the below link to deploy this solution to Azure. Note Make sure you follow the Post Deployment Steps after completing the deployment.
Note There are many parameters that you can supply, only one is required, scopes
:
- deploymentPrefix: this will prefix the name of all the resources created.
- scopes: this defines the scope or scopes for the Cost Management API.
- ex: providers/Microsoft.Billing/billingAccounts/cf9d8426-6a39-43c3-9709-128a891ad2d5:5a0b4b8f-5fa6-49fc-8f4e-7743746d8f04_2019-05-31
- ex: subscriptions/5f1c1322-cebc-4ea3-8779-fac7d666e18f
- ex: subscriptions/5f1c1322-cebc-4ea3-8779-fac7d666e18f, subscriptions/718c1322-cebc-4ea3-8779-fac7d666e18f
More informations on scopes can be found here: https://docs.microsoft.com/en-us/rest/api/cost-management/query/usage
Below are the details on the Bicep file used to deploy this solution.
The data from Azure Cost API will be stored in this Log Analytics Workspace. So the first step is to deploy the workspace.
We want to be able to monitor the Azure Function using Application Insights. So before we deploy our Azure Function we deploy Application Insights
Azure Function store data on a storage account. So before we deploy our Azure Function we deploy a Storage Account
Now that we have all the pre-reqs deployed we can deploy the Azure Function. The most important part of this deployment is configuring the Application Settings.
Having the Function app, we now need to actually deploy our functions. The code for this is sitting in this repo under the AzureCost_to_LogAnalytics folder.
The last step is to actually deploy the workbook that will present the data from the discovered anomalies. The definition of the workbook is in the file ./Templates/CostWorkbook.json and get injected by Bicep during the deployment.