Create and deploy a HTTP Cloud Function by using Python
This guide takes you through the process of writing a Cloud Function using the Python runtime. There are two types of Cloud Functions:
- An HTTP function, which you invoke from standard HTTP requests.
- An event-driven function, which you use to handle events from your Cloud infrastructure, such as messages on a Cloud Pub/Sub topic, or changes in a Cloud Storage bucket.
The sample shows how to create a simple HTTP function.
Guide structure
- Creating a GCP project using gcloud CLI
- Creating a function
- Specifying dependencies
- Deploying your function
- Testing your function
Creating a GCP project using gcloud CLI
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Cloud Functions and Cloud Build APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Cloud Functions and Cloud Build APIs.
- Install and initialize the gcloud CLI.
- Update and install
gcloud
components:gcloud components update
- Prepare your development environment.
Creating a function
Create a directory on your local system for the function code:
Linux or Mac OS X
mkdir ~/helloworld cd ~/helloworld
Windows
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworld
Create a
main.py
file in thehelloworld
directory with the following contents:This example function takes a name supplied in the HTTP request and returns a greeting, or "Hello World!" when no name is supplied.
Specifying dependencies
Dependencies in Python are managed with pip
and expressed in a metadata file called
requirements.txt
.
This file must be in the same directory as the main.py
file that contains
your function code.
You don't need to create a requirements.txt
to run this particular sample,
but suppose you wanted to add your own dependencies. Here's how you would do it:
Create a
requirements.txt
file in thehelloworld
directory.Add the function's dependency, to your
requirements.txt
file, for example:# An example requirements file, add your dependencies below sampleproject==2.0.0
Deploying the function
To deploy the function with an HTTP trigger, run the following
command in the helloworld
directory:
gcloud functions deploy hello_http --runtime python311 --trigger-http --allow-unauthenticated
The --allow-unauthenticated
flag lets you reach the function
without authentication.
To require
authentication, omit the
flag.
Testing the function
When the function finishes deploying, take note of the
httpsTrigger.url
property or find it using the following command:gcloud functions describe hello_http
It should look like this:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http
Visit this URL in your browser. You should see a "Hello World!" message.
Try passing a name in the HTTP request, for example by using the following URL:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http?name=NAME
You should see the message "Hello
NAME
!"
Viewing logs
Using the command-line tool
Logs for Cloud Functions are viewable in the Cloud Logging UI, and via the Google Cloud CLI.
To view logs for your function with the gcloud CLI, use the
logs read
command, followed by
the name of the function:
gcloud functions logs read hello_http
The output should resemble the following:
LEVEL NAME EXECUTION_ID TIME_UTC LOG D hello_http pdb5ys2t022n 2019-09-18 23:29:09.791 Function execution started D hello_http pdb5ys2t022n 2019-09-18 23:29:09.798 Function execution took 7 ms, finished with status code: 200
Using the Logging dashboard
You can also view logs for Cloud Functions from the Google Cloud console.