You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure中按位置获取指定日期范围的资源组成本?

按位置和日期范围查询Azure资源组成本

核心实现:通过Azure Cost Management Query API完成

你可以借助Azure Cost Management的Query API,精准筛选日期范围、按位置过滤并按资源组聚合成本,具体操作如下:


1. 指定日期范围

在API请求的timePeriod参数中设置起始和结束日期,格式为YYYY-MM-DD。比如要查询最近7天的数据,需自行计算对应的起止日期(例如2024-05-20至2024-05-26)。

2. 筛选目标区域

通过filter参数锁定指定位置的资源,以西欧为例,过滤语法为:dimensions[ResourceLocation] eq 'West Europe'

3. 按资源组聚合成本

在grouping参数中指定按ResourceGroup维度分组,同时通过aggregation设置成本值的求和聚合。


示例REST API请求

POST https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.CostManagement/query?api-version=2023-03-01
Content-Type: application/json

{
  "type": "ActualCost",
  "timePeriod": {
    "start": "2024-05-20",
    "end": "2024-05-26"
  },
  "dataset": {
    "granularity": "Daily",
    "filter": {
      "dimensions": {
        "name": "ResourceLocation",
        "operator": "In",
        "values": ["West Europe"]
      }
    },
    "aggregation": {
      "totalCost": {
        "name": "Cost",
        "function": "Sum"
      }
    },
    "grouping": [
      {
        "type": "Dimension",
        "name": "ResourceGroup"
      },
      {
        "type": "Dimension",
        "name": "ResourceLocation"
      }
    ]
  }
}

处理返回结果

API会返回按位置、资源组聚合后的成本数据,你可以将结果整理为目标格式:

West Europe
  RG: Europe1 $500
  RG: Europe2 $800

补充:Azure CLI方式

如果习惯用CLI,可执行az costmanagement query命令,参数逻辑与API一致:

az costmanagement query --subscription {subscriptionId} \
--type ActualCost \
--time-period start=2024-05-20 end=2024-05-26 \
--dataset granularity=Daily \
--filter "dimensions[ResourceLocation] eq 'West Europe'" \
--aggregation totalCost=Sum(Cost) \
--grouping type=Dimension name=ResourceGroup type=Dimension name=ResourceLocation

注:API暂不支持"最近7天"这类相对日期,需自行通过代码(如Python的datetime模块)计算起止日期。

内容的提问来源于stack exchange,提问作者Sandy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 08:50:08