DoiT Cloud Intelligence™
Cloud Run and Cloud Storage…now a perfect match

This article describes the recent feature enhancement to Cloud Run allowing Cloud Storage bucket to be mounted as a Container volume.
Introduction
If you want to use serverless services such as Cloud Run for a static website, you probably want to use files stored in a bucket. A new feature from Google Cloud Platform now makes this easier than ever. If you are unfamiliar with Cloud Run, it is a managed service for launching containerized workloads on Google’s scalable infrastructure. In this article, we explore the static website example, however, the use cases for this feature are endless.
Problem
Until recently, accessing object storage with Cloud Run could be achieved using third-party tools or cloud client libraries within your application. While sufficient, these approaches often introduced scalability and management overhead to your application delivery process.
With the recent introduction of Cloud Storage mounts in Cloud Run, you can now mount Cloud Storage buckets as volumes within Cloud Run containers without utilizing additional libraries.
Now that we have covered the improvements and their benefits, let's move on to our use case, hosting a static website using Cloud Run and Cloud Storage volume mounts.
Solution
Start by creating a Project to host the application and storage bucket. Using your favorite command-line application, set a few convenient environment variables in the terminal and configure gcloud.
export GCP_PROJECT=my-cloud-run-static-site
export REGION=us-east4
gcloud projects create $GCP_PROJECT
gcloud config set project $GCP_PROJECT
Next, enable the APIs for Cloud Run, Cloud Build, Artifact Registry to utilize these services.
gcloud services enable run.googleapis.com \
cloudbuild.googleapis.com \
artifactregistry.googleapis.com
Now it’s time to pull down the code for our static site.
git clone https://github.com/waymanls/my-cloud-run-static-site.git
Looking at the contents of our index.html file, you can see that we place our images under the /images path. We will need to utilize this path later on in our tutorial. For now, let’s create the Cloud Storage bucket that will hold our static assets and also upload our JPEG image.
gcloud storage buckets create gs://$GCP_PROJECT-bucket --location=$REGION
gcloud storage cp beach.jpeg gs://$GCP_PROJECT-bucket/beach.jpeg
Next, create our Artifact Registry repository.
gcloud artifacts repositories create $GCP_PROJECT-repo \
--repository-format=docker \
--location=$GCP_REGION
Now that we have an Artifact Registry repo, we will build and push the container image using Cloud Build.
gcloud builds submit --tag $REGION-docker.pkg.dev/$GCP_PROJECT/$GCP_PROJECT-repo/$GCP_PROJECT-svc
After the container image has been pushed to our repository, we can use it to create a Second Generation Cloud Run application.
gcloud run deploy $GCP_PROJECT-svc --image $REGION-docker.pkg.dev/$GCP_PROJECT/$GCP_PROJECT-repo/$GCP_PROJECT-svc \
--execution-environment=gen2 --allow-unauthenticated
We can now implement the final component of our example, attaching our Cloud Storage bucket as a container volume. This process is performed by “updating” our Cloud Run service. Although configuration values are provided using switches on the gcloud command, these can also be passed using a YAML Service file. As you can see in the command below, we use the “ images” path we set earlier in our index.html file as the base path and name for our container volume.
gcloud run services update $GCP_PROJECT-svc \
--execution-environment=gen2 \
--add-volume=name=images,type=cloud-storage,bucket=$GCP_PROJECT-bucket \
--add-volume-mount=volume=images,mount-path=/usr/share/nginx/html/images
At the end of the above steps, you will be left with a Cloud Run application that successfully mounts a Cloud Storage bucket as a volume within your Cloud Run service and accesses an object from that volume.

Cleaning up
At completion, it is recommended that you delete the project using the following command.
gcloud projects delete $GCP_PROJECT
The benefits of this method include the ability to natively connect your serverless Cloud Run workloads directly to static assets within Object store backends, eliminating the previous need for cloud client libraries and third-party services.