infracost_breakdown
Analyze Terraform infrastructure configurations to generate detailed cost estimates and breakdowns for cloud resources.
Instructions
Generate a cost breakdown for Terraform infrastructure. Analyzes Terraform configuration and provides detailed cost estimates for resources. Requires infracost CLI to be installed.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to Terraform directory or plan JSON file | |
| format | No | Output format (default: table) | |
| outFile | No | Save output to a file | |
| terraformVarFile | No | Terraform variable file paths | |
| terraformVar | No | Terraform variables as key-value pairs |
Implementation Reference
- src/cli.ts:24-73 (handler)Core implementation of the infracost_breakdown tool: executes the infracost CLI 'breakdown' command with resolved paths and options, captures output, parses JSON if applicable, and returns result.export async function executeBreakdown(options: BreakdownOptions): Promise<CommandResult> { try { const args = ['breakdown', '--path', resolve(options.path)]; if (options.format) { args.push('--format', options.format); } if (options.outFile) { args.push('--out-file', resolve(options.outFile)); } if (options.terraformVarFile) { options.terraformVarFile.forEach((file) => { args.push('--terraform-var-file', resolve(file)); }); } if (options.terraformVar) { Object.entries(options.terraformVar).forEach(([key, value]) => { args.push('--terraform-var', `${key}=${value}`); }); } const { stdout, stderr } = await execFileAsync('infracost', args, { maxBuffer: 10 * 1024 * 1024, }); let parsedData; if (options.format === 'json' && !options.outFile && stdout.trim()) { try { parsedData = JSON.parse(stdout); } catch (e) { // If parsing fails, leave data undefined } } return { success: true, output: options.outFile ? `Output saved to ${options.outFile}` : stdout, error: stderr || undefined, data: parsedData, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error occurred', }; } }
- src/tools.ts:13-19 (schema)Zod schema defining the input parameters for the infracost_breakdown tool, used for validation.export const BreakdownSchema = z.object({ path: z.string().describe('Path to Terraform directory or plan JSON file'), format: z.enum(['json', 'table', 'html']).optional().describe('Output format'), outFile: z.string().optional().describe('Save output to file'), terraformVarFile: z.array(z.string()).optional().describe('Terraform var file paths'), terraformVar: z.record(z.string()).optional().describe('Terraform variables as key-value pairs'), });
- src/tools.ts:286-308 (handler)Handler method in InfracostTools class: checks CLI installation, calls executeBreakdown, and formats MCP response.async handleBreakdown(args: z.infer<typeof BreakdownSchema>) { const isInstalled = await checkInfracostInstalled(); if (!isInstalled) { throw new Error( 'Infracost CLI is not installed. Please install it from https://www.infracost.io/docs/' ); } const result = await executeBreakdown(args); if (!result.success) { throw new Error(result.error || 'Breakdown command failed'); } return { content: [ { type: 'text', text: result.output || 'Breakdown completed successfully', }, ], }; }
- src/index.ts:73-105 (registration)Tool registration in ListToolsRequestSchema handler: defines name, description, and input schema for discovery.{ name: 'infracost_breakdown', description: 'Generate a cost breakdown for Terraform infrastructure. Analyzes Terraform configuration and provides detailed cost estimates for resources. Requires infracost CLI to be installed.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to Terraform directory or plan JSON file', }, format: { type: 'string', enum: ['json', 'table', 'html'], description: 'Output format (default: table)', }, outFile: { type: 'string', description: 'Save output to a file', }, terraformVarFile: { type: 'array', items: { type: 'string' }, description: 'Terraform variable file paths', }, terraformVar: { type: 'object', description: 'Terraform variables as key-value pairs', }, }, required: ['path'], }, },
- src/index.ts:704-707 (registration)Dispatch/registration in CallToolRequestSchema switch: parses args with schema and delegates to tool handler.case 'infracost_breakdown': { const validatedArgs = BreakdownSchema.parse(args); return await tools.handleBreakdown(validatedArgs); }