DEV Community

Cover image for Serverless Automation: Scheduling Python Scripts with Google Cloud
Ayush Kumar
Ayush Kumar

Posted on • Originally published at logiclooptech.dev

Serverless Automation: Scheduling Python Scripts with Google Cloud

In Part 1, we set up the credentials. In Part 2, we wrote the Python code to send a notification.

But there is a problem: The code only runs when you manually type python main.py on your laptop.

If you want to send a "Good Morning" notification to your users at 9:00 AM every day, you can't rely on your laptop being open. You need a server.

But buying a VPS (Virtual Private Server) just to run a 10-line script once a day is a waste of money. This is where Serverless shines.

In this final guide, we will deploy our Python logic to Google Cloud Functions and trigger it with Cloud Scheduler.

The Architecture

We are building a "Cron Job" in the cloud:

  1. Cloud Scheduler: The Alarm Clock (Triggers at 9:00 AM).

  2. Pub/Sub: The Messenger (Passes the signal).

  3. Cloud Function: The Worker (Executes our Python code).


Step 1: Prepare the Code for the Cloud

Cloud Functions work slightly differently than local scripts. They don't use if __name__ == "__main__":. Instead, they wait for an event.

We need two files: main.py and requirements.txt.

1. requirements.txt This tells Google which libraries to install.

Plaintext

firebase-admin==6.2.0
functions-framework==3.0.0
Enter fullscreen mode Exit fullscreen mode

2. main.py Notice how we removed the local certificate file path. In Google Cloud, the environment authenticates automatically!

Python

import firebase_admin
from firebase_admin import credentials, messaging

# Initialize Firebase without a manual JSON file
# Google Cloud Functions authenticates automatically!
if not firebase_admin._apps:
    firebase_admin.initialize_app()

def send_daily_notification(event, context):
    """
    Triggered from a message on a Cloud Pub/Sub topic.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    print(f"Function triggered by event: {event}")

    # Define the message
    message = messaging.Message(
        notification=messaging.Notification(
            title='Rise and Shine! ☀️',
            body='This is your automated daily update.',
        ),
        topic='news', # Target the 'news' topic
    )

    # Send it
    try:
        response = messaging.send(message)
        print(f"Successfully sent message: {response}")
        return "Done"
    except Exception as e:
        print(f"Error: {e}")
        return "Failed"
Enter fullscreen mode Exit fullscreen mode

Step 2: Deploy to Cloud Functions

  1. Go to the Google Cloud Console and search for "Cloud Functions".

  2. Click Create Function.

  3. Basics:

* [**Envir**](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)**onmen**[**t:** 1st](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3) [](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)Gen (simpler for thi[s tuto](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)rial)[.](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)

* [**F**](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)**unction N**[**ame:** `d`](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)`aily-notification-sender`.

* **Reg**[**ion:** C](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)h[oose o](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)ne close to you (e.g., `us-centra`[`l1` or](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3) `asia-south1`).
Enter fullscreen mode Exit fullscreen mode
  1. Trigger:
* [Sele](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)ct **Clo**[**ud Pub**](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)**/Sub**.

* Click **Crea**[**te a t**](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)[**o**](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)**pic** an[d name](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3) it `daily-trigger`.

* [Cli](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)ck **Save**[.](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)
Enter fullscreen mode Exit fullscreen mode

[IMAGE: Screenshot of the Trigger selection screen in GCP Console]

  1. Runtime:
* [Clic](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)k **Next**[.](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)

* [S](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)elect **Pyt**[**hon 3.**](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)**10** (o[r 3.9)](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3) as th[e runt](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)ime.

* C[opy an](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)d p[aste o](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)ur [`main.py`](http://main.py) and `requirements.txt` [code i](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)nto the inline editor.

* **Entry Poin**[**t:** Typ](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)e `send_daily_notification` (this [must m](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)atch your python function name).
Enter fullscreen mode Exit fullscreen mode
  1. Click Deploy.

It will take about 2 minutes. Once the green checkmark appears, your code is live on Google's infrastructure!


Step 3: Set the Schedule (The Alarm Clock)

Now we need something to "poke" that Pub/Sub topic every day.

  1. Search for "Cloud Scheduler" in the console.

  2. Click Create Job.

  3. Configuration:

* **Name:** `mor`[`n`](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)[`ing-tr`](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)`igger`.

* **Freq**[**uency:**](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3) [`0 9 *`](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3) `* *` ([This i](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)s Cron syntax for "E[very d](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)ay at 9:00 AM").

* **Timezone:** [Select](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3) your local timezone.
Enter fullscreen mode Exit fullscreen mode
  1. Target:
* [**Targe**](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)**t Typ**[**e:** Pub](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)/Sub.

* **Topic:** Sel[ect th](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)[e](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3) `daily`[`-trigg`](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)`er` topic we created [earli](https://www.google.com/search?q=https://logiclooptech.dev/send-fcm-notifications-python&authuser=3)er.

* **Message Bo**[**dy:** Ty](https://www.google.com/search?q=https://logiclooptech.dev/setup-firebase-fcm-account&authuser=3)pe anything (e.g., "Wake up").
Enter fullscreen mode Exit fullscreen mode
  1. Click Create.

Step 4: Test It

You don't have to wait until 9 AM to test it.

  1. In the Cloud Scheduler list, find your job.

  2. Click the "Force Run" (or "Run Now") button.

  3. Check your phone (if you have an app connected) or check the Cloud Functions Logs. You should see "Successfully sent message."

Conclusion

You have just built a completely Serverless Notification Engine.

  • Cost: $0.00 (until you hit millions of invocations).

  • Maintenance: None. No OS updates, no server restarts.

  • Scalability: Google handles it automatically.

This is the power of modern Backend Engineering. You moved from a script on a laptop to a production-grade cloud architecture.

Top comments (0)