infracost_diff
Compare Terraform configurations to identify cost changes between baseline and current infrastructure, helping teams manage cloud spending effectively.
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 for 'infracost_diff' tool. Validates args, checks Infracost CLI installation, executes the diff command 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/cli.ts:75-110 (helper)Core helper function that executes the 'infracost diff' CLI command with resolved paths and options, captures stdout/stderr/JSON, and returns structured result.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', }; } }
- src/tools.ts:21-26 (schema)Zod schema defining the input parameters for the infracost_diff tool, used for validation.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)Tool registration in ListTools handler, including name, description, and input schema (mirroring DiffSchema).{ 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 case in main CallToolRequestHandler that parses args with DiffSchema and delegates to InfracostTools.handleDiff.case 'infracost_diff': { const validatedArgs = DiffSchema.parse(args); return await tools.handleDiff(validatedArgs); }