# Challenge 12: Azure Automation - Send me yesterday's Azure cost

# Here is what you will learn 🎯

  • How to use an Azure Automation account with PowerShell runbooks.
  • Get insights in your Azure consumption for the day.

πŸ” This challenge was adapted from MyAzure Cost (opens new window). It will send you an email with your daily Azure cost report.

Each day you will get a report of the usage and the costs in your inbox :

  • A daily cost email

    daily email

  • Cost report as .csv

    cost report

It also contains e.g.:

  • Historic data (using an Azure table)

    7days History

  • Cost Per Category

    Costs Per Category

# A look behind the curtain

The overall architecture looks likes this:

Architecture

It comprises:

  • An ARM template for setup
  • Azure automation for daily tasks
  • .NET code to send email and analysis
  • A storage account to hold the data

# Table Of Contents

  1. Deploy the ARM Template
  2. Create an Azure Run As Account
  3. Create a Table and see AA Variables Section
  4. Upload a Price Sheet
  5. Run a Report
  6. Cleanup

# Deploy the ARM Template

Click on the button.

Name Values
Region West Europe
Resource group rg-myAzureCost
O_my Azure Cost Smtp Recipient %the destination email address%
O_my Azure Cost Smtp Sender %the source email/sender address%
O_my Azure Cost Smtp Sender Password %the email sender's smtp pwd%
O_my Azure Cost Smtp Server %the senders smtp server% e.g. smtp.office365.com
O_my Azure Cost Smtp Server SSL Port e.g. 587 for smtp.office365.com
O_base Time don't touch

The deployment should take < 10mins.

# Create an Azure Run As Account

For Azure Automation (AA) to perform tasks in the current subscription (e.g. gather usage information) needs an account a so called Run As Account. This account is a so called service principal (SP) which has permissions in the current subscription. To create this SP and connect it to AA please do the following:

[Azure Portal] 
-> Resource Groups 
-> "rg-myAzureCost" 
-> 'aaazurecost...' (Your Automation Account) 
-> Account Settings 
-> Run as accounts

Hit Create, wait and watch the account being created:

Create AA Run as account

TIP

πŸ“ Note that this account has an expiration date.

