az-cost-optimize

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Azure Cost Optimize

Azure 成本优化

This workflow analyzes Infrastructure-as-Code (IaC) files and Azure resources to generate cost optimization recommendations. It creates individual GitHub issues for each optimization opportunity plus one EPIC issue to coordinate implementation, enabling efficient tracking and execution of cost savings initiatives.
本工作流会分析基础设施即代码(IaC)文件和Azure资源,生成成本优化建议。它会为每个优化机会创建独立的GitHub Issue,同时生成一个EPIC Issue来协调实施,从而高效跟踪和执行成本节约举措。

Prerequisites

前置条件

  • Azure MCP server configured and authenticated
  • GitHub MCP server configured and authenticated
  • Target GitHub repository identified
  • Azure resources deployed (IaC files optional but helpful)
  • Prefer Azure MCP tools (
    azmcp-*
    ) over direct Azure CLI when available
  • 已配置并完成身份验证的Azure MCP服务器
  • 已配置并完成身份验证的GitHub MCP服务器
  • 已确定目标GitHub仓库
  • 已部署Azure资源(IaC文件为可选,但有助于分析)
  • 优先使用Azure MCP工具(
    azmcp-*
    ),而非直接使用Azure CLI(如有可用)

Workflow Steps

工作流步骤

Step 1: Get Azure Best Practices

步骤1:获取Azure最佳实践

Action: Retrieve cost optimization best practices before analysis Tools: Azure MCP best practices tool Process:
  1. Load Best Practices:
    • Execute
      azmcp-bestpractices-get
      to get some of the latest Azure optimization guidelines. This may not cover all scenarios but provides a foundation.
    • Use these practices to inform subsequent analysis and recommendations as much as possible
    • Reference best practices in optimization recommendations, either from the MCP tool output or general Azure documentation
操作:在分析前获取成本优化最佳实践 工具:Azure MCP最佳实践工具 流程:
  1. 加载最佳实践:
    • 执行
      azmcp-bestpractices-get
      获取最新的Azure优化指南。这可能无法覆盖所有场景,但可提供分析基础。
    • 尽可能利用这些实践指导后续分析和建议
    • 在优化建议中引用最佳实践,可来自MCP工具输出或通用Azure文档

Step 2: Discover Azure Infrastructure

步骤2:发现Azure基础设施

Action: Dynamically discover and analyze Azure resources and configurations Tools: Azure MCP tools + Azure CLI fallback + Local file system access Process:
  1. Resource Discovery:
    • Execute
      azmcp-subscription-list
      to find available subscriptions
    • Execute
      azmcp-group-list --subscription <subscription-id>
      to find resource groups
    • Get a list of all resources in the relevant group(s):
      • Use
        az resource list --subscription <id> --resource-group <name>
    • For each resource type, use MCP tools first if possible, then CLI fallback:
      • azmcp-cosmos-account-list --subscription <id>
        - Cosmos DB accounts
      • azmcp-storage-account-list --subscription <id>
        - Storage accounts
      • azmcp-monitor-workspace-list --subscription <id>
        - Log Analytics workspaces
      • azmcp-keyvault-key-list
        - Key Vaults
      • az webapp list
        - Web Apps (fallback - no MCP tool available)
      • az appservice plan list
        - App Service Plans (fallback)
      • az functionapp list
        - Function Apps (fallback)
      • az sql server list
        - SQL Servers (fallback)
      • az redis list
        - Redis Cache (fallback)
      • ... and so on for other resource types
  2. IaC Detection:
    • Use
      file_search
      to scan for IaC files: "/*.bicep", "/*.tf", "/main.json", "/template.json"
    • Parse resource definitions to understand intended configurations
    • Compare against discovered resources to identify discrepancies
    • Note presence of IaC files for implementation recommendations later on
    • Do NOT use any other file from the repository, only IaC files. Using other files is NOT allowed as it is not a source of truth.
    • If you do not find IaC files, then STOP and report no IaC files found to the user.
  3. Configuration Analysis:
    • Extract current SKUs, tiers, and settings for each resource
    • Identify resource relationships and dependencies
    • Map resource utilization patterns where available
