如何使用ARM Template导出Azure成本分析数据为CSV文件并获取示例模板
关于使用ARM Template导出Azure成本使用报告的说明
功能支持确认
- 完全支持通过ARM Template配置成本使用报告(Cost Usage Report)的自动导出规则,可覆盖指定范围下所有资源的成本分析数据
- 原生支持导出为CSV格式,部署后规则自动生效,无需手动操作即可将报表投递到指定的Azure存储账户中
- 支持按租户、管理组、订阅、资源组多粒度配置导出范围,可根据实际需求选择导出全量或部分资源的成本数据
前置要求
- 你需要持有导出范围(租户/管理组/订阅等)的成本管理写入权限,以及目标存储账户的Blob数据写入权限
- 目标存储账户需要和导出规则部署在同一Azure区域
ARM模板示例
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "exportName": { "type": "string", "defaultValue": "AllResourceCostUsageExport", "metadata": { "description": "成本导出规则的名称" } }, "storageAccountId": { "type": "string", "metadata": { "description": "目标存储账户的资源ID" } }, "storageContainerName": { "type": "string", "defaultValue": "cost-export-reports", "metadata": { "description": "存放CSV报告的存储容器名称" } }, "exportScope": { "type": "string", "metadata": { "description": "导出范围,填订阅/管理组/租户的资源ID,例:/subscriptions/你的订阅ID" } }, "timePeriodStart": { "type": "string", "defaultValue": "[utcNow('yyyy-MM-dd')]", "metadata": { "description": "导出生效的开始日期,格式YYYY-MM-DD" } }, "timePeriodEnd": { "type": "string", "defaultValue": "2025-12-31", "metadata": { "description": "导出失效的结束日期,格式YYYY-MM-DD" } } }, "resources": [ { "type": "Microsoft.CostManagement/exports", "apiVersion": "2023-08-01", "name": "[parameters('exportName')]", "scope": "[parameters('exportScope')]", "properties": { "schedule": { "status": "Active", "recurrence": "Daily", "recurrencePeriod": { "from": "[parameters('timePeriodStart')]", "to": "[parameters('timePeriodEnd')]" } }, "format": "Csv", "deliveryInfo": { "destination": { "resourceId": "[parameters('storageAccountId')]", "container": "[parameters('storageContainerName')]", "rootFolderPath": "cur-exports" } }, "definition": { "type": "ActualCost", "timeframe": "MonthToDate", "dataSet": { "granularity": "Daily", "configuration": { "columns": [ "Date", "SubscriptionId", "SubscriptionName", "ResourceGroup", "ResourceName", "ResourceType", "PreTaxCost", "Currency", "ServiceName", "Region" ] } } } } } ] }
部署说明
- 将上述模板保存为
cost-export-template.json - 可使用Azure CLI执行部署,参考命令如下:
az deployment create --location 你要部署的区域名称 --template-file cost-export-template.json --parameters storageAccountId="你的存储账户完整资源ID" exportScope="你要导出成本的范围资源ID"
- 部署完成后,系统会按配置的周期(示例中为每日)自动生成CSV格式的成本报告,投递到指定存储容器中
内容的提问来源于stack exchange,提问作者Pooja Vembar
相关产品推荐
相关产品推荐

