Skip to content
CLI tool · Cloud & deploy

Terraform for AI agents

Terraform for AI agents: format, validate, and plan infrastructure changes and read state, while apply and destroy stay with a human reviewer.

terraformby HashiCorpOfficial MCP option
What it is

What Terraform does for an agent

Terraform is HashiCorp's infrastructure-as-code tool. It reads configuration files, compares them with recorded state and the real infrastructure, and produces a plan of what it would create, change, or destroy.

That plan step is what makes Terraform agent-friendly: an agent can write configuration and produce a saved plan, and a human reviews and applies it.

Official site: www.terraform.io

Install

Install Terraform

The install commands from the official docs. Check it worked with terraform version.

Homebrew (HashiCorp tap)
brew tap hashicorp/tap && brew install hashicorp/tap/terraform
Auth

Signing in Terraform for an agent

Terraform uses each provider's own credentials, for example the AWS, Google Cloud, or Azure environment variables and profiles. terraform login stores a token for HCP Terraform.

Sign in to HCP Terraform (human)
terraform login
Common agent tasks

What agents use Terraform for

Example commands with placeholder names. Swap in your own project, app, or file names.

  1. Check formatting

    terraform
    terraform fmt -check -recursive
  2. Validate configuration

    terraform
    terraform validate

    Run terraform init first in a fresh checkout.

  3. Save a plan for review

    terraform
    terraform plan -out=tfplan
  4. Read a saved plan as JSON

    terraform
    terraform show -json tfplan
  5. List resources in state

    terraform
    terraform state list
Safety

Safety rules for agents

Safe to run without asking

  • terraform fmt -check
  • terraform validate
  • terraform plan
  • terraform show
  • terraform state list / show
  • terraform output

Ask a human first

  • terraform apply
  • terraform destroy
  • terraform import
  • terraform state rm / mv
  • terraform force-unlock
  • Stop at terraform plan. Applies are reviewed and run by a human or a CI pipeline.
  • Never pass -auto-approve, and never run terraform destroy.
  • Treat state as sensitive: it can contain secrets in plain text. Do not print it in full.
Agent instructions

AGENTS.md or CLAUDE.md snippet

Paste this into the instruction file your agent reads at the start of every session, then adjust the rules to your setup.

AGENTS.md
## Terraform (terraform)

Use `terraform` for formatting, validating, and planning infrastructure-as-code changes.

- Check it is installed with `terraform version`. If it is missing, ask before installing it.
- Auth: credentials are set up by a human. Never print or commit them.
- Run without asking (read-only): `terraform fmt -check`, `terraform validate`, `terraform plan`, `terraform show`, `terraform state list / show`, `terraform output`.
- Ask a human first (changes or deletes things): `terraform apply`, `terraform destroy`, `terraform import`, `terraform state rm / mv`, `terraform force-unlock`.
- Stop at terraform plan. Applies are reviewed and run by a human or a CI pipeline.
- Never pass -auto-approve, and never run terraform destroy.
- Treat state as sensitive: it can contain secrets in plain text. Do not print it in full.
- Prefer JSON output where the command supports it, and keep output short.
CLI or MCP server?

Terraform or an MCP server?

An official MCP option exists. HashiCorp publishes an official Terraform MCP server for looking up providers, modules, and registry docs. It complements the CLI: the server helps write configuration, the CLI validates and plans it.

Rule of thumb: in a terminal agent that a human has signed in, the CLI is usually faster and cheaper in context. In a chat client without a shell, or when you want a fixed set of typed tools, use an MCP server.

Same category
All Cloud & deploy tools
FAQ

Terraform questions

How do I install Terraform?

Homebrew (HashiCorp tap): brew tap hashicorp/tap && brew install hashicorp/tap/terraform. Check the install with terraform version, and see the HashiCorp docs for other platforms.

How does an AI agent authenticate Terraform?

Terraform uses each provider's own credentials, for example the AWS, Google Cloud, or Azure environment variables and profiles. terraform login stores a token for HCP Terraform.

Which Terraform commands are safe for an agent to run?

Commands that only read state are safe to run without asking: terraform fmt -check; terraform validate; terraform plan; terraform show; terraform state list / show; terraform output. Ask a human before anything that changes or deletes something: terraform apply; terraform destroy; terraform import; terraform state rm / mv; terraform force-unlock.

Is there an MCP server for Terraform?

HashiCorp publishes an official Terraform MCP server for looking up providers, modules, and registry docs. It complements the CLI: the server helps write configuration, the CLI validates and plans it.