操作:动态发现并分析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
        - 密钥保管库
      • az webapp list
        - Web应用(备选方案 - 无可用MCP工具)
      • az appservice plan list
        - 应用服务计划(备选方案)
      • az functionapp list
        - 函数应用(备选方案)
      • az sql server list
        - SQL服务器(备选方案)
      • az redis list
        - Redis缓存(备选方案)
      • ...其他资源类型以此类推
  2. IaC检测:
    • 使用
      file_search
      扫描IaC文件:"/*.bicep", "/*.tf", "/main.json", "/template.json"
    • 解析资源定义以了解预期配置
    • 与发现的资源进行对比,识别差异
    • 记录IaC文件的存在情况,以便后续提供实施建议
    • 请勿使用仓库中的其他文件,仅使用IaC文件。使用其他文件是不被允许的,因为它们不是可信来源。
    • 若未找到IaC文件,请停止操作并向用户报告未发现IaC文件。
  3. 配置分析:
    • 提取每个资源的当前SKU、层级和设置
    • 识别资源关系和依赖项
    • 在可用情况下映射资源使用模式

Step 3: Collect Usage Metrics & Validate Current Costs

步骤3:收集使用指标并验证当前成本

Action: Gather utilization data AND verify actual resource costs Tools: Azure MCP monitoring tools + Azure CLI Process:
  1. Find Monitoring Sources:
    • Use
      azmcp-monitor-workspace-list --subscription <id>
      to find Log Analytics workspaces
    • Use
      azmcp-monitor-table-list --subscription <id> --workspace <name> --table-type "CustomLog"
      to discover available data
  2. Execute Usage Queries:
    • Use
      azmcp-monitor-log-query
      with these predefined queries:
      • Query: "recent" for recent activity patterns
      • Query: "errors" for error-level logs indicating issues
    • For custom analysis, use KQL queries:
    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. Calculate Baseline Metrics:
    • CPU/Memory utilization averages
    • Database throughput patterns
    • Storage access frequency
    • Function execution rates
  4. VALIDATE CURRENT COSTS:
    • Using the SKU/tier configurations discovered in Step 2
    • Look up current Azure pricing at https://azure.microsoft.com/pricing/ or use
      az billing
      commands
    • Document: Resource → Current SKU → Estimated monthly cost
    • Calculate realistic current monthly total before proceeding to recommendations
操作:收集使用数据并验证实际资源成本 工具: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查询:
    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 → 预估月度成本
    • 在生成建议前,计算真实的当前月度总成本

Step 4: Generate Cost Optimization Recommendations

步骤4:生成成本优化建议

Action: Analyze resources to identify optimization opportunities Tools: Local analysis using collected data Process:
  1. Apply Optimization Patterns based on resource types found:
    Compute Optimizations:
    • App Service Plans: Right-size based on CPU/memory usage
    • Function Apps: Premium → Consumption plan for low usage
    • Virtual Machines: Scale down oversized instances
    Database Optimizations:
    • Cosmos DB:
      • Provisioned → Serverless for variable workloads
      • Right-size RU/s based on actual usage
    • SQL Database: Right-size service tiers based on DTU usage
    Storage Optimizations:
    • Implement lifecycle policies (Hot → Cool → Archive)
    • Consolidate redundant storage accounts
    • Right-size storage tiers based on access patterns
    Infrastructure Optimizations:
    • Remove unused/redundant resources
    • Implement auto-scaling where beneficial
    • Schedule non-production environments
  2. Calculate Evidence-Based Savings:
    • Current validated cost → Target cost = Savings
    • Document pricing source for both current and target configurations
  3. Calculate Priority Score for each recommendation:
    Priority Score = (Value Score × Monthly Savings) / (Risk Score × Implementation Days)
    
    High Priority: Score > 20
    Medium Priority: Score 5-20
    Low Priority: Score < 5
  4. Validate Recommendations:
    • Ensure Azure CLI commands are accurate
    • Verify estimated savings calculations
    • Assess implementation risks and prerequisites
    • Ensure all savings calculations have supporting evidence
