Practice Lab: Using Cloud Storage and Deploying Applications on Cloud Run

Nova Novriansyah
NovAI Cloud Computing — GCP
5 min readJun 9, 2024

Objective: In this practice lab, you will learn how to work with Google Cloud Storage, utilize it within your applications, and deploy applications using Google Cloud Run. By the end of this lab, you will have uploaded an image to Cloud Storage, created a containerized application that displays the uploaded image, and deployed this application using Google Cloud Run.

Part 1: Setting Up Your Google Cloud Account

  1. Create a Google Cloud Account:
  • Go to the Google Cloud Console.
  • Sign in with your Google account. If you don’t have one, create a new Google account.

If you havent yet set billing and requeted to set it, please do so:

Set Up Billing:

  • Navigate to the “Billing” section in the Console.
  • Add your billing information. Google offers a free tier with credits for new users.

Part 2: Setting Up Cloud Storage

  1. Navigate to Cloud Storage:
  • In the Google Cloud Console, use the navigation menu to go to “Storage” > “Browser.”

2. Create a New Bucket:

  • Click “Create bucket.”
  • Configure your bucket:
  • Name: Enter a unique name (e.g., my-app-bucket2). If the name already used, use can change it. But on the code to deploy later you need to adjust it to refer this new name.
  • Location type: Choose “Region” and select a region close to you.
  • Storage class: Select “Standard.”
  • Click “Create” to set up the bucket.
Creating bucket
After bucket creation

3. Upload an Image to the Bucket:

  • After creating the bucket, click on its name to open it.
  • Click “Upload files” and select any jpeg image from your computer to upload.
Upload image to the bucket
  • Rename the image as myimage.jpg as shown below
rename the image

Part 3: Creating a Containerized Application

This application will shows the image you uploaded before.

  1. Install Docker:

2. Create a Simple Application:

  • Create a directory for your application, e.g., my-app.
  • Inside the directory, create a file named app.py with the following content:
from flask import Flask, render_template
from google.cloud import storage
import os

app = Flask(__name__)

@app.route('/')
def home():
bucket_name = os.environ.get('BUCKET_NAME')
image_name = os.environ.get('IMAGE_NAME')
if not bucket_name or not image_name:
return "Bucket name or image name not configured.", 400

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(image_name)
image_url = blob.generate_signed_url(version="v4", expiration=timedelta(minutes=15), method="GET")

return render_template('index.html', image_url=image_url)

if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)

3. Create a Template File:

  • Inside the my-app directory, create a folder named templates.
  • In the templates folder, create a file named index.html with the following content:
<!DOCTYPE html>
<html>
<head>
<title>Uploaded Image</title>
</head>
<body>
<h1>Uploaded Image</h1>
<img src="{{ image_url }}" alt="Uploaded Image">
</body>
</html>

4. Create a Dockerfile:

  • In the same directory (my-app), create a file named Dockerfile with the following content:
FROM python:3.8-slim
WORKDIR /app
COPY app.py /app
COPY templates /app/templates
RUN pip install flask google-cloud-storage
CMD ["python", "app.py"]

5. Build the Docker Image:

  • Open a terminal, navigate to your application directory, and run the following command to build your Docker image:
docker build -t my-app .

6. Run the Docker Container Locally:

  • Run the following command to start your container:
docker run -p 8080:8080 -e BUCKET_NAME=my-app-bucket2 -e IMAGE_NAME=your-image-file-name my-app
  • Replace the my-app-bucket2and your-image-file-name with the name of the image you uploaded to the bucket.
  • Visit http://localhost:8080 in your browser to see your running application.

Part 4: Deploying the Application Using Google Cloud Run

Google Cloud Run is a fully managed compute platform by Google Cloud Platform (GCP) that allows you to run stateless containers directly on top of Google’s scalable infrastructure. Cloud Run abstracts away the complexities of managing the underlying infrastructure, enabling developers to focus solely on writing code. It offers the flexibility of containerized applications without the need to manage servers, clusters, or other infrastructure components.

  1. Enable the Cloud Run API:
  • In the Google Cloud Console, go to “APIs & Services” > “Library.”
  • Search for “Cloud Run API” and click “Enable.”

2. Install Google Cloud SDK:

  • If you don’t already have the Google Cloud SDK installed, download and install it from the Google Cloud SDK website.

3. Authenticate with Your Google Cloud Account:

  • Open a terminal and run the following command to authenticate:
gcloud auth login

4. Set Your Project ID:

  • Set your project ID using the following command:
gcloud config set project [YOUR_PROJECT_ID]
  • Replace [YOUR_PROJECT_ID] with your actual GCP project ID.

5. Build and Push the Docker Image to Google Container Registry:

  • Tag your Docker image:
docker tag my-app gcr.io/[YOUR_PROJECT_ID]/my-app
  • Push the image to Google Container Registry:
docker push gcr.io/[YOUR_PROJECT_ID]/my-app

6. Deploy the Application to Cloud Run:

  • Deploy your container image to Cloud Run:
gcloud run deploy my-app --image gcr.io/[YOUR_PROJECT_ID]/my-app --platform managed --region [YOUR_REGION] --allow-unauthenticated --set-env-vars BUCKET_NAME=my-app-bucket,IMAGE_NAME=your-image-file-name
  • Replace [YOUR_PROJECT_ID], [YOUR_REGION], my-app-bucket and your-image-file-name with your actual GCP project ID, the region you want to deploy to, and the name of the image file you uploaded.

7. Access Your Application:

  • After deployment, you will get a URL where your application is hosted. Visit this URL in your browser to see your running application displaying the uploaded image.

Congratulations! You have successfully set up cloud storage, uploaded an image, created a containerized application that displays the uploaded image, and deployed the application using Google Cloud Run. This practice lab covered the essential steps to work with cloud storage, manage containers, and deploy applications in GCP.

By mastering these skills, you are now equipped to handle more advanced scenarios and configurations in GCP, enabling you to build, deploy, and scale cloud-native applications effectively. Continue exploring GCP to leverage its full potential for your projects and applications.

--

--

Nova Novriansyah
NovAI Cloud Computing — GCP

C|CISO, CEH, CC, CVA,CertBlockchainPractitioner, Google Machine Learning , Tensorflow, Unity Cert, Arduino Cert, AWS Arch Cert. CTO, IT leaders. Platform owners