mcpField
A
az-cost-optimize

az-cost-optimize

github.com/github云与 DevOps40.5k8.7k 次安装今天更新
代码仓库

关于 az-cost-optimize

用于分析 Azure 资源和基础设施即代码文件、寻找节省成本机会的工作流技能。它验证成本与使用数据,并在用户批准后创建单项 GitHub issue 和协调用的 EPIC issue。

主要功能

  1. 01发现 Azure 资源和 IaC 文件
  2. 02检查使用指标与当前成本
  3. 03制定有证据支持的节省建议
  4. 04按优先级为建议评分
  5. 05获批准后创建 GitHub issue

适用场景

  1. 01审查 Azure 基础设施以寻找节省空间
  2. 02将优化建议转为可跟踪任务
  3. 03通过 EPIC 协调多项优化

MCP 相关

使用 Azure MCP 工具获取最佳实践、发现资源并检查监控数据;也可回退到 Azure CLI。经用户确认后,使用 GitHub MCP 工具创建优化 issue 和 EPIC issue。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/github/awesome-copilot --skill az-cost-optimize

安装后在 Claude Code 中输入/az-cost-optimize确认已加载。

详细介绍

Azure 成本优化

此工作流分析基础设施即代码(IaC)文件和 Azure 资源,以生成成本优化建议。它会为每个优化机会创建单独的 GitHub issue,并创建一个 EPIC issue 来协调实施,从而高效跟踪和执行成本节约计划。

先决条件

  • 已配置并完成身份验证的 Azure MCP 服务
  • 已配置并完成身份验证的 GitHub MCP 服务
  • 已确定目标 GitHub 仓库
  • 已部署 Azure 资源(IaC 文件可选,但有帮助)
  • 在可用时,优先使用 Azure MCP 工具(azmcp-*),而不是直接使用 Azure CLI

工作流步骤

步骤 1:获取 Azure 最佳实践

操作:在分析前检索成本优化最佳实践 工具:Azure MCP 最佳实践工具 流程:

  1. 加载最佳实践:
    • 执行 azmcp-bestpractices-get 以获取一些最新的 Azure 优化指南。这可能无法覆盖所有场景,但提供了基础。
    • 尽可能使用这些实践来指导后续分析和建议
    • 在优化建议中引用最佳实践,无论是来自 MCP 工具输出还是一般 Azure 文档
步骤 2:发现 Azure 基础设施

操作:动态发现并分析 Azure 资源和配置 工具:Azure MCP 工具 + Azure CLI 回退 + 本地文件系统访问 流程:

  1. 资源发现:

    • 执行 azmcp-subscription-list 以查找可用订阅
    • 执行 azmcp-group-list --subscription <subscription-id> 以查找资源组
    • 获取相关资源组中所有资源的列表:
      • 使用 az resource list --subscription <id> --resource-group <name>
    • 对于每种资源类型,尽可能先使用 MCP 工具,然后回退到 CLI:
      • azmcp-cosmos-account-list --subscription <id> - Cosmos DB 账户
      • azmcp-storage-account-list --subscription <id> - 存储账户
      • azmcp-monitor-workspace-list --subscription <id> - Log Analytics 工作区
      • azmcp-keyvault-key-list - Key Vault
      • az webapp list - Web 应用(回退 - 没有可用的 MCP 工具)
      • az appservice plan list - App Service 计划(回退)
      • az functionapp list - Function 应用(回退)
      • az sql server list - SQL Server(回退)
      • az redis list - Redis Cache(回退)
      • ... 以及其他资源类型依此类推
  2. IaC 检测:

    • 使用 file_search 扫描 IaC 文件:"/*.bicep"、"/*.tf"、"/main.json"、"/template.json"
    • 解析资源定义以了解预期配置
    • 与已发现的资源进行比较,以识别差异
    • 记录 IaC 文件的存在,以便后续实施建议
    • 不要使用仓库中的任何其他文件,仅使用 IaC 文件。不允许使用其他文件,因为它不是事实来源。
    • 如果未找到 IaC 文件,则停止并向用户报告未找到 IaC 文件。
  3. 配置分析:

    • 提取每个资源的当前 SKU、层级和设置
    • 识别资源关系和依赖关系
    • 在可用时映射资源使用模式
步骤 3:收集使用指标并验证当前成本

