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. 登入您的 Google 帳戶。

    如果您沒有帳戶,請申請新帳戶

  2. 選取或建立 Google Cloud Platform 專案。

    前往「Manage resources」(管理資源) 頁面

  3. 請確認您已啟用 Google Cloud Platform 專案的計費功能。

    瞭解如何啟用計費功能

  4. 啟用Cloud Functions API。

    啟用 API

  5. Update gcloud components:
    gcloud components update
  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

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

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

    Go

    git clone https://github.com/GoogleCloudPlatform/golang-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

    cd python-docs-samples/functions/helloworld/

    Go

    cd golang-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

    def hello_get(request):
        """HTTP Cloud Function.
        Args:
            request (flask.Request): The request object.
            <http://flask.pocoo.org/docs/1.0/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/1.0/api/#flask.Flask.make_response>.
        """
        return 'Hello World!'

    Go

    
    // Package helloworld provides a set of Cloud Functions samples.
    package helloworld
    
    import (
    	"fmt"
    	"net/http"
    )
    
    // HelloGet is an HTTP Cloud Function.
    func HelloGet(w http.ResponseWriter, r *http.Request) {
    	fmt.Fprint(w, "Hello, World!")
    }
    

Deploying the function

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

Node.js 8

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

Node.js 10 (Beta)

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

Node.js 6 (Deprecated)

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

Python

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

Go

gcloud functions deploy HelloGet --runtime go111 --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

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

Go

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

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 that you created for the tutorial.

To delete the project:

  1. 前往 GCP 主控台的「Projects」(專案) 頁面。

    前往專案頁面

  2. 在專案清單中選取要刪除的專案,然後按一下 [Delete] (刪除)
  3. 在對話方塊中輸入專案 ID,按一下 [Shut down] (關閉) 即可刪除專案。

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

gcloud functions delete hello_get 

Go

gcloud functions delete HelloGet 

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

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Functions Documentation