HTTP Tutorial


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

Objectives

Costs

In this document, you use the following billable components of Google Cloud:

  • Cloud Run functions

To generate a cost estimate based on your projected usage, use the pricing calculator. New Google Cloud users might be eligible for a free trial.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Functions and Cloud Build APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.
  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  8. Make sure that billing is enabled for your Google Cloud project.

  9. Enable the Cloud Functions and Cloud Build APIs.

    Enable the APIs

  10. Install the Google Cloud CLI.
  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. If you already have the gcloud CLI installed, update it by running the following command:

    gcloud components update
  13. 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.

    Python

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    Go

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    Java

    git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    C#

    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    Ruby

    git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git

    Alternatively, you can download the sample as a zip file and extract it.

    PHP

    git clone https://github.com/GoogleCloudPlatform/php-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 Run functions sample code:

    Node.js

    cd nodejs-docs-samples/functions/helloworld/

    Python

    cd python-docs-samples/functions/helloworld/

    Go

    cd golang-samples/functions/helloworld/

    Java

    cd java-docs-samples/functions/helloworld/helloworld/

    C#

    cd dotnet-docs-samples/functions/helloworld/HelloWorld/

    Ruby

    cd ruby-docs-samples/functions/helloworld/

    PHP

    cd php-docs-samples/functions/helloworld_get/

  3. Take a look at the sample code:

    Node.js

    const functions = require('@google-cloud/functions-framework');
    
    // Register an HTTP function with the Functions Framework that will be executed
    // when you make an HTTP request to the deployed function's endpoint.
    functions.http('helloGET', (req, res) => {
      res.send('Hello World!');
    });

    Python

    import functions_framework
    
    @functions_framework.http
    def hello_get(request):
        """HTTP Cloud Function.
        Args:
            request (flask.Request): The request object.
            <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
        Returns:
            The response text, or any set of values that can be turned into a
            Response object using `make_response`
            <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
        Note:
            For more information on how Flask integrates with Cloud
            Functions, see the `Writing HTTP functions` page.
            <https://cloud.google.com/functions/docs/writing/http#http_frameworks>
        """
        return "Hello World!"
    
    

    Go

    
    // Package helloworld provides a set of Cloud Functions samples.
    package helloworld
    
    import (
    	"fmt"
    	"net/http"
    )
    
    // HelloGet is an HTTP Cloud Function.
    func HelloGet(w http.ResponseWriter, r *http.Request) {
    	fmt.Fprint(w, "Hello, World!")
    }
    

    Java

    
    package functions;
    
    import com.google.cloud.functions.HttpFunction;
    import com.google.cloud.functions.HttpRequest;
    import com.google.cloud.functions.HttpResponse;
    import java.io.BufferedWriter;
    import java.io.IOException;
    
    public class HelloWorld implements HttpFunction {
      // Simple function to return "Hello World"
      @Override
      public void service(HttpRequest request, HttpResponse response)
          throws IOException {
        BufferedWriter writer = response.getWriter();
        writer.write("Hello World!");
      }
    }

    C#

    using Google.Cloud.Functions.Framework;
    using Microsoft.AspNetCore.Http;
    using System.Threading.Tasks;
    
    namespace HelloWorld;
    
    public class Function : IHttpFunction
    {
        public async Task HandleAsync(HttpContext context)
        {
            await context.Response.WriteAsync("Hello World!", context.RequestAborted);
        }
    }

    Ruby

    require "functions_framework"
    
    FunctionsFramework.http "hello_get" do |_request|
      # The request parameter is a Rack::Request object.
      # See https://www.rubydoc.info/gems/rack/Rack/Request
    
      # Return the response body as a string.
      # You can also return a Rack::Response object, a Rack response array, or
      # a hash which will be JSON-encoded into a response.
      "Hello World!"
    end

    PHP

    
    use Psr\Http\Message\ServerRequestInterface;
    
    function helloGet(ServerRequestInterface $request): string
    {
        return 'Hello, World!' . PHP_EOL;
    }
    

Deploying the function

To deploy the function with an HTTP trigger, run the following command in the directory that contains the sample code (or in the case of Java, the pom.xml file):

Node.js

gcloud functions deploy helloGET \
--runtime nodejs20 --trigger-http

Use the --runtime flag to specify the runtime ID of a supported Node.js version to run your function.

Python

gcloud functions deploy hello_get \
--runtime python312 --trigger-http

Use the --runtime flag to specify the runtime ID of a supported Python version to run your function.

Go

gcloud functions deploy HelloGet \
--runtime go121 --trigger-http

Use the --runtime flag to specify the runtime ID of a supported Go version to run your function.

Java

gcloud functions deploy java-http-function \
--entry-point functions.HelloWorld \
--runtime java17 \
--memory 512MB --trigger-http

Use the --runtime flag to specify the runtime ID of a supported Java version to run your function.

C#

gcloud functions deploy csharp-http-function \
--entry-point HelloWorld.Function \
--runtime dotnet6 --trigger-http

Use the --runtime flag to specify the runtime ID of a supported .NET version to run your function.

Ruby

gcloud functions deploy hello_get --runtime ruby32 --trigger-http

Use the --runtime flag to specify the runtime ID of a supported Ruby version to run your function.

PHP

 gcloud functions deploy helloGet --runtime php82 --trigger-http

Use the --runtime flag to specify the runtime ID of a supported PHP version to run your function.

You can optionally use the --allow-unauthenticated flag to reach the function without authentication. This is useful for testing, but we don't recommend using this setting in production unless you are creating a public API or website. Further, it might not work for you, depending on your corporate policy settings. See Authenticating for invocation for details on how to invoke a function that requires authentication.

Triggering the function

To make an HTTP request to your function, run the following command:

Node.js

curl "https://REGION-PROJECT_ID.cloudfunctions.net/helloGET" 

Python

curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get" 

Go

curl "https://REGION-PROJECT_ID.cloudfunctions.net/HelloGet" 

Java

curl "https://REGION-PROJECT_ID.cloudfunctions.net/java-http-function" 

C#

curl "https://REGION-PROJECT_ID.cloudfunctions.net/csharp-http-function" 

Ruby

curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get" 

PHP

curl "https://REGION-PROJECT_ID.cloudfunctions.net/helloGet" 

where

  • REGION is the region where your function is deployed. This is visible in your terminal when your function finishes deploying.
  • PROJECT_ID is your Cloud project ID. This is visible in your terminal when your function finishes deploying.

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

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.

Deleting the project

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

To delete the project:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting the function

Deleting Cloud Run functions does not remove any resources stored in Cloud Storage.

To delete the Cloud Run function you created in this tutorial, run the following command:

Node.js

gcloud functions delete helloGET 

Python

gcloud functions delete hello_get 

Go

gcloud functions delete HelloGet 

Java

gcloud functions delete java-http-function 

C#

gcloud functions delete csharp-http-function 

Ruby

gcloud functions delete hello_get 

PHP

gcloud functions delete helloGet 

You can also delete Cloud Run functions from the Google Cloud console.