# See how the AA Run As account manifests itself throughout AAD and your subscription (optional)

  • 'AA Run As' is an Azure AD - Application Registration

    `[Azure Portal] 
    -> Azure Active Directory 
    -> App registrations` 
    

    ApplicationRegistration

  • 'AA Run as' has contributor rights to your subscription

    [Azure Portal] 
    -> Subscriptions 
    -> Access Control (IAM) 
    -> View role assignments 
    -> 'View'`
    

    ApplicationRights

# Create a Table and see AA Variables Section

Storing settings for Azure Automation (AA) e.g. account information, locale settings,... AA credentials and AA variables can be used. In our myAzureCost sample we use:

  • AA Credentials to store the sender's account details
  • AA Variables to hold settings needed for the AA Runbooks that do the usage and cost calculation
  • One Azure table to store processed data e.g. daily cost history.

# Inspect the variables

[Azure Portal] 
-> Resource Groups 
-> "rg-AzureCost" 
-> 'aaazurecost...' (Your Automation Account) 
-> Variables
Variable Name Description
myAzureCostAzureSubscriptionId GUID of your subscription* to calculate the usage for
myAzureCostCultureInfo e.g. de-DE for reports (CSVs) to come with numbers, date formatted in German format
myAzureCostPriceSheetURI a URI pointing to a CSV with pricing information about Azure resources - we'll take care of this soon
myAzureCostSAContainer where your daily reports are stored - pls don't change
myAzureCostSATable table name to hold your daily usage costs for 'history view'
myAzureCostSmtpRecipient email recipient of the report
myAzureCostStorageAccountName where your daily reports are stored

# Create the Azure table to hold your daily usage costs for 'history view'

There is a AA Runbook that will create an Azure table for us - we only need to start it:

[Azure Portal] 
-> Resource Groups 
-> "rg-AzureCost" 
-> 'aaazurecost...' (Your Automation Account) 
-> "Process Automation" Runbooks 
-> RunBk_CreateTable -> Start

Trigger Runbook Create Table

TIP

πŸ“ A runbook is a piece of code (here PowerShell) that is being executed in an Azure runtime environment.

This runbook will execute PowerShell code that creates an Azure table using the AA Run as Account.

It'll login to your subscription as the Run as Account and perform tasks against your subscription. You can follow the execution by:

Follow runbook output

Once completed you should have a new table in:

[Azure Portal] 
-> Resource Groups 
-> "rg-AzureCost" 
-> 'azconsumption...' (Your Storage Account) 
-> Tables

History costs table created

# Upload a price sheet

myAzureCost can gather your daily consumption data. Additionally it can also estimate the costs that your consumption will pose.

To do this you need to upload a price sheet with your specific Azure rates. The price sheet needs to be formatted as CSV (en-us) and contain at least 2 columns: MeterID and MeterRates.

TIP

πŸ“ Every Azure resource in each region has a MeterID (GUID) that uniquely identifies it. When you query the usage of an Azure resource the MeterID is delivered with it. The MeterID translates to a price -> MeterRates - e.g.:

MeterId MeterName MeterRates MeterRegion
793843d0-d081-4934-9782-ee92505c56cb D2 v3 0.1011.. EU West

TIP

πŸ“ Price information for Azure resources is accessible through the RateCard API (opens new window). If you want to dig into some details go here for a sample (opens new window)

  1. You find a sample price list in this directory: day1\challenge-12\challengestart\Sample_PriceSheet_EN.csv

  2. Upload this to your storage account:

[Azure Portal] 
-> Resource Groups 
-> "rg-AzureCost" 
-> 'azconsumption...' (Your Storage Account) 
-> Containers 
-> 'consumption'

Pricelist on storage account

  1. Generate a URI with a Read only SAS token (e.g. expiry +2 years) for this fil. If you have done challenge 6 you know how to do this πŸ˜‰

  2. Copy & paste the URI (with the SAS token) into the AA variable myAzureCostPriceSheetURI so that runbooks can download the pricesheet:

[Azure Portal] 
-> Resource Groups 
-> "rg-AzureCost" 
-> 'aaazurecost...' (Your Automation Account) 
-> Variables

pricelist URI in AA variables

# Run a report

Here you'll kick off the runbooks to test your myAzureCost implementation. You'll also might want to link the runbook to a schedule to receive a daily report.

# Gather your daily usage Runbook

Start the AA Runbook that will gather the Azure usage for the previous day. It will save it as CSV (en-us) in the storage account. Leave the MYDATE parameter empty.:

[Azure Portal] 
-> Resource Groups 
-> "rg-AzureCost" 
-> 'aaazurecost...' (Your Automation Account) 
-> "Process Automation" Runbooks 
-> RunBk_GetUsageAggregates 
-> Start

TIP

πŸ“The optional MYDATE parameter takes a short en-us formatted time string MM/dd/yyyy -> e.g. '07/13/2020'

Once the runbook is completed you should find a result report in your storage account:

[Azure Portal] 
-> Resource Groups 
-> "rg-AzureCost" 
-> 'azconsumption...' (Your Storage Account) 
-> Containers 
-> 'consumption'

Usage aggregates of previous day in sa

# Send cost report email

As the previous runbook has calculated the Azure consumption for a day and stored it on our storage account. We can now start the AA Runbook that will do a cost estimation and send it as email the recipient. Leave the MYDATE parameter empty.:

[Azure Portal] 
-> Resource Groups 
-> "rg-AzureCost" 
-> 'aaazurecost...' (Your Automation Account) 
-> "Process Automation" Runbooks 
-> RunBk_SendCostEmail 
-> Start

TIP

πŸ“ The optional MYDATE parameter takes a short en-us formatted time string MM/dd/yyyy -> e.g. '07/13/2020'

Once the runbooks is completed you should receive an email with the costs & graphs calculated and some reports attached as CSV:

Cost email

πŸ₯³ Congratulations! πŸ₯³

... and wait for tomorrows email πŸ˜ƒ

# Cleanup

Delete the resource group rg-myAzureCost

β—€ Previous challenge | πŸ”Ό Day 1