The Simplest Way to Make Cloud Functions and Spanner Work Like They Should
Teams use cloud functions and spanner to run event code on global data. The function handles incoming requests, and the database stores relational records across regions. This pair lets developers build fast APIs and background jobs with ease. You do not need to manage servers when traffic spikes up or down. Still, serverless code behaves in a unique way with stateful databases. You must set up roles, sessions, and network paths with great care. Good setup stops timeouts and keeps your data saf
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 functions and spanner to run event code on global data. The function handles incoming requests, and the database stores relational records across regions.
This pair lets developers build fast APIs and background jobs with ease. You do not need to manage servers when traffic spikes up or down. Still, serverless code behaves in a unique way with stateful databases. You must set up roles, sessions, and network paths with great care. Good setup stops timeouts and keeps your data safe in production.
How cloud functions and spanner pass identity and data
Every function run uses a dedicated cloud service account to prove its identity. The cloud platform gives this account a short token at runtime. Your code sends this token to the database when it opens a session. The database checks the IAM role tied to that service account. If the role allows the action, the database runs the SQL query.
Data moves between the function and the database over Google network links. Client libraries manage a pool of database sessions inside the function memory. When a function wakes up, it creates a fresh pool of sessions. Warm functions reuse these open sessions for the next incoming requests. This reuse cuts down wait times and keeps read and write speeds fast.
You should keep the client object in global scope inside your code. Global scope lets warm function instances share the same session pool. If you create a client inside the handler, every call makes new sessions. That pattern wastes time and hits database limits during high traffic.
Common failure modes and how to scope roles
Shared service accounts cause major security risks for many cloud teams. Teams often give the default compute account broad editor roles to save time. If one function gets hacked, the attacker can read or delete all tables. You should create one unique service account for each function instead. Give a read role to query functions and a write role to updaters.
Session leaks are another common bug when running serverless code at scale. Functions scale out fast, and each instance opens its own session pool. Five hundred function instances can open thousands of database sessions at once. Yet the database has a hard cap on active sessions per node. You must tune the pool settings to keep minimum and maximum counts low.
Cloud Functions IAM + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Unlogged queries and human debug access also create blind spots for security teams. When a bug hits production, engineers often test queries from local laptops. They might use broad admin keys to check live tables and fix data. These manual runs rarely link back to a clear user identity in logs. Data masking is also missing when developers query sensitive rows directly. Good masking rewrites matched fields like emails, phone numbers, or card numbers in results.
Securing network paths and developer workflows
Network rules help lock down how traffic reaches your database instances. You should route function traffic through private network channels when possible. Private routing stops data from crossing the public internet during a call. You can also set up perimeter rules around your cloud project resources. Perimeter rules block data theft even if a service key leaks out.
Human access needs a different path than automated function traffic. Lock down the production service accounts so people cannot download static keys. Require every engineer to log in with their own work identity for debugging. Keep a clear audit trail of every read and write query they run. Also review IAM roles each month to drop unused permissions from old functions.
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 my function run out of database sessions during traffic spikes?
Each function instance creates its own session pool when it starts up. Set your client pool size to a small number in global scope.
Should multiple functions share the same service account for database queries?
No, sharing one account gives every function the exact same data permissions. Create a separate service account for each function with only the needed role.
How can we protect sensitive columns when engineers debug database issues?
Do not let engineers use function service keys to run manual queries. Use a tool that logs user identity and rewrites emails or card numbers.
Related guides
In this post