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. Sign in to your Google account.

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

  2. Select or create a Cloud Platform project.

    Go to the Manage resources page

  3. Enable billing for your project.

    Enable billing

  4. Enable the Cloud Functions API.

    Enable the API

  5. Install and initialize the Cloud SDK.
  6. Update and install gcloud components:
    gcloud components update &&
    gcloud components install beta
  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

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Cloud Functions Documentation