The Simplest Way to Make Cloud Run and Terraform Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Using cloud run and terraform together lets teams build and ship container apps in code. Terraform sets up the project rules while Cloud Run hosts and scales the running containers. This workflow turns manual console clicks into clean text files that live in git. Teams review every change before it goes live, and every deploy follows the exact same path. How cloud run and terraform handle identity and data flow First, an engineer or build pipeline logs in to Google Cloud with a dedicated ide

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 cloud run and terraform together lets teams build and ship container apps in code. Terraform sets up the project rules while Cloud Run hosts and scales the running containers.

This workflow turns manual console clicks into clean text files that live in git. Teams review every change before it goes live, and every deploy follows the exact same path.

How cloud run and terraform handle identity and data flow

First, an engineer or build pipeline logs in to Google Cloud with a dedicated identity. Terraform reads the local state file and compares it against the live cloud project.

Next, Terraform sends API calls to create or update the Cloud Run service. It defines the container image, memory limits, scaling bounds, and network rules in one pass.

Two separate identities matter in this flow. The deploy identity runs Terraform and needs permission to create services and attach roles.

The runtime identity belongs to the Cloud Run app itself. The app uses this service account to read secrets, write logs, or query a database.

Keeping these two identities separate stops a compromised app from changing its own cloud settings. Data flows from the build system to the registry, and Terraform updates the service pointer.

Common failure modes and how to scope roles

Many teams fail by giving one shared service account to both Terraform and the app. If that broad key leaks, an attacker can delete services or read every project secret.

Another common bug happens when teams mix Terraform with manual image deploys. When a build script updates the container tag outside Terraform, the next plan reverts the app.

To fix this, split your setup into base rules and app release steps. Let Terraform manage the network, service account roles, and secret shells.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

If Terraform also manages the service, tell it to ignore changes to the image tag. Or pass the exact image digest into Terraform during every build pipeline run.

Scope the deploy role so it can only touch Cloud Run and its assigned service accounts. Never grant project owner or editor roles to a build runner.

Remove static JSON keys from your build servers as well. Use federated workload identity so your code host trades a short token for cloud permissions.

Securing network paths and state files

Terraform stores a record of every resource it creates inside a state file. That file often holds plain text details about your services, network paths, and environment inputs.

Store the state file in a remote cloud bucket with bucket locks and encryption turned on. Limit read and write permissions on that bucket to the deploy identity alone.

Lock down the network path for your Cloud Run apps at the same time. Set ingress rules so internal apps only accept traffic from your private network or load balancer.

Route outbound traffic through a private connector when the app talks to internal databases. This keeps database traffic off the public internet and inside your cloud network.

When engineers need to inspect live databases or masked fields like emails and card numbers, log every query. Clear audit logs show who ran each command and keep production data safe.

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

Should Terraform deploy every new Cloud Run container image?

It depends on your release cadence. Many teams use Terraform for roles and network setup, then let build pipelines update the image.

How do I stop Terraform from overwriting my latest Cloud Run image?

Configure your Terraform service block to ignore changes to the container image field. You can also pass the active image digest variable into every Terraform run.

How should Terraform pass secrets to a Cloud Run service?

Do not put raw secret values in your Terraform variables or state file. Have Terraform link the Cloud Run service to secret manager references instead.

The query runs. The emails stay hidden.