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.
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 Terraform
The install commands from the official docs. Check it worked with terraform version.
brew tap hashicorp/tap && brew install hashicorp/tap/terraform
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.
terraform login
What agents use Terraform for
Example commands with placeholder names. Swap in your own project, app, or file names.
Check formatting
terraform fmt -check -recursive
Validate configuration
terraform validate
Run terraform init first in a fresh checkout.
Save a plan for review
terraform plan -out=tfplan
Read a saved plan as JSON
terraform show -json tfplan
List resources in state
terraform state list
Safety rules for agents
Safe to run without asking
terraform fmt -checkterraform validateterraform planterraform showterraform state list / showterraform output
Ask a human first
terraform applyterraform destroyterraform importterraform state rm / mvterraform 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.
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.
## 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.
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.
Other Cloud & deploy tools
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.