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:
-
Pub/Sub: The Messenger (Passes the signal).
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.0functions-framework==3.0.02. main.py Notice how we removed the local certificate file path. In Google Cloud, the environment authenticates automatically!
Python
import firebase_adminfrom 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"Step 2: Deploy to Cloud Functions
-
Go to the Google Cloud Console and search for “Cloud Functions”.
-
Click Create Function.
-
Basics:
-
Trigger:
[IMAGE: Screenshot of the Trigger selection screen in GCP Console]
-
Runtime:
-
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.
Step 4: Test It
You don’t have to wait until 9 AM to test it.
-
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.
-
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.
Working on something similar?
If you're building backend or AI systems and want a second set of senior eyes, let's talk.
