This page contains code samples for Cloud Run. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.
Authenticate service-to-service requests
Services hosted on Google Cloud with access to the Compute Metadata Server are able to generate an OAuth authentication token using the service account identity associated with the service. This token can be used to authenticate the service as a permitted invoker of a Cloud Run service.
View in documentation
Generate a diagram with the dot tool from the graphviz package
Use a customized Dockerfile to configure system packages whose command-line utilities are used as part of serving HTTP requests.
View in documentation
Handler for Pub/Sub messages
Service to handle messages delivered by a Cloud Pub/Sub Push subscription.
Hello World service
Create a simple Hello World application, package it into a container image, upload the container image to Container Registry, and then deploy the container image to Cloud Run.
Lazy variable initialization
Demonstrate the use of lazy initialization of values for cases where memory allocation and response latency impacting operations are not commonly needed by the Cloud Run service.
View in documentation
View in documentation
Pub/Sub handler to process Cloud Storage events
This tutorial demonstrates using Cloud Run, Cloud Vision API, and ImageMagick to detect and blur offensive images uploaded to a Cloud Storage bucket.
View in documentation
Retrieve image from Cloud Storage to blur and then upload to a storage bucket
This tutorial demonstrates using Cloud Run, Cloud Vision API, and ImageMagick to detect and blur offensive images uploaded to a Cloud Storage bucket.
View in documentation
Send gRPC requests with authentication
Sends a request with an authorization header using a gRPC connection.
View in documentation
Send gRPC requests without authentication
Sends a request without authentication using a gRPC connection.
View in documentation
Service for local troubleshooting
Sample demonstrating an easily broken service that is difficult to troubleshoot without careful investigation, and an improved version of the code.
View in documentation
Sign in via Google
Client side code for signing in via the Google provider using the Firebase SDK
View in documentation
Trap termination signal (SIGTERM) sent to the container instance
Cloud Run sends a SIGTERM signal to your container instance before the container instance terminates, due to an event like scale down or deleted revision. By handling this signal, you can now gracefully terminate your applications and do some cleanup tasks–as opposed to an abrupt shutdown of the container.
Use Cloud Vision API to determine if image is safe
This tutorial demonstrates using Cloud Run, Cloud Vision API, and ImageMagick to detect and blur offensive images uploaded to a Cloud Storage bucket.
View in documentation
Using global scoped variables
Demonstrate how to minimize the memory footprint of reusable variables by leveraging global scope.
View in documentation
Write structured logs
Writes structured log entries with request log correlation using common libraries.