操作:分析资源以识别优化机会 工具:使用收集的数据进行本地分析 流程:
  1. 基于发现的资源类型应用优化模式:
    计算资源优化:
    • 应用服务计划:基于CPU/内存使用情况调整规格
    • 函数应用:低使用率场景下从Premium计划切换到Consumption计划
    • 虚拟机:缩小过大实例的规格
    数据库优化:
    • Cosmos DB:
      • 可变工作负载场景下从预配模式切换到无服务器模式
      • 根据实际使用情况调整RU/s规格
    • SQL数据库:基于DTU使用情况调整服务层级
    存储优化:
    • 实施生命周期策略(热存储 → 冷存储 → 归档存储)
    • 合并冗余存储账户
    • 根据访问模式调整存储层级
    基础设施优化:
    • 删除未使用/冗余资源
    • 在有益场景下实现自动缩放
    • 为非生产环境设置调度启停
  2. 计算基于证据的节约金额:
    • 当前验证成本 → 目标成本 = 节约金额
    • 记录当前和目标配置的定价来源
  3. 为每个建议计算优先级分数:
    优先级分数 = (价值分数 × 月度节约金额) / (风险分数 × 实施天数)
    
    高优先级:分数 > 20
    中优先级:分数 5-20
    低优先级:分数 < 5
  4. 验证建议:
    • 确保Azure CLI命令准确
    • 验证预估节约金额的计算
    • 评估实施风险和前置条件
    • 确保所有节约金额计算都有支持证据

Step 5: User Confirmation

步骤5:用户确认

Action: Present summary and get approval before creating GitHub issues Process:
  1. Display Optimization Summary:
    🎯 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. Wait for User Confirmation: Only proceed if user confirms
操作:展示摘要并在创建GitHub Issue前获得批准 流程:
  1. 展示优化摘要:
    🎯 Azure 成本优化摘要
    
    📊 分析结果:
    • 分析的资源总数: X
    • 当前月度成本: $X 
    • 潜在月度节约金额: $Y 
    • 优化机会数量: Z
    • 高优先级项数量: N
    
    🏆 建议:
    1. [资源]: [当前SKU] → [目标SKU] = 每月节约$X - [风险等级] | [实施工作量]
    2. [资源]: [当前配置] → [目标配置] = 每月节约$Y - [风险等级] | [实施工作量]
    3. [资源]: [当前配置] → [目标配置] = 每月节约$Z - [风险等级] | [实施工作量]
    ...以此类推
    
    💡 这将创建:
    • Y个独立的GitHub Issue(每个优化项对应一个)
    • 1个用于协调实施的EPIC Issue
    
    ❓ 是否继续创建GitHub Issue?(y/n)
  2. 等待用户确认:仅在用户确认后继续操作

Step 6: Create Individual Optimization Issues

步骤6:创建独立优化Issue

Action: Create separate GitHub issues for each optimization opportunity. Label them with "cost-optimization" (green color), "azure" (blue color). MCP Tools Required:
create_issue
for each recommendation Process:
  1. Create Individual Issues using this template:
    Title Format:
    [COST-OPT] [Resource Type] - [Brief Description] - $X/month savings
    Body Template:
    markdown
    ## 💰 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

    📊 Evidence

    • Current Configuration: [details]
    • Usage Pattern: [evidence from monitoring data]
    • Cost Impact: $X/month → $Y/month
    • Best Practice Alignment: [reference to Azure best practices if applicable]

    ✅ Validation Steps

    • Test in non-production environment
    • Verify no performance degradation
    • Confirm cost reduction in Azure Cost Management
    • Update monitoring and alerts if needed

    ⚠️ Risks & Considerations

    • [Risk 1 and mitigation]
    • [Risk 2 and mitigation]
    Priority Score: X | Value: X/10 | Risk: X/10
    undefined