操作:收集使用率数据并验证实际资源成本 工具:Azure MCP 监控工具 + Azure CLI 流程:

  1. 查找监控源:

    • 使用 azmcp-monitor-workspace-list --subscription <id> 查找 Log Analytics 工作区
    • 使用 azmcp-monitor-table-list --subscription <id> --workspace <name> --table-type "CustomLog" 发现可用数据
  2. 执行使用情况查询:

    • 使用 azmcp-monitor-log-query 配合以下预定义查询:
      • 查询:"recent" 用于近期活动模式
      • 查询:"errors" 用于指示问题的错误级别日志
    • 如需自定义分析,请使用 KQL 查询:
    // CPU utilization for App Services
    AppServiceAppLogs
    | where TimeGenerated > ago(7d)
    | summarize avg(CpuTime) by Resource, bin(TimeGenerated, 1h)
    
    // Cosmos DB RU consumption  
    AzureDiagnostics
    | where ResourceProvider == "MICROSOFT.DOCUMENTDB"
    | where TimeGenerated > ago(7d)
    | summarize avg(RequestCharge) by Resource
    
    // Storage account access patterns
    StorageBlobLogs
    | where TimeGenerated > ago(7d)
    | summarize RequestCount=count() by AccountName, bin(TimeGenerated, 1d)
    
  3. 计算基线指标:

    • CPU/内存使用率平均值
    • 数据库吞吐量模式
    • 存储访问频率
    • 函数执行速率
  4. 验证当前成本:

    • 使用步骤 2 中发现的 SKU/层级配置
    • 在 https://azure.microsoft.com/pricing/ 查询当前 Azure 定价,或使用 az billing 命令
    • 记录:资源 → 当前 SKU → 预计月度成本
    • 在进入建议环节之前,计算出现实的当前月度总计
步骤 4:生成成本优化建议

操作:分析资源以识别优化机会 工具:使用已收集数据进行本地分析 流程:

  1. 根据发现的资源类型应用优化模式:

    计算优化:

    • App Service 计划:根据 CPU/内存使用情况进行适当调整
    • Function App:低使用率时从 Premium → 消耗计划
    • 虚拟机:缩减过大的实例规格

    数据库优化:

    • Cosmos DB:
      • 对于波动性工作负载,从预配 → 无服务器
      • 根据实际使用情况适当调整 RU/s
    • SQL 数据库:根据 DTU 使用情况适当调整服务层级

    存储优化:

    • 实施生命周期策略(热 → 冷 → 归档)
    • 合并冗余存储账户
    • 根据访问模式适当调整存储层级

    基础设施优化:

    • 移除未使用/冗余资源
    • 在有益处实施自动缩放
    • 对非生产环境进行调度
  2. 计算基于证据的节省金额:

    • 当前已验证成本 → 目标成本 = 节省金额
    • 记录当前配置和目标配置的定价来源
  3. 为每条建议计算优先级得分:

    Priority Score = (Value Score × Monthly Savings) / (Risk Score × Implementation Days)
    
    High Priority: Score > 20
    Medium Priority: Score 5-20
    Low Priority: Score < 5
    
  4. 验证建议:

    • 确保 Azure CLI 命令准确无误
    • 核实预计节省金额的计算
    • 评估实施风险和前提条件
    • 确保所有节省金额计算均有支持性证据
步骤 5:用户确认

操作:在创建 GitHub issue 之前展示摘要并获取批准 流程:

  1. 显示优化摘要:

    🎯 Azure Cost Optimization Summary
    
    📊 Analysis Results:
    • Total Resources Analyzed: X
    • Current Monthly Cost: $X 
    • Potential Monthly Savings: $Y 
    • Optimization Opportunities: Z
    • High Priority Items: N
    
    🏆 Recommendations:
    1. [Resource]: [Current SKU] → [Target SKU] = $X/month savings - [Risk Level] | [Implementation Effort]
    2. [Resource]: [Current Config] → [Target Config] = $Y/month savings - [Risk Level] | [Implementation Effort]
    3. [Resource]: [Current Config] → [Target Config] = $Z/month savings - [Risk Level] | [Implementation Effort]
    ... and so on
    
    💡 This will create:
    • Y individual GitHub issues (one per optimization)
    • 1 EPIC issue to coordinate implementation
    
    ❓ Proceed with creating GitHub issues? (y/n)
    
  2. 等待用户确认:仅在用户确认后继续

步骤 6:创建单独的优化 Issue

