Modern Serverless Deployment: Orchestrating Python on Google Cloud

Overview

Modern serverless development allows engineers to execute logic in the cloud without managing the underlying hardware. This guide focuses on

, specifically the second-generation
Cloud Run functions
. We use
Infrastructure as Code
to ensure deployments are reproducible, secure, and version-controlled. By moving away from manual dashboard clicks, you treat your infrastructure as a living part of your codebase.

Modern Serverless Deployment: Orchestrating Python on Google Cloud
Serverless on Google Cloud: What You Can and Can’t Do

Prerequisites

To follow this guide, you should have a baseline understanding of

and
REST API
principles. You will need a
Google
account with an active billing project and the
gcloud CLI
initialized. Knowledge of virtual environments is essential, specifically using modern tools like
uv
for dependency management.

Key Libraries & Tools

  • Pulumi
    :
    An IaC tool that uses real programming languages to define cloud resources.
  • Functions Framework
    :
    An open-source library to run functions locally for testing.
  • Flask
    :
    The underlying web framework for Google’s Python runtime.
  • Secret Manager
    :
    A Google Cloud service for storing sensitive data like API keys.

Code Walkthrough

1. Local Testing

Before deploying, simulate the cloud environment locally using the Functions Framework.

import functions_framework

@functions_framework.http
def hello_world(request):
    return "The ruins of the old web are replaced by serverless logic."

Run this locally with functions-framework --target hello_world --port 8080 to verify your logic.

2. Infrastructure Definition with Pulumi

We define our bucket, function, and access policies in a __main__.py file. Pulumi handles the complex

permissions under the hood.

import pulumi
import pulumi_gcp as gcp

# Create a storage bucket for the function code
bucket = gcp.storage.Bucket("function-bucket")

# Upload the code as a zip file
archive = gcp.storage.BucketObject("code-archive",
    bucket=bucket.name,
    source=pulumi.FileArchive("./src"))

# Define the Cloud Function
function = gcp.cloudfunctionsv2.Function("my-function",
    build_config=gcp.cloudfunctionsv2.FunctionBuildConfigArgs(
        runtime="python311",
        entry_point="hello_world",
    ))

Syntax Notes

Google Cloud Functions natively use

. This means your entry point must accept a request object. Unlike
FastAPI
, there is no native async event loop running by default. If you require subroutes, you must manually dispatch requests using a mini-Flask app inside your function to simulate multiple endpoints.

Practical Examples

Serverless excels at event-driven tasks. Use these functions to process image uploads to

, handle
Pub/Sub
messages for data pipelines, or create simple webhooks for external integrations like
Stripe
or
GitHub
.

Tips & Gotchas

Avoid the "Admin Urge." It is tempting to grant your service account roles/editor just to make things work. Resist this. Assign minimal privileges to your

to prevent security breaches. Furthermore, be wary of Cold Starts. Functions with heavy dependencies take longer to initialize on the first request. Use
Cloud Run
if you need custom
Docker
images or consistent performance.

Modern Serverless Deployment: Orchestrating Python on Google Cloud

Fancy watching it?

Watch the full video and context

3 min read