infracost_diff
Compare Terraform configurations to identify cost changes between baseline and current infrastructure, helping track cloud spending differences.
Instructions
Show cost differences between two Terraform configurations. Compares baseline and current infrastructure to identify cost changes. Requires infracost CLI to be installed.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to current Terraform directory or plan JSON file | |
| compareTo | Yes | Path to baseline Terraform directory or plan JSON file | |
| format | No | Output format (default: diff) | |
| outFile | No | Save output to a file |
Implementation Reference
- src/tools.ts:310-332 (handler)Main handler function for the infracost_diff tool. Validates arguments using DiffSchema, checks if Infracost CLI is installed, executes the diff via executeDiff, and returns the output as MCP content.async handleDiff(args: z.infer<typeof DiffSchema>) { 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 executeDiff(args); if (!result.success) { throw new Error(result.error || 'Diff command failed'); } return { content: [ { type: 'text', text: result.output || 'Diff completed successfully', }, ], }; }
- src/tools.ts:21-26 (schema)Zod schema defining the input parameters and validation for the infracost_diff tool.export const DiffSchema = z.object({ path: z.string().describe('Path to Terraform directory or plan JSON file'), compareTo: z.string().describe('Path to baseline Terraform directory or plan JSON file'), format: z.enum(['json', 'diff']).optional().describe('Output format'), outFile: z.string().optional().describe('Save output to file'), });
- src/index.ts:106-133 (registration)Registration of the infracost_diff tool in the ListTools response, including name, description, and input schema definition.{ name: 'infracost_diff', description: 'Show cost differences between two Terraform configurations. Compares baseline and current infrastructure to identify cost changes. Requires infracost CLI to be installed.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to current Terraform directory or plan JSON file', }, compareTo: { type: 'string', description: 'Path to baseline Terraform directory or plan JSON file', }, format: { type: 'string', enum: ['json', 'diff'], description: 'Output format (default: diff)', }, outFile: { type: 'string', description: 'Save output to a file', }, }, required: ['path', 'compareTo'], }, },
- src/index.ts:709-712 (handler)Dispatch handler in CallToolRequestSchema that parses arguments with DiffSchema and delegates to tools.handleDiff.case 'infracost_diff': { const validatedArgs = DiffSchema.parse(args); return await tools.handleDiff(validatedArgs); }
- src/cli.ts:75-110 (helper)Helper function that executes the 'infracost diff' CLI command with resolved paths and options, captures output and parses JSON if applicable.export async function executeDiff(options: DiffOptions): Promise<CommandResult> { try { const args = ['diff', '--path', resolve(options.path), '--compare-to', resolve(options.compareTo)]; if (options.format) { args.push('--format', options.format); } if (options.outFile) { args.push('--out-file', resolve(options.outFile)); } 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) {} } 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', }; } }