How to Use Terraform MCP with Claude Code
Plan, apply, and inspect Terraform infrastructure from Claude Code — generate modules, review state, and manage cloud resources with AI-powered infrastructure-as-code assistance.
What is the Terraform MCP?
The Terraform MCP integrates Claude Code with the Terraform CLI and Terraform Cloud/HCP Terraform APIs. Claude can generate HCL modules, run terraform plan, inspect state files, validate configurations, and manage workspaces — making infrastructure-as-code workflows dramatically faster and less error-prone.
Installation
mcpizy install terraform
The MCP requires Terraform CLI to be installed on your machine. For Terraform Cloud integration, provide a TFC organization and API token during setup.
Key Capabilities
- Generate Terraform modules — describe infrastructure in plain English; Claude writes the HCL.
- Run terraform plan — execute plans and parse the output to summarize what will change.
- Inspect state — list resources in state and show their current attributes.
- Validate configurations — run
terraform validateand explain any errors. - Manage workspaces — create and switch between Terraform Cloud workspaces.
Example Usage
Provision a new S3 bucket with versioning and a lifecycle rule:
resource "aws_s3_bucket" "artifacts" {
bucket = "my-app-artifacts-prod"
}
resource "aws_s3_bucket_versioning" "artifacts" {
bucket = aws_s3_bucket.artifacts.id
versioning_configuration { status = "Enabled" }
}
resource "aws_s3_bucket_lifecycle_configuration" "artifacts" {
bucket = aws_s3_bucket.artifacts.id
rule {
id = "expire-old"
status = "Enabled"
expiration { days = 90 }
}
}
Claude generates this HCL, runs the plan, and reports back the expected changes.
Tips and Best Practices
- Always review the plan output before asking Claude to apply — never auto-apply in production.
- Store sensitive variables in Terraform Cloud variable sets rather than in
.tfvarsfiles. - Combine with Kubernetes MCP to manage both cluster infrastructure and application workloads from one session.
- Ask Claude to add resource tagging to all AWS/GCP/Azure resources for cost attribution.