Skip to main content

Azure Cost Management Exports

The Azure Cost Management Exports integration reads Azure cost exports and attributes cost to instances.

Prerequisites​

  • An Azure subscription with Cost Management access
  • Permission to create resource groups, storage accounts, app registrations, and role assignments
  • OpenTofu or Terraform, and the Azure CLI

Setup​

Step 1: Apply the module​

shell
git clone https://github.com/massdriver-cloud/integrations.git
cd integrations/azure-cost-management-exports
az login
az account set --subscription "YOUR_SUBSCRIPTION_ID"
tofu init
tofu apply
VariableDefaultDescription
locationeastusRegion of the resource group and storage account. The export covers resources in every region of the subscription.
export_recurrence_duration17520h (about 2 years)How long the daily export runs
service_principal_password_duration17520h (about 2 years)How long the client secret is valid
tags{}Tags for Azure resources. managed-by = massdriver is always set.
azuread_tags[]Tags for the app registration and service principal. massdriver is always set.

The export stops and the client secret expires when these durations end. Apply the module again before then.

Step 2: Read the outputs​

shell
tofu output -json massdriver_integration_config
json
{
"tenant_id": "00000000-0000-0000-0000-000000000000",
"subscription_id": "00000000-0000-0000-0000-000000000000",
"client_id": "00000000-0000-0000-0000-000000000000",
"client_secret": "***",
"storage_account_name": "mdcostsa1b2c3d4",
"container_name": "massdriver-costs-a1b2c3d4"
}

Step 3: Create the integration​

Under Organization settings → Integrations, click Integrate on Azure Cost Management Exports, or call createIntegration with id: "azure-cost-management-exports".

FieldKeySource
Directory (Tenant) IDauth.tenant_idtenant_id output
Subscription IDauth.subscription_idsubscription_id output
Application (Client) IDauth.client_idclient_id output
Client Secretauth.client_secretclient_secret output
Storage Account Nameconfig.storage_account_namestorage_account_name output
Container Nameconfig.container_namecontainer_name output

Resources created​

ResourceNamePurpose
Resource groupmassdriver-costs-{suffix}Holds the storage account
Storage accountmdcosts{suffix}Stores the exports
Blob containermassdriver-costs-{suffix}Container for the export files
Cost Management exportmassdriver-costsDaily export for the subscription
App registrationmassdriver-cost-readerIdentity for Massdriver
Service principal and client secretmassdriver-cost-reader-secretCredentials Massdriver uses
Role assignmentStorage Blob Data ReaderRead access to the storage account

Service principal permissions​

The service principal has one role: Storage Blob Data Reader on the storage account. It can read the exports and nothing else.

Export configuration​

  • Type: ActualCost
  • Timeframe: MonthToDate
  • Recurrence: daily
  • Format: CSV

Data collection​

Every 24 hours, Massdriver:

  1. Authenticates as the service principal.
  2. Lists the blobs under massdriver-costs/ in the container and downloads the newest.
  3. Attributes each row to an instance by its md-instance tag, or the legacy md-package tag, and totals the cost per instance.

Switching to custom default tags​

Organizations that use the legacy default tags send md-package on every resource. When an organization switches to a custom list of default tags, Massdriver stops sending md-package and sends md-instance instead. Each resource gets the new tags at the next deployment of its instance.

No change to the export is necessary. Cost Management exports include all resource tags.

Troubleshooting​

Enable fails with access_denied​

Storage returned 403. Check that the service principal has Storage Blob Data Reader on the storage account. New role assignments can take a few minutes to apply.

Enable fails with container_not_found​

Storage returned 404. Check Storage Account Name and Container Name against the module outputs.

No cost data appears​

  • Check that resources have the md-instance or md-package tag.
  • The first export can take up to 24 hours to appear.
  • Check in the Azure portal that the export is writing to the container.

Authentication errors​

  • Check that the client secret has not expired.
  • Check the tenant ID and subscription ID.

Cleanup​

shell
cd integrations/azure-cost-management-exports
tofu destroy
danger

This deletes the storage account and its exports. Cost data Massdriver already collected is kept.