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
| Name | Required | Description | Default |
|---|---|---|---|
| start_date | No | Start date in YYYY-MM-DD format. | |
| end_date | No | End date in YYYY-MM-DD format. |
Implementation Reference
- src/index.ts:981-1005 (handler)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." } } } },
- src/index.ts:194-206 (schema)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." } } }
- src/index.ts:58-58 (helper)Initialization of the CostExplorerClient used by the get_cost_by_service handler.const costExplorerClient = new CostExplorerClient({});
- src/index.ts:24-24 (helper)Import of CostExplorerClient and relevant commands used in the tool.import { CostExplorerClient, GetCostAndUsageCommand, GetCostForecastCommand, GetAnomaliesCommand, GetSavingsPlansUtilizationCommand, GetReservationUtilizationCommand } from "@aws-sdk/client-cost-explorer";