Quickstart

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 Projects 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. Install the gcloud tool's beta commands component:
    gcloud components install beta

Create a function

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

    • Linux or Mac OS X:

      mkdir ~/gcf_hello_world
      cd ~/gcf_hello_world
      

    • Windows:

      mkdir %HOMEPATH%\gcf_hello_world
      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} 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:

    status: READY
    triggers:
    - pubsubTopic: projects/[PROJECT_ID]/topics/hello_world
    

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:

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  -               2016-03-17 20:35:41.043  User function helloWorld loaded
D      helloWorld  eTe2m2-RVmJU-0  2016-03-17 20:45:18.334  User function triggered, starting execution
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  Execution took 2 ms, user function completed successfully

What's next

Send feedback about...

Cloud Functions Documentation