What a backend on Google Cloud needs, from Cloud Run and Cloud SQL to Pub/Sub, buckets, secrets and service accounts, what Terraform for it looks like, and how to deploy the same backend to your own GCP project with the infrastructure declared in application code.

Getting one container onto Cloud Run takes a single command. Getting a backend onto Google Cloud, with Cloud SQL on a private IP, Pub/Sub topics with dead-letter handling, a bucket, secrets and a service account per service, is where teams reach for Terraform and a few hundred lines of HCL.
This guide lists what such a backend needs, what the usual tools cover, and how to deploy it with the infrastructure declared in the application code instead.
The Cloud Run v2 service resource alone has dozens of arguments, and every environment repeats the set with different names and sizes. Connecting Cloud Run to Cloud SQL adds its own choices between the Cloud SQL connector, private IP and the Auth Proxy, which Google lays out in its Cloud Run connection guide.
gcloud run deploy --source . builds the code with Google Cloud's buildpacks and Cloud Build and deploys it, creating an Artifact Registry repository named cloud-run-source-deploy on first use, per the source deployment docs. It is the quickest route for one stateless service, and everything around it, the database, topics, buckets and IAM, becomes a script of gcloud commands that has to stay in sync with what is deployed.
Pulumi swaps HCL for a general-purpose language and keeps the same model, where a separate infrastructure program describes each resource and someone keeps it in step with the code that uses those resources. Config Connector does the same with Kubernetes manifests. All of them still ask the application team to know which IAM role lets a Cloud Run service publish to a topic.
Encore brings infrastructure from code: the service declares the database, topics, buckets and secrets it uses as part of its code, and Encore provisions them in your own GCP project. Per the GCP infrastructure docs, each environment gets a dedicated project with a private network, services run on Cloud Run or GKE, Postgres runs on Cloud SQL with daily backups kept for 7 days and point-in-time recovery, Pub/Sub topics get dead-letter topics, and secrets come from Secret Manager.

