Skip to main content
Glama

get_cost_by_service

Retrieve AWS cost breakdowns by service for specific date ranges to analyze spending patterns and identify cost drivers.

Instructions

Retrieves AWS costs broken down by service for the specified date range.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
start_dateNoStart date in YYYY-MM-DD format.
end_dateNoEnd date in YYYY-MM-DD format.

Implementation Reference

  • Handler implementation for the 'get_cost_by_service' tool. It fetches AWS costs broken down by service using CostExplorerClient's GetCostAndUsageCommand, grouped by SERVICE dimension, for a specified date range (default last 7 days). Returns JSON with date, service, cost, and unit.
    if (name === "get_cost_by_service") { const endDate = (args as any)?.end_date || new Date().toISOString().split('T')[0]; const startDate = (args as any)?.start_date || new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]; const command = new GetCostAndUsageCommand({ TimePeriod: { Start: startDate, End: endDate }, Granularity: "DAILY", Metrics: ["UnblendedCost"], GroupBy: [{ Type: "DIMENSION", Key: "SERVICE" }] }); const response = await costExplorerClient.send(command); const costs = response.ResultsByTime?.flatMap(r => r.Groups?.map(g => ({ Date: r.TimePeriod?.Start, Service: g.Keys?.[0], Cost: g.Metrics?.UnblendedCost?.Amount, Unit: g.Metrics?.UnblendedCost?.Unit })) ) || []; return { content: [{ type: "text", text: JSON.stringify(costs, null, 2) }] }; }
  • src/index.ts:192-207 (registration)
    Tool registration in ListTools response, including name, description, and input schema definition.
    name: "get_cost_by_service", description: "Retrieves AWS costs broken down by service for the specified date range.", inputSchema: { type: "object", properties: { start_date: { type: "string", description: "Start date in YYYY-MM-DD format." }, end_date: { type: "string", description: "End date in YYYY-MM-DD format." } } } },
  • Input schema for the 'get_cost_by_service' tool, defining optional start_date and end_date parameters.
    inputSchema: { type: "object", properties: { start_date: { type: "string", description: "Start date in YYYY-MM-DD format." }, end_date: { type: "string", description: "End date in YYYY-MM-DD format." } } }
  • Initialization of the CostExplorerClient used by the get_cost_by_service handler.
    const costExplorerClient = new CostExplorerClient({});
  • Import of CostExplorerClient and relevant commands used in the tool.
    import { CostExplorerClient, GetCostAndUsageCommand, GetCostForecastCommand, GetAnomaliesCommand, GetSavingsPlansUtilizationCommand, GetReservationUtilizationCommand } from "@aws-sdk/client-cost-explorer";

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Bhavesh8890/MCP-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server