AIDevOps·

Supercharge Terraform with MCP Servers

Connect your AI assistant to the official Terraform MCP server and the AWS Knowledge MCP server to generate accurate, up-to-date HCL instead of hallucinated resources.

AI assistants are great at writing Terraform, until they invent a resource argument that was removed three provider versions ago. The root cause is always the same: the model's training data is frozen in time while providers ship weekly. MCP (Model Context Protocol) fixes exactly that by letting your assistant query live sources instead of guessing. In this tutorial, I'll show you how to wire up the two MCP servers that make the biggest difference when programming Terraform.

The Terraform MCP server

HashiCorp ships an official Terraform MCP server that connects your assistant directly to the Terraform Registry. Out of the box, it exposes three families of registry tools:

  • Provider tools: search any provider's documentation and fetch the full docs for a resource, data source, or function, always at the latest version.
  • Module tools: discover community and verified modules, then pull their complete inputs, outputs, and examples before deciding to use one.
  • Policy tools: browse Sentinel policies for governance and compliance requirements.

The server runs as a Docker container using the default stdio transport, so nothing is exposed on your network.

Docker must be installed and running. If you can't use Docker, HashiCorp also distributes the server as a binary via go install github.com/hashicorp/terraform-mcp-server/cmd/terraform-mcp-server@latest.

Here's how to register it in the most common assistants:

claude mcp add terraform -s user -t stdio -- docker run -i --rm hashicorp/terraform-mcp-server
Kiro reads this from .kiro/settings/mcp.json (per workspace) or ~/.kiro/settings/mcp.json (global), Cursor from .cursor/mcp.json, and VS Code from .vscode/mcp.json.

From there, your assistant stops relying on stale training data from your LLM. Ask it to write an S3 bucket with versioning and it will pull the current aws_s3_bucket documentation first.

With the configurations above, your assistant starts a fresh container on demand and talks to it over stdio. You can also run the server yourself in HTTP mode, which is handy to check that it works, or to keep a single instance running for several tools.

Connect it to HCP Terraform

The registry tools work without any credentials. But if you use HCP Terraform or Terraform Enterprise, the same server can also manage your organization: listing workspaces and runs, reading plan and apply logs, browsing state versions, managing variables and variable sets, or searching your private registry. Provide a token and your assistant becomes an operator, not just a documentation reader:

{
  "mcpServers": {
    "terraform": {
      "command": "docker",
      "args": [
        "run", "-i", "--rm",
        "-e", "TFE_ADDRESS=https://app.terraform.io",
        "-e", "TFE_TOKEN=<YOUR_TOKEN>",
        "hashicorp/terraform-mcp-server"
      ]
    }
  }
}
Destructive operations (applying runs, deleting workspaces) are disabled by default and require explicitly setting ENABLE_TF_OPERATIONS=true. Leave it off unless you really want your assistant pressing the apply button, and scope the TFE_TOKEN permissions to the minimum needed.

The AWS Knowledge MCP server

Provider docs tell your assistant how to write a resource, but not always which AWS architecture makes more sense. If your Terraform targets AWS, pairing the Terraform server with the AWS Knowledge MCP server is a nice combo: it's a fully managed remote server hosted by AWS that exposes the AWS documentation, API references, architectural guidance, Well-Architected best practices, and even the regional availability of each service and API. No container, no credentials, not even an AWS account, just an URL:

claude mcp add --transport http aws-knowledge https://knowledge-mcp.global.api.aws

With both servers connected, the division of labor is neat: the AWS Knowledge server helps design the architecture and confirms a service is available in your target region, while the Terraform server makes sure the generated code matches the current provider schema.

AWS used to maintain a dedicated awslabs.terraform-mcp-server with Checkov integration, but it's now deprecated in favor of HashiCorp's official server and no longer receives updates. If you still have it configured, it's time to migrate.

Put them to work

Once configured, your assitant trigger automatically when your questions touch their domain. A few prompts that show the difference:

  • "Write an aws_s3_bucket with versioning and lifecycle rules using the current provider syntax", pulls the latest provider docs instead of recalling deprecated arguments.
  • "Is there a verified module for an EKS cluster? Compare its inputs with writing the resources myself", fetches real module documentation with download counts and verification status.
  • "Review this VPC configuration against AWS best practices", combines Well-Architected guidance with the current provider schema.
  • "Which of my workspaces failed their last run, and why?", reads run logs straight from HCP Terraform (token required).

One last tip to close the loop: these servers make your assistant accurate, but they don't make terraform apply safe. Keep the actual deployment in a controlled pipeline with plan review and least-privilege roles, exactly like the workflow I describe in Deploy Your Terraform IaC with GitHub Actions. Let AI write the Terraform code, and let your CI/CD ship it.