操作:为每个优化机会创建单独的 GitHub issue。为它们添加 "cost-optimization"(绿色)和 "azure"(蓝色)标签。 所需 MCP 工具:为每条建议使用 create_issue 流程:

  1. 创建单独的 Issue,使用此模板:

    标题格式:[COST-OPT] [Resource Type] - [Brief Description] - $X/month savings

    正文模板:

    ## 💰 Cost Optimization: [Brief Title]
    
    **Monthly Savings**: $X | **Risk Level**: [Low/Medium/High] | **Implementation Effort**: X days
    
    ### 📋 Description
    [Clear explanation of the optimization and why it's needed]
    
    ### 🔧 Implementation
    
    **IaC Files Detected**: [Yes/No - based on file_search results]
    
    ```bash
    # If IaC files found: Show IaC modifications + deployment
    # File: infrastructure/bicep/modules/app-service.bicep
    # Change: sku.name: 'S3' → 'B2'
    az deployment group create --resource-group [rg] --template-file infrastructure/bicep/main.bicep
    
    # If no IaC files: Direct Azure CLI commands + warning
    # ⚠️ No IaC files found. If they exist elsewhere, modify those instead.
    az appservice plan update --name [plan] --sku B2
    
    📊 证据
    • 当前配置:[详细信息]
    • 使用模式:[来自监控数据的证据]
    • 成本影响:$X/月 → $Y/月
    • 最佳实践符合度:[如适用,引用 Azure 最佳实践]
    ✅ 验证步骤
    • 在非生产环境中测试
    • 验证无性能下降
    • 在 Azure Cost Management 中确认成本降低
    • 如需要,更新监控和告警
    ⚠️ 风险与考量
    • [风险 1 及缓解措施]
    • [风险 2 及缓解措施]

    优先级评分:X | 价值:X/10 | 风险:X/10

Step 7: Create EPIC Coordinating Issue

Action: Create master issue to track all optimization work. Label it with "cost-optimization" (green color), "azure" (blue color), and "epic" (purple color). MCP Tools Required: create_issue for EPIC Note about mermaid diagrams: Ensure you verify mermaid syntax is correct and create the diagrams taking accessibility guidelines into account (styling, colors, etc.). Process:

  1. Create EPIC Issue:

    Title: [EPIC] Azure Cost Optimization Initiative - $X/month potential savings

    Body Template:

    # 🎯 Azure Cost Optimization EPIC
    
    **Total Potential Savings**: $X/month | **Implementation Timeline**: X weeks
    
    ## 📊 Executive Summary
    - **Resources Analyzed**: X
    - **Optimization Opportunities**: Y  
    - **Total Monthly Savings Potential**: $X
    - **High Priority Items**: N
    
    ## 🏗️ Current Architecture Overview
    
    ```mermaid
    graph TB
        subgraph "Resource Group: [name]"
            [Generated architecture diagram showing current resources and costs]
        end
    

    📋 实施跟踪

    🚀 高优先级(优先实施)
    • #[issue-number]:[标题] - 节省 $X/月
    • #[issue-number]:[标题] - 节省 $X/月
    ⚡ 中优先级
    • #[issue-number]:[标题] - 节省 $X/月
    • #[issue-number]:[标题] - 节省 $X/月
    🔄 低优先级(锦上添花)
    • #[issue-number]:[标题] - 节省 $X/月

    📈 进度跟踪

    • 已完成:Y 项优化中的 0 项
    • 已实现节省:$X/月中的 $0
    • 实施状态:未开始

    🎯 成功标准

    • 所有高优先级优化均已实施
    • 实现超过 80% 的预估节省
    • 未观察到性能下降
    • 成本监控仪表板已更新

    📝 备注

    • 随着 issue 完成,审查并更新此 EPIC
    • 监控实际节省与预估节省的对比
    • 考虑安排定期的成本优化审查

Error Handling

  • Cost Validation: If savings estimates lack supporting evidence or seem inconsistent with Azure pricing, re-verify configurations and pricing sources before proceeding
  • Azure Authentication Failure: Provide manual Azure CLI setup steps
  • No Resources Found: Create informational issue about Azure resource deployment
  • GitHub Creation Failure: Output formatted recommendations to console
  • Insufficient Usage Data: Note limitations and provide configuration-based recommendations only

Success Criteria

  • ✅ All cost estimates verified against actual resource configurations and Azure pricing
  • ✅ Individual issues created for each optimization (trackable and assignable)
  • ✅ EPIC issue provides comprehensive coordination and tracking
  • ✅ All recommendations include specific, executable Azure CLI commands
  • ✅ Priority scoring enables ROI-focused implementation
  • ✅ Architecture diagram accurately represents current state
  • ✅ User confirmation prevents unwanted issue creation

评论

支持 Markdown