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

Published
Length
3 min read

Luis Felipe Dias

All posts

Running cosmosdb and terraform together lets teams set up cloud databases with plain text files. This pair turns manual clicks into repeatable steps that create tables, keys, and roles. Teams use this workflow to keep test and live databases matching at all times. You write rules once, check them in git, and run a plan to build. How cosmosdb and terraform handle logins and data First, the build tool logs in to your cloud account using an identity token. It checks its local state file against

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.

Running cosmosdb and terraform together lets teams set up cloud databases with plain text files. This pair turns manual clicks into repeatable steps that create tables, keys, and roles.

Teams use this workflow to keep test and live databases matching at all times. You write rules once, check them in git, and run a plan to build.

How cosmosdb and terraform handle logins and data

First, the build tool logs in to your cloud account using an identity token. It checks its local state file against the live cloud setup to find gaps.

Then, the tool sends requests to the cloud API to build or update databases. It sets up regions, backup rules, and throughput limits based on your text files.

Once the database is ready, the cloud API returns master keys and connection strings. The build tool saves those secret strings right inside its own state file.

Apps and engineers then need those keys or identity roles to read and write data. Setting up cosmosdb and terraform well means splitting control rules from actual data tasks.

Control rules build the database shell, set network borders, and pick the regions. Data tasks read and write items inside the tables after the build finishes.

Keep these two jobs apart so your build pipeline does not touch live data. Use role assignments in your build files to grant apps narrow data rights.

Common risks with shared keys and broad roles

Many teams give the build tool a single shared account with full admin rights. If someone steals that token, they can wipe out every database in minutes.

State files create another big risk when teams use cosmosdb and terraform at scale. The state file holds raw master keys for the database in plain text.

Anyone who can read that state file can grab the master key right away. With that key, a user can read all tables without leaving a clear trail.

CosmosDB RBAC + Terraform Security (tfsec, Checkov): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Shared master keys also make audit logs hard to read during a security review. Every query looks like it came from the same system account, not a person.

To fix this, lock down the remote bucket that holds your state files. Only the automated build runner should have rights to read or write that file.

Next, stop giving humans the master keys that the build tool creates. Turn off local key logins on the database so users must use cloud identities.

Scope the build runner role so it only manages database shells and network rules. Give it zero rights to read or change the actual records inside the tables.

Scoping network paths and live data checks

Good setups also lock down the network paths between your runners and the cloud. Public endpoints let anyone with a leaked key try to log in from home.

Use your build files to set up private network links for the database. Put your build runners inside the same private network so traffic stays off the internet.

Even with clean build files, engineers still need to check live data to fix bugs. They run queries to inspect broken records or check how fast a table runs.

Do not use the build tool to run ad-hoc data fixes or quick checks. Build tools work best for slow, planned changes to the outer database shell.

For daily data work, map each engineer to their own cloud identity role. Keep read roles separate from write roles so a mistake cannot drop a table.

Mask sensitive fields like emails, phone numbers, card numbers, and national IDs in results. Masking rewrites matched fields in results so personal data stays safe during debug sessions.

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

Why do database master keys show up in the state file?

The build tool tracks every detail of the resources it creates to spot drift later. Because the cloud API returns keys upon creation, the tool saves them in state.

Should developers run database builds from their own laptops?

No, running builds from laptops requires giving wide cloud rights to many people. Run all builds through a central pipeline so you only manage one narrow runner role.

How can teams rotate database keys without breaking live apps?

Cloud databases give you a primary key and a secondary key for safe rotation. Point apps to the secondary key, rebuild the primary key, and then switch apps back.

The query runs. The emails stay hidden.