HTTP Tutorial

This simple tutorial demonstrates writing, deploying, and triggering an HTTP Cloud Function.

Objectives

Costs

This tutorial uses billable components of Cloud Platform, including:

  • Google Cloud Functions

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Enable the Cloud Functions API.

    Enable the API

  5. Update and install gcloud components:

    Node.js 6

    gcloud components update

    Node.js 8 (Beta)

    gcloud components update &&
    gcloud components install beta

    Python (Beta)

    gcloud components update &&
    gcloud components install beta
  6. Prepare your development environment.

Preparing the application

  1. Clone the sample app repository to your local machine:

    Node.js

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    Python (Beta)

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

  2. Change to the directory that contains the Cloud Functions sample code:

    Node.js

    cd nodejs-docs-samples/functions/helloworld/

    Python (Beta)

    cd python-docs-samples/functions/helloworld/

  3. Take a look at the sample code:

    Node.js

    /**
     * HTTP Cloud Function.
     * This function is exported by index.js, and is executed when
     * you make an HTTP request to the deployed function's endpoint.
     *
     * @param {Object} req Cloud Function request context.
     *                     More info: https://expressjs.com/en/api.html#req
     * @param {Object} res Cloud Function response context.
     *                     More info: https://expressjs.com/en/api.html#res
     */
    exports.helloGET = (req, res) => {
      res.send('Hello World!');
    };

    Python (Beta)

    def hello_get(request):
        """HTTP Cloud Function.
        Args:
            request (flask.Request): The request object.
            <http://flask.pocoo.org/docs/0.12/api/#flask.Request>
        Returns:
            The response text, or any set of values that can be turned into a
            Response object using `make_response`
            <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>.
        """
        return 'Hello, World!'

Deploying the function

To deploy the function with an HTTP trigger, run the following command in the helloworld directory:

Node.js 6

gcloud functions deploy helloGET --runtime nodejs6 --trigger-http

Node.js 8 (Beta)

gcloud functions deploy helloGET --runtime nodejs8 --trigger-http

Python (Beta)

gcloud functions deploy hello_get --runtime python37 --trigger-http

Triggering the function

To make an HTTP request to your function, run the following command:

Node.js

curl "https://REGION-PROJECT_ID.cloudfunctions.net/helloGET" 

Python (Beta)

curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get" 

where

  • REGION is the region where your function is deployed. This is visible in your terminal when your function finishes deploying.
  • PROJECT_ID is your Cloud project ID. This is visible in your terminal when your function finishes deploying.

You can also visit the deployed function's endpoint in your browser to see the "Hello World!" message.

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the GCP Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting the Cloud Function

Deleting Cloud Functions does not remove any resources stored in Cloud Storage.

To delete the Cloud Function you created in this tutorial, run the following command:

Node.js

gcloud functions delete helloGET 

Python (Beta)

gcloud functions delete hello_get 

You can also delete Cloud Functions from the Google Cloud Platform Console.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Functions Documentation