The Simplest Way to Make Spanner and Terraform Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Using spanner and terraform together lets teams build and scale cloud databases with code. You write simple text files to set up database nodes, schemas, and roles. This workflow removes manual clicks in the cloud console. Teams review every database change in version control before the build tool runs it. When a review passes, the pipeline sends the change to the cloud API. The database then updates its capacity or schema to match your files. How permissions and data flow in spanner and terr

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.

Using spanner and terraform together lets teams build and scale cloud databases with code. You write simple text files to set up database nodes, schemas, and roles.

This workflow removes manual clicks in the cloud console. Teams review every database change in version control before the build tool runs it. When a review passes, the pipeline sends the change to the cloud API. The database then updates its capacity or schema to match your files.

How permissions and data flow in spanner and terraform

The build tool needs a cloud role to talk to the database service. You set up a dedicated service account for the automation runner to use. The runner logs in with short-lived tokens through workload identity instead of static keys. Once logged in, the tool reads the current state of your database instances. It compares that live state against the text files in your code repo.

If the tool finds a difference, it plans an update and calls the cloud API. Data flows in two main steps during a database deployment. First, the control plane sets up the regional instance and its compute capacity. Second, the tool sends schema instructions to create tables, columns, and indexes. You can also bind identity roles to specific databases during this same run. This keeps your database structure and your team permissions in one clean place.

Common failure modes and how to scope roles

Many teams make the mistake of giving the build runner a broad project owner role. If an attacker steals that token, they can delete data or read secret tables. Another common trap is putting schema updates and node scaling in one state file. Schema drops take time and can block simple capacity changes during a traffic spike. Teams also run into trouble when engineers run manual queries outside the code pipeline.

Manual fixes cause drift between the live database and the state file. On the next run, the automation tool might wipe out those manual schema fixes. You can fix this by splitting your setup into two separate stages. Use one workspace for instance capacity and a second workspace for table schemas. Give the instance runner a role that only manages compute nodes and network rules.

Terraform Security (tfsec, Checkov) + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Give the schema runner a role that only alters tables without reading row data. Never share one service account across dev, staging, and production projects. Set up private network paths so the runner only reaches the API from trusted subnets. When developers need to check data, require individual logins instead of shared build keys. Masking rules can also rewrite emails, phone numbers, card numbers, and national IDs in results.

How to manage state and schema changes

State files hold details about your database setup and must stay safe. Store your state files in a private cloud bucket with encryption turned on. Turn on state locking so two pipelines cannot change the database at once. When you update a big table, test the run in a staging project first. Large index builds can take hours and use extra compute nodes while they run.

Keep your schema change scripts small so each run finishes fast. If a schema step fails, small changes make it easy to find the bug. Always turn on deletion protection on your production instances inside your code files. This simple flag stops a bad code push from wiping out live user data. Review the plan output on every pull request to catch accidental drops early.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

Should I manage database schemas in the same state file as instances?

No, you should split instances and schemas into separate state files. This stops slow schema builds from blocking fast node scaling during high traffic.

How do I stop a code run from deleting a live database?

Enable the deletion protection setting on both the instance and database resources. The cloud API will then block any destroy command until you remove that flag.

How should the build runner log in to the cloud API?

Use workload identity federation to give the runner short-lived tokens for each job. Never store long-lived JSON service account keys in your code repo.

The query runs. The emails stay hidden.