HTTP Tutorial

This simple tutorial demonstrates writing, deploying, and triggering an HTTP Cloud Function.

Objectives

Costs

This tutorial uses billable components of Cloud Platform, including:

  • Google Cloud Functions

Use the Pricing Calculator to generate a cost estimate based on your projected usage.

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. Update and install gcloud components:
    gcloud components update beta &&
    gcloud components install

Preparing the application

  1. Create a Cloud Storage bucket to stage your Cloud Functions files, where [YOUR_STAGING_BUCKET_NAME] is a globally-unique bucket name:

    gsutil mb gs://[YOUR_STAGING_BUCKET_NAME]

  2. Create a directory on your local system for the application code:

    • Linux or Mac OS X:

      mkdir ~/gcf_http
      cd ~/gcf_http
      
    • Windows

      mkdir %HOMEPATH%\gcf_http
      cd %HOMEPATH%\gcf_http
      
  3. Create an index.js file in the gcf_http directory with the following contents:

    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!');
    };

    The helloGET function is exported by the module and is executed when you make an HTTP request to the function's endpoint.

Deploying the function

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

gcloud beta functions deploy helloGET --stage-bucket [YOUR_STAGING_BUCKET_NAME] --trigger-http

where [YOUR_STAGING_BUCKET_NAME] is the name of your staging Cloud Storage Bucket.

Triggering the function

Make an HTTP request to your function:

curl "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/helloGET"

where

  • [YOUR_REGION] is the region where your function is deployed. This is visible in your terminal when your function finishes deploying.
  • [YOUR_PROJECT_ID] is your Cloud project ID. This is visible in your terminal when your function finishes deploying.

You can also visit the function's endpoint in your browser to see the "Hello World!" message.

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting the Cloud Functions

Deleting the Cloud Functions also removes the resources they use.

Delete a Cloud Function:

gcloud beta functions delete helloGET

You can also delete Cloud Functions from the Google Cloud Platform Console.

Send feedback about...

Cloud Functions Documentation