Hi @Syahirah S ,
Thanks for using Microsoft Q&A!!
You can use Generate Cost Details Report - Create Operation REST endpoint to generate cost details report using Azure logic Apps or Azure Data Factory as per your requirements. I have provided similar solution (except for email part) using Azure Data Factory on another Microsoft Q&A thread over here.
You achieve the same using Azure logic apps with a solution something like below -
- Use a recurrence trigger to schedule it as per your requirements.
- Send an HTTP request to Cost Details Report API endpoint to generate the report in csv format.
Here I am using Azure AD Authentication with a Service Principal and provided "Contributor" permissions to the subscription by selecting your subscription > Access Control > Add
- You can then use Parse the Json returned from the API call, to fetch the BlobUrl from where you can download the generated file. Here is the Schema which you can use to parse the json -
{
"properties": {
"id": {
"type": "string"
},
"manifest": {
"properties": {
"blobCount": {
"type": "integer"
},
"blobs": {
"items": {
"properties": {
"blobLink": {
"type": "string"
},
"byteCount": {
"type": "integer"
}
},
"required": [
"blobLink",
"byteCount"
],
"type": "object"
},
"type": "array"
},
"byteCount": {
"type": "integer"
},
"compressData": {
"type": "boolean"
},
"dataFormat": {
"type": "string"
},
"manifestVersion": {
"type": "string"
},
"requestContext": {
"properties": {
"requestBody": {
"properties": {
"billingPeriod": {},
"invoiceId": {},
"metric": {
"type": "string"
},
"timePeriod": {
"properties": {
"end": {
"type": "string"
},
"start": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"requestScope": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"name": {
"type": "string"
},
"status": {
"type": "string"
},
"validTill": {
"type": "string"
}
},
"type": "object"
}
As Bloblink is under json array, you need to add ForEach loop to get the bloblink then use that for another HTTP connector to get the blob and use the return body of the HTTP in the email to send as an attachment, like below -
Here is the Full Logic Apps Code so that you can add and play around it in your subscription.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_each": {
"actions": {
"HTTP-Get_Cost_Report_Blob": {
"inputs": {
"method": "GET",
"uri": "@items('For_each')?['blobLink']"
},
"runtimeConfiguration": {
"contentTransfer": {
"transferMode": "Chunked"
}
},
"type": "Http"
},
"Send_an_email_(V2)": {
"inputs": {
"body": {
"Attachments": [
{
"ContentBytes": "@{base64(body('HTTP-Get_Cost_Report_Blob'))}",
"Name": "Cost-Management-Report.csv"
}
],
"Body": "<p>Test</p>",
"Importance": "Normal",
"Subject": "Cost Management Report",
"To": "saurabh@microsoft.com"
},
"host": {
"connection": {
"referenceName": "office365"
}
},
"method": "post",
"path": "/v2/Mail"
},
"runAfter": {
"HTTP-Get_Cost_Report_Blob": [
"SUCCEEDED"
]
},
"type": "ApiConnection"
}
},
"foreach": "@outputs('Parse_JSON')?['body']?['manifest']?['blobs']",
"runAfter": {
"Parse_JSON": [
"SUCCEEDED"
]
},
"type": "foreach"
},
"HTTP-GenerateCostDetailsReport": {
"inputs": {
"authentication": {
"audience": "https://management.azure.com",
"clientId": "{ClientId}",
"secret": "{ClientSecret}",
"tenant": "{TenantId}",
"type": "ActiveDirectoryOAuth"
},
"body": {
"metric": "ActualCost",
"timePeriod": {
"end": "2023-06-10",
"start": "2023-06-01"
}
},
"headers": {
"Content-Type": "application/json"
},
"method": "POST",
"uri": "https://management.azure.com/subscriptions/@{variables('subscription')}/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2023-03-01"
},
"runAfter": {
"Set_SubscriptionId": [
"SUCCEEDED"
]
},
"runtimeConfiguration": {
"contentTransfer": {
"transferMode": "Chunked"
}
},
"type": "Http"
},
"Parse_JSON": {
"inputs": {
"content": "@body('HTTP-GenerateCostDetailsReport')",
"schema": {
"properties": {
"id": {
"type": "string"
},
"manifest": {
"properties": {
"blobCount": {
"type": "integer"
},
"blobs": {
"items": {
"properties": {
"blobLink": {
"type": "string"
},
"byteCount": {
"type": "integer"
}
},
"required": [
"blobLink",
"byteCount"
],
"type": "object"
},
"type": "array"
},
"byteCount": {
"type": "integer"
},
"compressData": {
"type": "boolean"
},
"dataFormat": {
"type": "string"
},
"manifestVersion": {
"type": "string"
},
"requestContext": {
"properties": {
"requestBody": {
"properties": {
"billingPeriod": {},
"invoiceId": {},
"metric": {
"type": "string"
},
"timePeriod": {
"properties": {
"end": {
"type": "string"
},
"start": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"requestScope": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"name": {
"type": "string"
},
"status": {
"type": "string"
},
"validTill": {
"type": "string"
}
},
"type": "object"
}
},
"runAfter": {
"HTTP-GenerateCostDetailsReport": [
"SUCCEEDED"
]
},
"type": "ParseJson"
},
"Set_SubscriptionId": {
"inputs": {
"variables": [
{
"name": "subscription",
"type": "string",
"value": "abc2334-dfsd234-7293492349"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Hour",
"interval": 8
},
"type": "Recurrence"
}
}
},
"kind": "Stateful"
}
Please let me know if you have any questions. Please let me know if you have any questions.
Thanks
Saurabh
Please do not forget to "Accept the answer" wherever the information provided helps you to help others in the community.