infracost_breakdown
Analyze Terraform infrastructure configurations to generate detailed cost estimates for cloud resources, helping teams understand and optimize cloud spending.
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/tools.ts:286-308 (handler)MCP tool handler method in InfracostTools class that validates input with BreakdownSchema, checks if Infracost CLI is installed, executes the breakdown via CLI wrapper, and returns the output as MCP content.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/cli.ts:24-73 (helper)Core helper function that constructs and executes the 'infracost breakdown' CLI command using child_process.execFile, handles options like format, outFile, terraform vars, parses JSON output if applicable, and returns structured 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 for input validation of infracost_breakdown tool parameters.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/index.ts:704-707 (registration)Dispatch case in CallToolRequestSchema handler that parses args with BreakdownSchema and calls the tool handler.case 'infracost_breakdown': { const validatedArgs = BreakdownSchema.parse(args); return await tools.handleBreakdown(validatedArgs); }
- src/index.ts:73-105 (registration)Tool registration in ListToolsRequestSchema response, defining name, description, and inputSchema.{ 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'], }, },