Run in your terminal to get started locally.
An orders service with Postgres, a topic for order events, a bucket for receipts and an API key secret, which together replace most of the table above:
// orders/orders.go
package orders
import (
"context"
"fmt"
"time"
"encore.dev/beta/errs"
"encore.dev/pubsub"
"encore.dev/rlog"
"encore.dev/storage/objects"
"encore.dev/storage/sqldb"
)
// Cloud SQL for PostgreSQL on a private IP; `encore run` starts Postgres locally.
var db = sqldb.NewDatabase("orders", sqldb.DatabaseConfig{Migrations: "./migrations"})
// A Pub/Sub topic, with a dead-letter topic for messages that keep failing.
var OrderPlaced = pubsub.NewTopic[*OrderPlacedEvent]("order-placed", pubsub.TopicConfig{
DeliveryGuarantee: pubsub.AtLeastOnce,
})
// A Cloud Storage bucket in your project; the local filesystem under `encore run`.
var Receipts = objects.NewBucket("receipts", objects.BucketConfig{Versioned: false})
// Stored in Secret Manager; set with `encore secret set --type prod MailerAPIKey`.
var secrets struct {
MailerAPIKey string
}
type OrderPlacedEvent struct {
OrderID int64
}
type PlaceParams struct {
CustomerEmail string
TotalCents int64
}
type PlaceResponse struct {
OrderID int64
}
//encore:api public method=POST path=/orders
func Place(ctx context.Context, p *PlaceParams) (*PlaceResponse, error) {
if p.CustomerEmail == "" || p.TotalCents <= 0 {
return nil, &errs.Error{Code: errs.InvalidArgument, Message: "customer email and a positive total are required"}
}
var id int64
err := db.QueryRow(ctx,
`INSERT INTO orders (customer_email, total_cents) VALUES ($1, $2) RETURNING id`,
p.CustomerEmail, p.TotalCents).Scan(&id)
if err != nil {
return nil, err
}
if _, err := OrderPlaced.Publish(ctx, &OrderPlacedEvent{OrderID: id}); err != nil {
return nil, err
}
return &PlaceResponse{OrderID: id}, nil
}
var _ = pubsub.NewSubscription(OrderPlaced, "write-receipt", pubsub.SubscriptionConfig[*OrderPlacedEvent]{
Handler: writeReceipt,
RetryPolicy: &pubsub.RetryPolicy{
MinBackoff: 10 * time.Second,
MaxBackoff: 5 * time.Minute,
MaxRetries: 8,
},
})
func writeReceipt(ctx context.Context, e *OrderPlacedEvent) error {
var email string
var total int64
err := db.QueryRow(ctx,
`SELECT customer_email, total_cents FROM orders WHERE id = $1`, e.OrderID).Scan(&email, &total)
if err != nil {
return err
}
// Writing the same key again overwrites the receipt, so redelivery is harmless.
w := Receipts.Upload(ctx, fmt.Sprintf("receipts/%d.txt", e.OrderID), objects.WithUploadAttrs(objects.UploadAttrs{
ContentType: "text/plain",
}))
if _, err := fmt.Fprintf(w, "Order %d\nTotal: %d cents\n", e.OrderID, total); err != nil {
w.Abort(err)
return err
}
if err := w.Close(); err != nil {
return err
}
rlog.Info("receipt stored", "order_id", e.OrderID, "mailer_configured", secrets.MailerAPIKey != "")
return nil
}
-- orders/migrations/1_create_orders.up.sql
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
customer_email TEXT NOT NULL,
total_cents BIGINT NOT NULL CHECK (total_cents > 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
encore run starts Postgres, the topic and the bucket on your machine, so placing an order and watching the receipt land in the local bucket works before anything exists in GCP. The Pub/Sub docs cover retries and the dead-letter topic, and the secrets docs cover setting values per environment.
In the Encore dashboard, the Connect Cloud page shows a service account for your app, and you grant it access to your GCP organization or to a single project, as described in Connect your cloud account. Create an environment that targets GCP and Cloud Run, then push:
Encore builds the image, creates the resources from the table above, including a service account for the service with access to the resources it uses, and shows the provisioned resources in the dashboard. It does not destroy infrastructure on its own when something is removed, so deletions need an explicit approval. A staging environment is the same code deployed to its own project with its own resources.
With the topic, bucket and database declared as lines in the service, an AI coding agent can add a new subscriber or bucket in the same change as the code that uses it, run it under encore run, and leave nothing for a separate infrastructure pull request.
Best fit: Teams moving a backend to their own GCP project who want Cloud Run, Cloud SQL and Pub/Sub provisioned from the code that uses them.
Use gcloud run deploy for a single stateless service, and Terraform or Pulumi when a platform team already owns GCP infrastructure as code and the application consumes what it provides. Use Encore when the application team owns its backend end to end and wants the GCP resources to follow the code automatically in every environment.
Yes. For a single stateless service, gcloud run deploy --source builds and deploys from your source code. Once the backend needs Cloud SQL, Pub/Sub, buckets, secrets and service accounts, you either script those with gcloud, describe them with an infrastructure as code tool, or use a framework that provisions them from application code.
Typically google_cloud_run_v2_service, google_sql_database_instance with google_sql_database and google_sql_user, a VPC with private services access for private IP, google_pubsub_topic and google_pubsub_subscription, google_storage_bucket, google_secret_manager_secret, google_artifact_registry_repository, a service account per service and IAM bindings for each resource it uses.
It is enough for the service itself, and it creates an Artifact Registry repository for the built images on first use. The database, private networking, topics, buckets and the IAM bindings between them still have to be created and kept consistent across environments by some other means.
With Encore you declare the database with sqldb.NewDatabase, topics with pubsub.NewTopic and buckets with objects.NewBucket in the service code, connect your GCP project once, and push. Encore provisions Cloud Run or GKE, Cloud SQL, Pub/Sub, Cloud Storage and Secret Manager in your project for each environment.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.