How to create, version and grant access to Secret Manager secrets for a Cloud Run service with the console and gcloud, how secrets reach your container, what they cost, and how to get the same setup from application code in your own Google Cloud project.

Secret Manager is where API keys and database passwords go on Google Cloud, and creating a secret takes one command. Getting the value into a Cloud Run service takes a service account that may read it, a deploy flag and a plan for rotation.
This guide shows that with gcloud, then Terraform, then application code.
Pricing is listed on the Secret Manager pricing page, with 6 active versions and 10,000 access operations free each month, and the delivery options are described in the Cloud Run secrets guide.
The console creates a secret from a form, and gcloud does the same in a few commands, with the value read from standard input so it stays out of shell history:
The service then references the secret when it is deployed. Cloud Run resolves an environment variable when an instance starts and fails to start the instance if the lookup fails, so pinning a version number is the safer choice than latest:
That works for one secret in one environment, and the staging copy, the second service, the version bump after a rotation and the record of who changed what are all manual steps after it, which is the gap Terraform fills.
The secret and the access binding are two resources, with the value left to gcloud so it never lands in state:
variable "service_account_email" {
type = string
}
resource "google_secret_manager_secret" "stripe" {
secret_id = "billing-stripe-secret-key"
replication {
auto {}
}
}
resource "google_secret_manager_secret_iam_member" "read_stripe" {
secret_id = google_secret_manager_secret.stripe.id
role = "roles/secretmanager.secretAccessor"
member = "serviceAccount:${var.service_account_email}"
}
The Secret Manager with Terraform guide covers versions, rotation and customer-managed encryption keys in detail.
Encore brings infrastructure from code, so a secret is declared in the service that uses it, its value is set per environment from the CLI, and a deploy to your own Google Cloud project stores it in Secret Manager and injects it into that service, as the GCP infrastructure docs describe under secrets management. Each service can read only the secrets it declares, and the value is never in the repository, so an AI coding agent working in the project can declare and use a secret without seeing the production value. Local values set for development are readable on the machine that runs encore run.

Run in your terminal to get started locally.
The service reads a payment provider key and an admin token as secrets:
// customers/customers.go
package customers
import (
"context"
"crypto/subtle"
"encoding/json"
"net/http"
"net/url"
"strings"
"encore.dev/beta/errs"
)
var secrets struct {
StripeSecretKey string
AdminToken string
}
type CreateParams struct {
Email string `json:"email"`
}
type CreateResponse struct {
ID string `json:"id"`
}
//encore:api public method=POST path=/customers
func Create(ctx context.Context, p *CreateParams) (*CreateResponse, error) {
if !strings.Contains(p.Email, "@") {
return nil, &errs.Error{Code: errs.InvalidArgument, Message: "a valid email is required"}
}
form := url.Values{"email": {p.Email}}
req, err := http.NewRequestWithContext(ctx, "POST", "https://api.stripe.com/v1/customers", strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+secrets.StripeSecretKey)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Idempotency-Key", "customer-"+strings.ToLower(p.Email))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, &errs.Error{Code: errs.Unavailable, Message: "payment provider rejected the request"}
}
var out CreateResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return nil, err
}
return &out, nil
}
type StatusParams struct {
Token string `header:"X-Admin-Token"`
}
type StatusResponse struct {
OK bool `json:"ok"`
}
//encore:api public method=GET path=/admin/status
func Status(ctx context.Context, p *StatusParams) (*StatusResponse, error) {
if subtle.ConstantTimeCompare([]byte(p.Token), []byte(secrets.AdminToken)) != 1 {
return nil, &errs.Error{Code: errs.Unauthenticated, Message: "bad token"}
}
return &StatusResponse{OK: true}, nil
}
Values are set per environment type, with encore secret set --type dev,local StripeSecretKey for development and encore secret set --type prod StripeSecretKey for production, and encore run reads the local values. The secrets docs cover environment types and per-environment overrides. A cloud deploy fails until every declared secret has a value.
To deploy, follow Connect your cloud account and push. Encore stores the values in Secret Manager in your project and injects them into the services that declare them. Encore provisions an IAM role for the service, or a service account on GCP. Changing a value is another encore secret set. When teams bring their own agents to that work, Encore provides the environments, the system context and the controls, so an agent can declare a secret in the same change as the code that reads it without seeing the production value.
Best fit: Teams that want API keys and passwords in Secret Manager in their own Google Cloud project, declared next to the code that uses them.
Run gcloud secrets create with a name and the automatic replication policy, then add the value with gcloud secrets versions add and --data-file=- so it is read from standard input. Each later change is another version, and the secret keeps the same name.
Grant the service account roles/secretmanager.secretAccessor on the secret and pass it with --set-secrets, either as an environment variable or as a mounted file. Environment variables are resolved when an instance starts, so Google recommends pinning a version instead of using latest.
Active secret versions are billed at about $0.06 a month each after 6 free versions, and access operations cost $0.03 per 10,000 after the first 10,000 each month. Ten secrets with one version each cost about $0.24 a month, since 6 versions are free, and light traffic stays within the free access operations.
Declare the secret in code, set its value with encore secret set, and deploy to your own Google Cloud project. Encore provisions an IAM role for the service, or a service account on GCP. It stores the value in Secret Manager and grants that identity access to the secrets the service declares, with no binding or deploy flag to write by hand.
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.