Serverless Architecture on GCP: Building Applications with Cloud Functions and Cloud Run
- Ashish Tiwari
- Oct 20, 2024
- 5 min read
Updated: Oct 21, 2024
In the rapidly evolving world of cloud computing, serverless architecture has become a game-changer, offering developers flexibility, scalability, and ease of use without the burden of managing servers. Google Cloud Platform (GCP) offers two standout serverless services—Cloud Functions and Cloud Run—that enable developers to build highly scalable and efficient applications. In this blog post, we will dive deep into how to harness the power of serverless architecture using GCP’s Cloud Functions and Cloud Run, and how these services can help you create real-time applications.

What is Serverless Architecture?
Before we delve into the details of Google Cloud’s serverless offerings, let's first clarify what serverless architecture means. Serverless does not mean there are no servers involved—rather, it means developers don’t have to worry about managing the infrastructure that hosts the application. In a serverless environment, the cloud provider automatically handles server provisioning, scaling, and management.
This allows developers to focus on writing the application code rather than worrying about the underlying infrastructure, resulting in faster development cycles and more scalable solutions.
Cloud Functions: The Event-Driven Powerhouse
Google Cloud Functions is an event-driven serverless compute service that allows you to run your code in response to events, such as HTTP requests, file uploads to Cloud Storage, or messages sent to a Pub/Sub topic.
How Cloud Functions Work
With Cloud Functions, you write a function to handle an event, deploy it to GCP, and let the cloud handle the rest. The function is triggered by specific events, and you are only billed for the time your function is actually running. This makes Cloud Functions an ideal solution for scenarios that require lightweight, event-driven processing.
Real-World Use Case: Image Processing
Consider an application that lets users upload images. Once an image is uploaded to Cloud Storage, a Cloud Function can be triggered to process the image, resize it, or apply filters. After processing, the image can be stored in a different Cloud Storage bucket or sent via an HTTP response to the user.
Cloud Run: Building Containerized, Scalable Applications
Google Cloud Run is a fully managed service that allows you to deploy containerized applications in a serverless environment. With Cloud Run, you can run applications in a stateless, scalable manner with automatic load balancing.
How Cloud Run Works
Unlike Cloud Functions, which executes code in response to events, Cloud Run lets you deploy applications packaged as Docker containers. These containers can be written in any language or framework, giving you flexibility in how you build your applications. Cloud Run also scales automatically based on incoming traffic, meaning it can handle both small-scale applications and large, high-traffic systems without any manual intervention.
Real-World Use Case: REST API Service
Let’s say you need to build a REST API to handle user authentication and profile management for a mobile application. Using Cloud Run, you can containerize the API, deploy it to GCP, and rely on automatic scaling to handle traffic spikes. Cloud Run will also take care of provisioning and managing the underlying infrastructure.
Benefits of Serverless on GCP
Serverless architecture on GCP, through Cloud Functions and Cloud Run, provides several benefits that make it highly attractive for modern application development:
Cost Efficiency: With both Cloud Functions and Cloud Run, you are billed based on the actual usage rather than the infrastructure you're using. This pay-as-you-go model ensures that you only pay for what you use, which is perfect for applications with fluctuating demand.
Scalability: Both Cloud Functions and Cloud Run scale automatically with incoming traffic. No matter how large or small the traffic is, the cloud services adjust resources dynamically, ensuring high performance.
Reduced Time-to-Market: Serverless architecture allows developers to focus on writing the application code, while the cloud provider handles infrastructure management. This leads to faster development cycles and quicker deployment of new features or updates.
Managed Infrastructure: One of the key benefits is that developers don’t have to worry about provisioning servers, configuring load balancers, or managing databases. GCP handles the underlying infrastructure, allowing you to focus on delivering value through your application.
Step-by-Step Guide: Building a Serverless Application on GCP
In this section, let’s walk through the steps to build a simple serverless application using both Cloud Functions and Cloud Run. We’ll create a basic image resizing service.
Step 1: Set Up Google Cloud Platform
Before you begin, make sure you have a GCP account and the necessary permissions to create resources. If not already done, create a project in the Google Cloud Console.
Go to the Google Cloud Console.
Create a new project and ensure that billing is enabled.
Enable the Cloud Functions and Cloud Run APIs for your project.
Step 2: Create a Cloud Function for Image Processing
We’ll start by creating a Cloud Function that will be triggered when a new image is uploaded to Cloud Storage.
Go to the Cloud Functions section in the GCP Console.
Click “Create Function”.
Set the trigger to be a Cloud Storage event. Choose a bucket where images will be uploaded.
Write the function code (for example, using Python or Node.js) that will resize the uploaded image. You can use libraries like Pillow (Python) or Sharp (Node.js) for image manipulation.
Example Python code:
from google.cloud import storage
from PIL import Image
import os
def resize_image(event, context):
storage_client = storage.Client()
bucket_name = event['bucket']
file_name = event['name']
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(file_name)
temp_file = "/tmp/temp_image.jpg"
blob.download_to_filename(temp_file)
image = Image.open(temp_file)
image = image.resize((100, 100))
new_blob = bucket.blob(f"resized-{file_name}")
image.save(new_blob)
print(f"Resized image saved to {new_blob.name}")
Deploy the function, and it will automatically trigger when an image is uploaded to the specified Cloud Storage bucket.
Step 3: Create a REST API Using Cloud Run

Now let’s create a REST API for our image resizing service using Cloud Run.
First, create a Dockerfile to containerize your image processing logic. The Dockerfile is something like this:
FROM python:3.8-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Next, build the Docker image and push it to Google Container Registry (GCR).
In GCP Console, navigate to Cloud Run, and click “Create Service”.
Select your container image from GCR and configure the service to allow unauthenticated access (if required).
Deploy the service, and Cloud Run will automatically handle the scaling based on incoming API requests.
Step 4: Test the Application
Once both the Cloud Function and Cloud Run services are deployed:
Upload an image to the Cloud Storage bucket.
The Cloud Function will trigger and resize the image.
You can now call the REST API hosted on Cloud Run to interact with the resized images, like fetching metadata or performing additional operations.
The Future of Serverless with GCP
Serverless architecture, when implemented with Google Cloud’s Cloud Functions and Cloud Run, offers a powerful, scalable, and cost-effective way to build modern applications. Whether you're building event-driven microservices or a full-fledged API, these services provide the flexibility to focus on code while leaving the complexity of infrastructure management to Google.
With automatic scaling, reduced operational overhead, and efficient cost structures, GCP’s serverless services empower developers to create high-performing, cost-effective applications in a fraction of the time it would take with traditional architectures.
Disclaimer:
This blog post is for informational purposes only. The architecture, code examples, and recommendations provided here are based on the latest available information at the time of writing. Please ensure to consult official GCP documentation and adapt the solutions according to your specific requirements. The author is not responsible for any inaccuracies or changes in the tools, services, or procedures described.
References:
This blog post should serve as a practical guide to help you get started with serverless architecture on Google Cloud Platform. Whether you’re building a small project or a large-scale application, the tools discussed here are invaluable for developers looking to optimize their workflows and minimize infrastructure complexity.
Comments