The Simplest Way to Make Cloud Run and Haproxy Work Like They Should
Teams use cloud run and haproxy together to route incoming traffic to serverless containers. HAProxy handles custom traffic rules, while Cloud Run scales your app containers up and down. This pair gives you fine control over requests without managing a huge fleet of servers. You get fast routing at the edge and simple container scaling in the back. Setting them up takes care with network paths, host headers, and service roles. How requests and roles flow between cloud run and haproxy In a st
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 cloud run and haproxy together to route incoming traffic to serverless containers. HAProxy handles custom traffic rules, while Cloud Run scales your app containers up and down.
This pair gives you fine control over requests without managing a huge fleet of servers. You get fast routing at the edge and simple container scaling in the back. Setting them up takes care with network paths, host headers, and service roles.
How requests and roles flow between cloud run and haproxy
In a standard setup, HAProxy sits in front of your Cloud Run apps. Outside users send web requests to HAProxy first. HAProxy checks the request path and headers against your custom routing rules. Then it forwards the traffic to the right Cloud Run URL.
Cloud Run expects a specific host header on every incoming request. It uses this header to find the right container service. If HAProxy passes the original user host header, Cloud Run rejects the request. You must set up HAProxy to rewrite the host header before sending traffic forward.
For private Cloud Run services, network routing is only half of the job. Cloud Run also checks who is sending the request through Google Cloud IAM. HAProxy needs a Google service account with the right role to call the app. A sidecar process or local job fetches an identity token for that service account. HAProxy attaches this token as an auth header on the outgoing request. Cloud Run checks the token and lets the request into the container.
You can also run HAProxy inside a Cloud Run container itself. In this pattern, Cloud Run takes the first hit from the internet. The container runs HAProxy to split traffic to other internal backends over a private network.
Common failure modes and how to fix them
One common mistake is using one shared service account for all backend routes. If HAProxy uses one broad role, any route mistake exposes every private app. Instead, break up your backends into clear groups. Give each group a dedicated service account with a narrow invoker role.
End-to-End Encryption + HAProxy Security: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Another big issue is stale DNS caching inside HAProxy. Cloud Run endpoints use dynamic IP addresses that change over time. By default, HAProxy might check DNS only when it starts up. When Google shifts the IP addresses, HAProxy keeps sending traffic to dead addresses. You must set up runtime DNS resolvers in HAProxy so it refreshes IP addresses often.
Unlogged traffic is another trap that hurts teams during an outage. If you only log requests at the edge, you miss internal routing failures. Make sure both tools write structured logs with a shared trace ID. HAProxy should create or pass a trace header on every call. Cloud Run reads this header and links its container logs to the same trace.
Watch your timeout values across both tools as well. If HAProxy waits longer than Cloud Run allows, connections drop in the middle of work. Set the HAProxy server timeout slightly lower than the Cloud Run request limit. Keep private network paths tight with a serverless VPC connector when linking internal nodes. Never leave backend Cloud Run services open to the public internet just to skip token setup.
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
Why does Cloud Run return a 404 error when HAProxy forwards traffic?
Cloud Run uses the HTTP host header to route requests to your container. HAProxy must rewrite the host header to match the exact Cloud Run service URL.
How do I stop HAProxy from sending traffic to dead Cloud Run IPs?
Add a resolver block in HAProxy and point it to reliable DNS servers. This forces HAProxy to look up fresh Cloud Run IP addresses while it runs.
Can HAProxy call a private Cloud Run service without public access?
Yes, you can restrict the Cloud Run service to internal traffic and require IAM checks. HAProxy must route through your private network and attach a valid identity token header.
Related guides
In this post