Quickstart: Using the gcloud Command-Line Tool

This page shows you how to create and deploy a Cloud Function using the gcloud command-line tool.

Before you begin

  1. Google アカウントへのログイン

    Google アカウントをまだお持ちでない場合は、新しいアカウントを登録します。

  2. Cloud Platform Console プロジェクトを選択または作成します。

    [プロジェクト] ページに移動

  3. プロジェクトの課金を有効にします。

    課金の有効化

  4. Cloud Functions API を有効にする。

    Enable the API

    API を有効にすると、認証情報を得る必要はありません。

  5. Google Cloud SDK をインストールし、初期化します
  6. gcloud コンポーネントを更新し、インストールします。
    gcloud components update beta &&
    gcloud components install
  7. Need a command prompt? You can use the Google Cloud Shell. The Google Cloud Shell is a command line environment that already includes the Google Cloud SDK, so you don't need to install it. (The Google Cloud SDK also comes preinstalled on Google Compute Engine Virtual Machines.)

  8. Prepare your environment for Node.js development.

    Go to the setup guide

Create a function

  1. Create a directory on your local system for the function code:

    Linux or Mac OS X

    Create the directory:

    mkdir ~/gcf_hello_world

    Move into the directory:

    cd ~/gcf_hello_world

    Windows

    Create the directory:

    mkdir %HOMEPATH%\gcf_hello_world

    Move into the directory:

    cd %HOMEPATH%\gcf_hello_world

  2. Create an index.js file in the gcf_hello_world directory with the following contents. This is a simple function named helloGET that responds to GET requests via HTTP with the text "Hello World!".

    Node.js

    /**
     * HTTP Cloud Function.
     *
     * @param {Object} req Cloud Function request context.
     * @param {Object} res Cloud Function response context.
     */
    exports.helloGET = function helloGET (req, res) {
      res.send('Hello World!');
    };

Deploy a function

Deploy the function using the following command. Note that it might take a few minutes to finish.

gcloud beta functions deploy helloGET --trigger-http

Test the function

  1. When the function finishes deploying, take note of the httpsTrigger's url property or find it using the following command:

    gcloud beta functions describe helloGET
    It should look like this: https://[GCP_REGION]-[PROJECT_ID].cloudfunctions.net/helloGET.

  2. Visit this URL in your browser. You should see a Hello World! message.

What's next

フィードバックを送信...

Cloud Functions Documentation