The Simplest Way to Make Azure Functions and Spanner Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams use azure functions and spanner together to run event code against a global database. The function takes an event, gets a short token, and reads or writes rows. This setup splits compute and storage across two big cloud platforms. An event in the first cloud triggers a small piece of code. That code then talks to the database in the second cloud. Done right, this pattern keeps apps fast and scales with user load. Done wrong, it leaves static keys sitting in app settings for months. How

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 azure functions and spanner together to run event code against a global database. The function takes an event, gets a short token, and reads or writes rows.

This setup splits compute and storage across two big cloud platforms. An event in the first cloud triggers a small piece of code. That code then talks to the database in the second cloud. Done right, this pattern keeps apps fast and scales with user load. Done wrong, it leaves static keys sitting in app settings for months.

How identity and data flow between azure functions and spanner

Linking two clouds starts with how the app logs in. The function needs a safe way to prove who it is. In a good setup, you do not use static JSON keys. Instead, you set up workload identity federation between the two clouds.

First, you give the function its own managed identity in its home cloud. When the function runs, it asks its cloud for a local token. Next, the function sends that token to the second cloud's token service. The second cloud checks the token against a trust rule you set up. If the check passes, it gives back a short-lived database token.

Now the function can open a session to the database. The client library inside the function holds a pool of open sessions. When an event arrives, the code grabs an open session from the pool. It runs a read or write query and sends back the result. Good session handling stops the function from making too many new sessions under load.

Common failure modes and how to scope roles

The biggest risk in a cross-cloud setup is a shared service account. Many teams make one database role with full admin rights. Then they paste that role's static key into twenty different function apps. If one app leaks its key, an attacker gets the whole database. Worse, when a bad query runs, the logs show the same shared name. You cannot tell which function or developer caused the spike.

Another common trap is how serverless apps scale during traffic spikes. When traffic jumps, the cloud spins up dozens of new function instances. Each new instance tries to build its own pool of database sessions. Without tight limits on session count, the app can waste database compute. You must tune the client pool so idle instances close their sessions fast.

Azure RBAC + Cloud Functions IAM: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To fix identity risks, give each function its own cloud identity. Map each identity to a fine-grained database role with narrow rights. A function that only reads orders should never have write permissions. If an app handles user profiles, mask fields like emails or phone numbers. Masking rewrites matched fields in results before the payload leaves the database path.

Network paths and query logs across clouds

Sending data across two clouds means traffic crosses the public internet by default. For private workloads, teams link the two cloud networks with a private tunnel. You must first put the function on a virtual network plan. That way, outbound database calls travel through your private network link and firewall rules.

You also need clear logs for every query that hits the database. Default cloud logs often show that a service account logged in. They rarely show the exact query text without extra audit settings turned on. Turn on data read and write logs for the tables that hold card numbers. Pair those logs with request tags from the function code. This lets your team trace any database write back to the exact event.

Add runtime guardrails and masking beside your databases and APIs

Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.

Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.

Frequently asked questions

How do we avoid putting static database keys in function settings?

Set up workload identity federation between the two cloud providers. The function trades its local managed identity token for a short-lived database token.

Why does our serverless app create too many database sessions?

Each new function instance builds its own session pool when traffic spikes. Lower the minimum session count in the client and reuse one client per instance.

How should we scope database roles for multiple function apps?

Create separate roles for read tasks and write tasks on specific tables. Bind each function identity only to the single role its code needs to run.

The query runs. The emails stay hidden.