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 GCP project.

    Go to the Manage resources page

  3. Verifica che la fatturazione sia attivata per il tuo progetto.

    scopri come attivare la fatturazione

  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 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.

  2. Change to the directory that contains the Cloud Functions sample code:

    Node.js

    cd nodejs-docs-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.
     * @param {Object} res Cloud Function response context.
     */
    exports.helloGET = (req, res) => {
      res.send('Hello World!');
    };

Deploy a function

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

Node.js

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:

    Node.js

    gcloud beta functions describe helloGET

    It should look like this:

    Node.js

    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

Hai trovato utile questa pagina? Facci sapere cosa ne pensi:

Invia feedback per...

Cloud Functions Documentation