Stop exporting your Cost Management data to Azure SQL Database – Part 2

Are you going to show us how o setup a daily data refresh for the Cost Management export?

Yes but first of all I need to show you something and you tell me if you see anything wrong:

Daily export month-to-date costs

Well, the daily export is missing.

That’s right. This is how they do it at Microsoft; because they cannot really calculate your daily consumption each day (which it’s already odd by itself) they propose you:

  • Daily export of month-to-date costs: II don’t know for what reason but Microsoft cannot calculate how much you spent yesterday. That’s why they don’t propose a daily export but a month-to-date export. This way you are forced to substitute that file every day and their miscalculation are corrected.
  • Weekly export of last 7 days: never tried yet, put a comment if you know how it behaves
  • Monthly export of last month’s costs: Each monthly export will be executed on the 5th of every month. This means that Microsoft is giving us an hint about how many days they need to calculate it right, 5 days.
  • One-time export: use it carefully, now that you know that you don’t have that precision in the last 5 days

So what we are going to do now is setting up a Logic App that will substitute the month-to-date export daily.

And are you going to give us the step by step roadmap with pictures?

Open your mouth, the airplane is coming… here the first step:

1) Recurrence

Setup a regular Recurrence with 1 hour interval. You cannot really tell at what time Microsoft is going to drop that file:

Logic App Recurrence2

2) List blobs (V2)

Use List blob (V2) to target a valid Storage account and a container:

Logic App List blobs (V2)

3) Initialize Variable

We create a variable with function utcNow():

Logic App Initialize Variable
4) Initialize Variable 2

And another variable in which we are going to use the function

concat(split(variables('CurrentDate'),'-')[0],split(variables('CurrentDate'),'-')[1]):

Logic App Initialize Variable2

5) For each

OK, this step is much more complicated that you think, it looks like this:

Logic App For each

I’m going to brake it down in small chunks so you don’t get mad. The very first step says check if there is a folder and if its name starts with a date:

Log App Condition

If that is the case take the name of that folder, which is our input folder, navigate to it, and enumerate if there are files in it

Logic App List blobs in current month folder2

And now go into the LatestCSV folder, which is our output folder, and see if there are files into it

List blobs in Latest CSV

And is the folder LatestCSV empty? No? Well, that’s alright, in fact is containing yesterday’s month-to-date CSV Export. So go into that folder and delete that file: 

Is LatestCSV empty

And now go to the input folder where the CSV are exported, copy that CSV to the LatestCSV folder, and after that you have moved the file delete the folder that was containing it:

Last steps

That wasn’t easy.

Yeah, I know.

It’s a pity that Microsoft hasn’t put in place an auto month-to-date cancelling.

In fact it would be that easy: because the Storage account cannot contain files with the same name they should have named the month-to-date export always with the same name.

This way the month-to-date export would have automatically replaced the previous one.

But why doing easy things when you can do hard ones?

Related

Leave a Reply

Your email address will not be published. Required fields are marked *