Skip to main content
Glama

terraform_validate

Validate Terraform configuration and check formatting to ensure infrastructure code follows best practices before deployment.

Instructions

Validate Terraform configuration and check formatting

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
directoryYesDirectory containing Terraform files

Implementation Reference

  • The implementation of the terraform_validate tool, which executes `terraform validate` and `terraform fmt -check` commands.
    export async function terraformValidate(args: Record<string, unknown>): Promise<string> {
      const directory = args.directory as string;
      if (!directory) throw new Error("Terraform directory is required");
    
      const lines: string[] = [];
    
      // Validate
      try {
        const { stdout } = await execFileAsync("terraform", ["validate", "-json"], {
          cwd: directory,
          timeout: 30000,
        });
        const result = JSON.parse(stdout);
        if (result.valid) {
          lines.push("Validation: PASSED");
        } else {
          lines.push("Validation: FAILED");
          for (const diag of result.diagnostics || []) {
            lines.push(`  ${diag.severity}: ${diag.summary}`);
            if (diag.detail) lines.push(`    ${diag.detail}`);
          }
        }
      } catch (error: any) {
        lines.push(`Validation error: ${error.stderr || error.message}`);
      }
    
      // Format check
      try {
        const { stdout } = await execFileAsync("terraform", ["fmt", "-check", "-diff"], {
          cwd: directory,
          timeout: 30000,
        });
        if (stdout.trim()) {
          lines.push("", "Formatting issues:", stdout);
        } else {
          lines.push("", "Formatting: OK");
        }
      } catch (error: any) {
        if (error.stdout?.trim()) {
          lines.push("", "Formatting issues:", error.stdout);
        } else {
          lines.push("", `Format check error: ${error.message}`);
        }
      }
    
      return lines.join("\n");
    }
  • Registration of the `terraform_validate` tool including its name, description, and input schema.
    {
      name: "terraform_validate",
      description: "Validate Terraform configuration and check formatting",
      inputSchema: {
        type: "object" as const,
        properties: {
          directory: { type: "string", description: "Directory containing Terraform files" },
        },
        required: ["directory"],
      },
    },
  • The dispatch logic within `handleTerraformTool` that routes calls to the `terraform_validate` tool to its handler function.
    case "terraform_validate": return terraformValidate(a);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/batu-sonmez/infraclaude'

If you have feedback or need assistance with the MCP directory API, please join our Discord server