操作:为每个优化机会创建单独的GitHub Issue。为其添加标签"cost-optimization"(绿色)、"azure"(蓝色)。 所需MCP工具: 为每个建议使用
create_issue
流程:
  1. 使用以下模板创建独立Issue:
    标题格式:
    [COST-OPT] [资源类型] - [简要描述] - 每月节约$X
    正文模板:
    markdown
    ## 💰 成本优化: [简要标题]
    
    **月度节约金额**: $X | **风险等级**: [低/中/高] | **实施工作量**: X天
    
    ### 📋 描述
    [清晰说明优化内容及必要性]
    
    ### 🔧 实施步骤
    
    **检测到IaC文件**: [是/否 - 基于file_search结果]
    
    ```bash
    # 若检测到IaC文件:展示IaC修改内容及部署命令
    # 文件: infrastructure/bicep/modules/app-service.bicep
    # 修改: sku.name: 'S3' → 'B2'
    az deployment group create --resource-group [rg] --template-file infrastructure/bicep/main.bicep
    
    # 若无IaC文件:直接使用Azure CLI命令并给出警告
    # ⚠️ 未检测到IaC文件。若存在IaC文件,请修改对应文件。
    az appservice plan update --name [plan] --sku B2

    📊 支持证据

    • 当前配置: [详情]
    • 使用模式: [来自监控数据的证据]
    • 成本影响: 每月$X → 每月$Y
    • 最佳实践对齐: [若适用,引用Azure最佳实践]

    ✅ 验证步骤

    • 在非生产环境中测试
    • 验证无性能下降
    • 在Azure成本管理中确认成本降低
    • 按需更新监控和告警

    ⚠️ 风险与注意事项

    • [风险1及缓解措施]
    • [风险2及缓解措施]
    优先级分数: X | 价值: X/10 | 风险: X/10
    undefined

Step 7: Create EPIC Coordinating Issue

步骤7:创建协调用EPIC 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:
    markdown
    # 🎯 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

    📋 Implementation Tracking

    🚀 High Priority (Implement First)

    • #[issue-number]: [Title] - $X/month savings
    • #[issue-number]: [Title] - $X/month savings

    ⚡ Medium Priority

    • #[issue-number]: [Title] - $X/month savings
    • #[issue-number]: [Title] - $X/month savings

    🔄 Low Priority (Nice to Have)

    • #[issue-number]: [Title] - $X/month savings

    📈 Progress Tracking

    • Completed: 0 of Y optimizations
    • Savings Realized: $0 of $X/month
    • Implementation Status: Not Started

    🎯 Success Criteria

    • All high-priority optimizations implemented
    • >80% of estimated savings realized
    • No performance degradation observed
    • Cost monitoring dashboard updated

    📝 Notes

    • Review and update this EPIC as issues are completed
    • Monitor actual vs. estimated savings
    • Consider scheduling regular cost optimization reviews
    undefined
操作:创建主Issue以跟踪所有优化工作。为其添加标签"cost-optimization"(绿色)、"azure"(蓝色)和"epic"(紫色)。 所需MCP工具: 为EPIC使用
create_issue
关于mermaid图的说明: 确保验证mermaid语法正确,并创建符合无障碍指南的图表(样式、颜色等)。 流程:
  1. 创建EPIC Issue:
    标题:
    [EPIC] Azure 成本优化计划 - 每月潜在节约$X
    正文模板:
    markdown
    # 🎯 Azure 成本优化EPIC
    
    **总潜在节约金额**: 每月$X | **实施时间线**: X周
    
    ## 📊 执行摘要
    - **分析的资源数量**: X
    - **优化机会数量**: Y  
    - **月度总潜在节约金额**: $X
    - **高优先级项数量**: N
    
    ## 🏗️ 当前架构概述
    
    ```mermaid
    graph TB
        subgraph "资源组: [名称]"
            [生成的架构图,展示当前资源和成本]
        end

    📋 实施跟踪

    🚀 高优先级(优先实施)

    • #[issue编号]: [标题] - 每月节约$X
    • #[issue编号]: [标题] - 每月节约$X

    ⚡ 中优先级

    • #[issue编号]: [标题] - 每月节约$X
    • #[issue编号]: [标题] - 每月节约$X

    🔄 低优先级(可选)

    • #[issue编号]: [标题] - 每月节约$X

    📈 进度跟踪

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

    🎯 成功标准

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

    📝 备注

    • 随着Issue完成,定期审阅和更新本EPIC
    • 监控实际节约金额与预估金额的差异
    • 考虑定期安排成本优化审阅
    undefined

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
  • 成本验证: 若节约金额预估缺乏支持证据或与Azure定价不一致,在继续前重新验证配置和定价来源
  • Azure身份验证失败: 提供手动Azure CLI设置步骤
  • 未找到资源: 创建关于Azure资源部署的信息性Issue
  • GitHub Issue创建失败: 将格式化的建议输出到控制台
  • 使用数据不足: 记录限制条件,仅提供基于配置的建议

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
  • ✅ 所有成本预估均已根据实际资源配置和Azure定价验证
  • ✅ 为每个优化项创建了独立Issue(可跟踪、可分配)
  • ✅ EPIC Issue提供了全面的协调和跟踪能力
  • ✅ 所有建议均包含具体、可执行的Azure CLI命令
  • ✅ 优先级评分支持以ROI为导向的实施
  • ✅ 架构图准确反映当前状态
  • ✅ 用户确认机制可防止创建不必要的Issue