The Simplest Way to Make Caddy and OpenTofu Work Like They Should
Teams use caddy and opentofu to build cloud servers and route web traffic. OpenTofu sets up the machines, while Caddy handles web requests and TLS certs. When these two tools work together, teams can ship new web apps fast. OpenTofu creates the virtual machines, DNS records, and firewall rules in the cloud. Then it passes the new server addresses and settings over to Caddy. Caddy reads those rules and gets TLS certs for each domain name. This workflow removes manual web server edits and keeps r
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
Teams use caddy and opentofu to build cloud servers and route web traffic. OpenTofu sets up the machines, while Caddy handles web requests and TLS certs.
When these two tools work together, teams can ship new web apps fast. OpenTofu creates the virtual machines, DNS records, and firewall rules in the cloud. Then it passes the new server addresses and settings over to Caddy. Caddy reads those rules and gets TLS certs for each domain name. This workflow removes manual web server edits and keeps routing rules in code. Engineers can review every routing change in git before it goes live.
How logins and data flow between caddy and opentofu
The workflow starts when an engineer merges a code change in git. OpenTofu reads the state file and logs in to the cloud provider API. It uses a cloud role to create networks, disks, and compute instances. Once the servers run, OpenTofu sends the new IP addresses to Caddy. It can write a startup file or call the Caddy admin endpoint.
Caddy needs its own set of permissions to finish the setup. When Caddy boots up, it reads the domain names from the file. If a domain needs a wildcard cert, Caddy talks to the DNS provider. It uses a scoped DNS API token to prove it owns the domain. Then the cert authority issues the TLS cert and Caddy stores it safely. Data flows one way from the code repo to the running web server.
Common failure modes and how to scope roles
Many teams make mistakes when they link these two tools in production. The biggest risk comes from shared service accounts with broad cloud keys. Teams often give OpenTofu full admin rights so deployments do not fail. Even worse, they might pass that same cloud key to the Caddy server. If an attacker breaks into the web server, they get the cloud key. Then they can read state files or delete databases across the whole account.
Another failure happens when teams open the Caddy admin endpoint to the network. If OpenTofu pushes settings over a public port, anyone might change routing rules. Unlogged API calls make it hard to see who changed a live route. Teams also get rate limited when OpenTofu rebuilds Caddy servers too often. If Caddy does not save certs on a shared disk, it asks for new ones on every run.
End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
You can fix these risks with tight roles and strict network paths. Give OpenTofu its own deployment role that only runs during a build job. Give the Caddy server a separate host role with zero cloud build rights. Its DNS token should only edit text records for one specific domain zone. Bind the Caddy admin port to the local machine so outside hosts cannot reach it. Have OpenTofu pass settings through a local file or a private deployment runner. Store TLS certs on a persistent volume so rebuilds do not lose them.
Keeping state files and routing rules safe
OpenTofu state files hold every value that you pass into a server setup. If you pass raw API tokens to Caddy through templates, they sit in state. Anyone who can read the state bucket can also read those raw tokens. You should lock down the state bucket so only the build runner reads it. Turn on bucket logs to track every single read and write request.
You also need clear logs for every change made to your web routes. When an engineer updates a domain rule, the build log should record their name. On the server side, Caddy should write structured logs for all web traffic. If sensitive data like emails or card numbers appear in logs, mask them first. Rewriting matched fields in results keeps private user data out of your central log store. Good role design and clean logs keep your web routing fast and safe.
Check database and API queries on the wire
Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.
Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.
Frequently asked questions
Should OpenTofu manage Caddy settings through files or the API?
Using a startup file on disk is safer for most cloud teams. It lets you keep the Caddy admin port closed to outside network traffic. OpenTofu can update the file and reload the service over a local command.
How do we stop TLS rate limits when OpenTofu replaces servers?
Attach a persistent storage volume or shared storage bucket to your Caddy instances. When OpenTofu destroys an old server, the new server reads the saved certs. This stops Caddy from asking the cert authority for a brand new cert.
How should we pass DNS tokens to Caddy during a build?
Do not write raw DNS tokens directly inside your OpenTofu code files. Have OpenTofu assign an instance identity to the server during the build step. Then let the server fetch its own scoped DNS token when it starts up.
Related guides
In this post