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​
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
| Variable | Default | Description |
|---|---|---|
location | eastus | Region of the resource group and storage account. The export covers resources in every region of the subscription. |
export_recurrence_duration | 17520h (about 2 years) | How long the daily export runs |
service_principal_password_duration | 17520h (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​
tofu output -json massdriver_integration_config
{
"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".
| Field | Key | Source |
|---|---|---|
| Directory (Tenant) ID | auth.tenant_id | tenant_id output |
| Subscription ID | auth.subscription_id | subscription_id output |
| Application (Client) ID | auth.client_id | client_id output |
| Client Secret | auth.client_secret | client_secret output |
| Storage Account Name | config.storage_account_name | storage_account_name output |
| Container Name | config.container_name | container_name output |
Resources created​
| Resource | Name | Purpose |
|---|---|---|
| Resource group | massdriver-costs-{suffix} | Holds the storage account |
| Storage account | mdcosts{suffix} | Stores the exports |
| Blob container | massdriver-costs-{suffix} | Container for the export files |
| Cost Management export | massdriver-costs | Daily export for the subscription |
| App registration | massdriver-cost-reader | Identity for Massdriver |
| Service principal and client secret | massdriver-cost-reader-secret | Credentials Massdriver uses |
| Role assignment | Storage Blob Data Reader | Read 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:
- Authenticates as the service principal.
- Lists the blobs under
massdriver-costs/in the container and downloads the newest. - Attributes each row to an instance by its
md-instancetag, or the legacymd-packagetag, 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-instanceormd-packagetag. - 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​
cd integrations/azure-cost-management-exports
tofu destroy
This deletes the storage account and its exports. Cost data Massdriver already collected is kept.