How to Use Google Cloud Function with Python ?
Last Updated :
03 Mar, 2025
Google Cloud Functions provides a way to run small pieces of code in response to cloud events without managing servers. If you're a developer looking to automate tasks, process data or build APIs, Python is a great language for working with Google Cloud Functions.
In this article, we will look into how to use the Google Cloud Function with python on any website.
Use of Google Cloud Functions
Here’s why Google Cloud Functions can be useful:
- No Server Management – Deploy code without worrying about infrastructure.
- Event-driven – Runs automatically when an event occurs, such as an HTTP request or a file upload.
- Scales Automatically – Handles increased workload without manual intervention.
- Cost-Effective – You only pay for the execution time and resources used.
- Seamless Google Cloud Integration – Works well with services like Firebase, BigQuery, and Cloud Storage.
What you need before getting started?
Before deploying your Python function, make sure you have:
- A Google Cloud Platform (GCP) account.
- The gcloud CLI installed on your system.
- Python 3.8 or later installed.
- A Google Cloud project with billing enabled.
To do so follow the below steps:
Step 1: Let's first head to the functions manager site on Google Cloud Platform (GCP). It should look like below:
Function manager siteStep 2: Now let's create our function. Click the "CREATE FUNCTION" on the top.
Create FunctionStep 3: A window like this one should appear next. Here in the function name, give any name. And if you are not going to use any authentication, choose "Allow unauthenticated invocations". For this particular example at least, we will choose "Allow unauthenticated invocations". Other options are not required to be changed. Now hit the "NEXT" button.
Allow unauthenticated invocationsStep 4: Now you will get a Trigger URL. Copy this URL somewhere as this is the URL we will use to invoke our function. Then hit "NEXT".
Trigger URLStep 5: Now under "Runtime", select a Python version. We have selected "Python 3.8". Now here comes the main part, the SOURCE CODE of the function. Right now if you deploy the function, it most probably won't work because, in the source code, there is no "Access Control" header. This means when you will invoke this function from a browser like Firefox or Chrome, it won't work. So let's use our own source code.
RuntimeThe request_json = request.get_json() extracts the JSON object sent by the client. The next code specifies the access scope of the user. The next code line checks whether there is a "Message" header in the headers. If yes, then send its value and the retrieved JSON, as a response to the client. If not, then send the entire header content along with any JSON retrieved back to the client. That's it for the source code.
But before we test the function, here are a few things. The "Entry point" beside the "Runtime" shows the method which will be invoked when a request has been received. Under the source code section, you will also see the requirements.txt file. This file is used to specify additional dependencies for your function. For additional queries regarding requirements.txt, you may refer to this documentation.
Step 6: Now let's hit "DEPLOY" to deploy the function. Now you will again be redirected to the functions manager site. It should take about 1-2 minutes for the function to be deployed.
DeployNow, let's test our function.
Step 7: Let's create a test.html file from where we can call our function using JavaScript.
test.htmlIn the "Your trigger URL" on line 14, paste the trigger URL you copied before. We set a header "Content-Type" to tell our function, what kind of data we have sent. In our case, it is JSON. Next, we set a header named "Message" just for testing purposes. Then we specify what to do when the response has been received. And at last, send some dummy data as JSON. Now let's run the below given HTML file in a browser.
HTML
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>GCP Function</title>
</head>
<body>
<p id="p">Processing...</p>
<script>
function httpGet()
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", "Your trigger URL");
xmlHttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlHttp.setRequestHeader("message", "Header: This is head");
xmlHttp.onload = function () {
document.getElementById("p").innerHTML=xmlHttp.responseText;
};
xmlHttp.send(JSON.stringify({"Body": "This is body"}));
}
httpGet();
</script>
</body>
</html>
HeaderAnd bravo! Here is the response! Everything is up and running. But what will happen if you code wrong in the source code of the function? Google is there to the rescue.
Rescue to wrong codeClick the function you created. In the next window, select the "LOGS" tab to see the logs of your function.
LogsAdvanced Use Cases
- Cloud Storage Integration – Automatically process uploaded files.
- Firestore Triggers – Execute functions when database entries are modified.
- Building APIs – Use Flask or FastAPI to create lightweight APIs.
Conclusion
Google Cloud Functions with Python is a powerful tool for automating tasks and building cloud applications. This guide walked you through the setup and deployment process in a simple, easy-to-follow manner.
Similar Reads
Google Cloud Platform Tutorial Google Cloud Platform (GCP) is a set of cloud services provided by Google, built on the same technology that powers Google services like Search, Gmail, YouTube, Google Docs, and Google Drive. Many companies prefer GCP because it can be up to 20% cheaper for storing data and databases compared to oth
8 min read
Introduction
What is Google Cloud Platform (GCP)?Google Cloud Platform (GCP) is a cloud computing service by Google that helps businesses, developers, and enterprises run applications, store data, and manage workloads on a secure, scalable, and high-performance infrastructure. Whether you're building a website, handling large datasets, or running
15+ min read
Introduction to Google Cloud PlatformGoogle Cloud Platform (GCP) is an initiative by Google to provide cloud computing services to customers. These services run on the same infrastructure and platform on which Google services such as Gmail, YouTube, etc run. GCP was launched on April 7, 2008, and the complete set of services and the pl
5 min read
Cloud Storage in Google Cloud Platform (GCP)Google Cloud Storage is a secure, scalable, and high-performance storage solution that lets businesses store, manage, and retrieve data effortlessly. Itâs designed for big data analytics, media storage, backups, and disaster recovery, making it a go-to option for enterprises looking for cost-effecti
8 min read
Features of Google Cloud PlatformGoogle Cloud Platform (GCP) is Googleâs cloud computing service that helps businesses build, deploy, and scale applications on a secure, global infrastructure. It offers powerful features like virtual machines, cloud storage, databases, AI, machine learning, and big data tools. GCP reduces infrastru
5 min read
Google Cloud Platform - Introduction to QwiklabsQwiklabs provides lab learning environments that help developers and IT professionals get hands-on experience working with leading cloud platforms and software. Qwiklabs provides temporary credentials to Google Cloud Platform and Amazon Web Services so that you can get a real-life experience by work
3 min read
Compute Services
Storage and Database Services
Networking Services
Security Services
Google Cloud Platform SecurityCloud computing is now the backbone of apps, services, and businesses we use dailyâGmail and Google Docs to large enterprise systems. At its core is Google Cloud Platform (GCP), a robust cloud service used by startups, global enterprises, and governments. Great power, however, brings great responsib
15+ min read
Access Control for Disaster Avoidance in Google Cloud IoT Core using IAM PolicyInternet of Things(IoT) is today's one of the most used technologies to establish the network between physical devices. In the case of the Cloud IoT, the cloud technology has added extra value by providing massive support to the modern IoT automation to make it more secure, managed, scalable and so
4 min read
Data Integration and Analytics Services
Introduction to DatabricksDatabricks is a cloud-based platform for managing and analyzing large datasets using the Apache Spark open-source big data processing engine. It offers a unified workspace for data scientists, engineers, and business analysts to collaborate, develop, and deploy data-driven applications. Databricks i
5 min read
Google Cloud Platform - Introduction to BigQueryGoogle BigQuery is a fully managed, serverless data warehouse designed to help businesses store and analyze large volumes of data quickly and efficiently. Whether you're dealing with massive datasets or real-time analytics, BigQuery allows you to run complex queries and get insights in seconds witho
8 min read
Google Cloud Platform - Introduction to BigQuery SandboxBigQuery sandbox gives you free access to try out BigQuery and use the UI without providing a credit card or using a billing account. It's a quick way to get started and try out some BigQuery concepts. To get started, click on this link and follow along with the rest of the article. If you're a new
2 min read
Google Cloud Platform - Tables in BigQueryTables in BigQuery or any database for that matter is used to store data in a structured manner. In this article, we will explore the concepts of the three types of table available in BigQuery: Temporary TablesPermanent TablesViews (Virtual Tables)Temporary Tables: Just as BigQuery automatically sav
3 min read
Google Cloud Platform- BigQuery(Running Queries, advantage and disadvantage)In this article, we're going to look into how to run a query in BigQuery. Running queries is one of the most fundamental parts of discovering insights from your data. So let's ask an outrageous question to BigQuery here and ask it "what is the best jersey number you should choose in order to improve
7 min read
Google Cloud Platform - User Defined Functions in BigQuerySQL has many built-in functions for performing calculations on data. But sometimes, your systems might need to handle data, such as string or date values, uniquely. User-defined functions are an efficient way to have these custom calculations at your fingertips when analyzing data. In this article,
4 min read
Google Cloud Platform - Working with External Data in BigQueryIn BigQuery it's also possible to query data stored externally or outside BigQuery. In this article, we're diving into these external data sources. It's possible to leave your data in any place and use BigQuery as your query engine. These sources are called external or federated data sources. This f
4 min read
Google Cloud Platform - Loading Data to BigQueryIn this article, we will look into how to load and analyze your own data in BigQuery. As it is better to understand the concept with examples, we will be answering the age-old question "Which is better, cats or dogs?" If you want to analyze data that are not already available as part of the public d
5 min read
Google Cloud Platform - Implementing Authorized View in BigQueryIn this article, we will look into how you can implement an Authorized view in BigQuery.You can follow along in your own BigQuery sandbox, which you can set up for free. For this, we're using two sandboxes in order to represent the perspectives of the data admin. As a data admin follow the below ste
3 min read
Google Cloud Platform - Query History vs Saved Query vs Shared Query in BigQueryThe process of writing and running SQL queries doesn't always follow a straight line. A particular query can be in constant iteration while you use it to explore and clean up your data, or as you fine-tune it to optimize its performance. In this article, we will highlight the ways to save and share
3 min read
Google Cloud Platform - Managing Access using IAM in BigQueryWhile big data brings us valuable insights and opportunities, it also brings the responsibility to ensure that data is secure, meaning that only the right data is shared with the right people. In this article, we're talking about how to use Google Cloud's Identity and Access Management Service to de
5 min read
Google Cloud Platform - Data Visualization in BigQueryWhether you're exploring a data set for the first time or summarizing the findings of your analysis to an audience, you can use data visualization to make large, complex data sets easier to understand and internalize. In this article, we will look into visualizing your BigQuery data. Data visualizat
4 min read
Google Cloud Platform - Data Security in BigQueryOne of the benefits of a data warehouse, like BigQuery, is the improved simplicity and speed of bringing data to your analysts and decision-makers. Data needs to vary across a company based on organizational function, geography, and more, so it's important to be able to provide customized access to
3 min read
Management tools and Monitoring Services
GCP DevOps
Miscellaneous
Difference Between Google Cloud and AWSGoogle Cloud Platform: It is a suite of cloud computing services developed by Google and launched publicly in 2008. Google Cloud Platform provides IaaS, PaaS, and serverless computing environments. A comparatively new Google Cloud Platform has all the tools and services required by developers and pr
3 min read
How To Share File From Host Machine(Windows) To Guest Machine(Linux)We need to have Ubuntu installed in our Virtual Box for the purpose of this experiment. The host machine is Windows 10 in the following experiment. Transfer File From Host Machine(Windows) To Guest Machine(Linux) 1. Method 1: Installing SSH on Ubuntu Terminal and allowing Firewall blockage Open Term
4 min read
Deployment Models in OpenStackPre-requisite: OpenStack OpenStack has a set of software tools for providing various cloud computing platforms for public and private clouds. OpenStack is managed by the OpenStack Foundation, a non-profit that oversees both development and community-building around that project. OpenStack is the fut
4 min read
How to Build G Suite Add-ons with Google Apps script?G Suite is a Google service that provides access to a core set of applications like Gmail, Calendar, Drive, Docs, Sheets, Slides, Forms, Meet, etc. Add-ons means the extension given to the pre-existing G Suite products (mentioned above). Developers can add many extra features to such products. Add-o
3 min read
Google Cloud Platform - Introduction to PhoneInfoga an OSINT Reconnaissance ToolPhoneInfoga is one of the most advanced tools which one can use to scan phone numbers and get detailed information about them using only free resources. The motive is to gather basic information such as country, area, line, and carrier on any international phone numbers with very good accuracy. Then
3 min read
Generating API Keys For Using Any Google APIsLike most software giants, Google provides its enthusiastic developers community with its APIs, SDKs and Services. These APIs from Google are hosted on their cloud platform, popularly known as Google Cloud Platform (GCP). Software such as Google Maps, YouTube, Gmail, etc., use the same APIs and now
3 min read
Google Cloud Platform - Understanding Federated Learning on CloudCrowdsourcing has a wide range of benefits. Whether it's restaurant reviews that help us find a perfect place for dinner or crowdfunding to bring our favorite TV show back to life, these distributed contributions combined to make some super useful tools. We can also use that same concept to build be
3 min read