How to send generated cost management exports csv file directly to email monthly?

Syahirah S 0 Reputation points
2023-08-04T09:02:53.6433333+00:00

Currently I have been monitoring monthly cost management. I have schedule exports that allows me to retrieve a CSV file monthly. However, I have to log into the portal to be able to download the file and send to user via email. Is there any way I can have the CSV file automatically forward to my email from every exports?

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
2,909 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Saurabh Sharma 23,826 Reputation points Microsoft Employee
    2023-08-05T00:02:27.1866667+00:00

    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 -

    1. Use a recurrence trigger to schedule it as per your requirements.
    2. Send an HTTP request to Cost Details Report API endpoint to generate the report in csv format.

    User's image

    User's image

    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

    User's image

    1. 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 -

    enter image description here

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.