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

Published
Length
3 min read

Luis Felipe Dias

All posts

You pair cloud run and opentofu to build and run serverless apps from code. OpenTofu sets up the cloud rules while Cloud Run hosts your container images. This workflow turns manual console clicks into repeatable files stored in version control. Teams can review every change before it reaches a live project. When a build finishes, the pipeline updates the service and shifts web traffic. How cloud run and opentofu share roles and data OpenTofu talks to the Google Cloud API to create and update

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.

You pair cloud run and opentofu to build and run serverless apps from code. OpenTofu sets up the cloud rules while Cloud Run hosts your container images.

This workflow turns manual console clicks into repeatable files stored in version control. Teams can review every change before it reaches a live project. When a build finishes, the pipeline updates the service and shifts web traffic.

How cloud run and opentofu share roles and data

OpenTofu talks to the Google Cloud API to create and update your services. It needs a dedicated identity with rights to manage Cloud Run resources. Your build pipeline logs in using workload identity federation instead of saved keys.

During a run, OpenTofu reads a remote state file stored in a cloud bucket. That state file tracks the current container image, memory limits, and network rules. OpenTofu compares your local code against the live state and plans the update.

Cloud Run also needs its own runtime service account to do its job. OpenTofu creates this account and attaches the exact roles your app needs. When the container starts, it uses that identity to read buckets or databases. Separating the build identity from the runtime identity keeps your project safe.

Common failure modes and how to scope roles

Many teams make the mistake of using one broad service account for everything. If a build runner has project owner rights, a bad script can delete databases. Shared accounts also make audit logs hard to read during an incident. You cannot tell which pipeline or developer made a specific change.

Long-lived JSON keys create another big risk for your team. Developers often download keys to test OpenTofu runs from their laptops. Those files can leak in chat logs, code commits, or old downloads folders. Instead, teams should use short-lived tokens tied to a user login or build job.

End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

State drift happens when someone edits a Cloud Run service in the web console. A quick manual fix to an image tag will break the next OpenTofu run. OpenTofu will overwrite the manual change and bring back the old bug. Lock down console write rights so all updates flow through your code repo.

You should also split image pushes from stack changes in your daily workflow. Let your app pipeline update the container image tag on its own schedule. Tell OpenTofu to ignore image tag changes after the first service deploy. This stops the two pipelines from fighting over the active container version.

Scoping network paths and secret handling

Cloud Run services are open to the public web if you allow unauthenticated calls. Use OpenTofu to set strict ingress rules so only internal traffic can enter. You can route traffic through a private VPC connector to reach internal databases. Never put raw database passwords or API keys in plain OpenTofu variables. Anything stored in a variable ends up in the state file in plain text. Instead, have OpenTofu link your Cloud Run service to a cloud secret manager. The container reads the secret at start time using its scoped runtime role. You should also encrypt the state bucket and limit who can read it.

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 stop OpenTofu from reverting new container deploys?

Set your OpenTofu lifecycle rules to ignore changes to the container image field. This lets your build pipeline deploy new app code while OpenTofu manages settings.

Where should we store the OpenTofu state file for Cloud Run?

Store the state file in a private Google Cloud Storage bucket with versioning on. Turn on object locking so two pipelines cannot write to the state at once.

How should developers run OpenTofu from their local machines?

Developers should only run plan checks locally using read-only roles and short-lived logins. Let your central build pipeline run the actual apply step after a code review.

The query runs. The emails stay hidden.