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:

    Node.js

    /**
     * Cloud Function.
     *
     * @param {object} event The Cloud Functions event.
     * @param {function} callback The callback function.
     */
    exports.helloWorld = function helloWorld (event, callback) {
      console.log(`My Cloud Function: ${event.data.message}`);
      callback();
    };

    This is a simple function named helloWorld that writes a message to the Cloud Functions logs.

Create a Cloud Storage bucket

Create a new Cloud Storage bucket for your function:

gsutil mb -p [PROJECT_ID] gs://[BUCKET_NAME]

where:

  • [PROJECT_ID] is the ID of your project
  • [BUCKET_NAME] is a globally-unique bucket name.

Deploy a function

  1. Deploy the function with a Pub/Sub topic named hello_world:

    gcloud beta functions deploy helloWorld --stage-bucket [BUCKET_NAME] --trigger-topic hello_world

    It might take a few minutes to deploy the function the first time.

    Note: [BUCKET_NAME] is the name of the bucket without the gs:// protocol identifier before the name.

  2. Verify the status of the function:

    gcloud beta functions describe helloWorld

    A READY status indicates that the function has been deployed:

    entryPoint: helloWorld
    eventTrigger:
      eventType: providers/cloud.pubsub/eventTypes/topic.publish
      resource: projects/[PROJECT_ID]/topics/hello_world
    ...
    status: READY
    ...
    

Test the function

Test the function from the command line:

Linux or Mac OS X

gcloud beta functions call helloWorld --data '{"message":"Hello World!"}'

Windows

gcloud beta functions call helloWorld --data "{\"message\":\"Hello World!\"}"

The gcloud tool returns the execution ID for the function, which looks something like this:

executionId: eTe2m2-RVmJU-0

View logs

Check the logs to see your actions in the log history:

gcloud beta functions logs read helloWorld

If the function executed successfully, messages in the log appear as follows:

LEVEL  NAME        EXECUTION_ID    TIME_UTC                 LOG
D      helloWorld  eTe2m2-RVmJU-0  2016-03-17 20:45:18.334  Function execution started
I      helloWorld  eTe2m2-RVmJU-0  2016-03-17 20:45:18.335  My Cloud Function: Hello World!
D      helloWorld  eTe2m2-RVmJU-0  2016-03-17 20:45:18.335  Function execution took 2 ms, finished with status: 'ok'

What's next

外出先でもリソースをモニタリング

Google Cloud Console アプリを入手して、プロジェクトの管理にお役立てください。

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

Cloud Functions Documentation