setupkits

How to stop Claude Code running terraform apply or wrangler deploy

If production deploys only through CI, an agent that runs terraform apply from your laptop skips the plan review, the state lock and every check in your pipeline. Telling it "don't" in CLAUDE.md helps, but a hook makes it a rule.

Why instructions alone aren't enough

CLAUDE.md is guidance. In a long session, after a failed CI run, "just apply it locally to unblock" can look reasonable to the model. A PreToolUse hook runs before every Bash command and every file write. If it exits with code 2, Claude Code cancels the call and shows the model your reason. The model reads the reason and takes the right path instead.

What to block, and what to keep

The trap: matching text instead of commands

A simple regex like /terraform\s+apply/ also blocks git commit -m "never run terraform apply locally". We hit exactly this: our own hook refused to create a GitHub repo because the description mentioned the words. The fix is to split the command line on ;, &&, ||, | and newlines, strip leading VAR=value assignments and runners like npx or sudo, and match only at the start of each segment. Then cd infra && terraform apply is still blocked, but quoted text is not.

Install it in two minutes

The hook is free and open source (MIT), with tests: claude-code-cloudflare-guard. Copy .claude/hooks/ into your repo and merge its .claude/settings.json entry into yours. It needs Node 18 or newer and has no dependencies. For a deliberate override, prefix the command with SETUPKITS_ALLOW=1.

Going further

The hook stops the mistakes. The Cloudflare Workers Kit also teaches Claude Code how you ship: four skills (deploy, infra, secrets, debug), a CLAUDE.md template, and the Terraform and GitHub Actions template that deploys this site, with a plan that fails on any destroy and a live smoke test after every deploy.