Java Quickstart

Cloud Functions is a serverless execution environment for building and connecting cloud services. With Cloud Functions you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your function is triggered when an event being watched is fired.

Learn how to create and deploy a Java Cloud Function using the Cloud Console.


For step-by-step guidance on this task directly in Cloud Console, click Guide me:

Guide me


The following sections take you through the same steps as clicking Guide me.

When this function is triggered by an HTTP request, it writes a message:

package com.example;

import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.BufferedWriter;

public class Example implements HttpFunction {
  @Override
  public void service(HttpRequest request, HttpResponse response) throws Exception {
    BufferedWriter writer = response.getWriter();
    writer.write("Hello world!");
  }
}

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 Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the Cloud Functions and Cloud Build APIs.

    Enable the APIs

Create a function

  1. Open the Functions Overview page in the Cloud Console:

    Go to the Cloud Functions Overview page

    Make sure that the project for which you enabled Cloud Functions is selected.

  2. Click Create function.

  3. Name your function.

  4. In the Trigger field, select HTTP.

  5. In the Authentication field, select Allow unauthenticated invocations.

  6. Click Save to save your changes, and then click Next.

  7. In the Source code field, select Inline editor. In this exercise, you will use the default function provided in the editor.

  8. Use the Runtime dropdown to select the desired Java runtime.

Deploy the function

  1. At the bottom of the page, click Deploy.

  2. After clicking Deploy, Cloud Console redirects to the Cloud Functions Overview page.

    While the function is being deployed, the icon next to it is a small spinner. After it finishes deploying, the spinner turns to a green check mark:

    Screenshot that shows function deployment process

Test the function

  1. Display the menu for your function, and click Test function.

    Screenshot that shows function testing process

  2. On the testing page, click Test the function.

    The Output screen displays the text "Hello World!"

View logs

Check the logs to see your actions in the log history:

  • Back on the Cloud Functions Overview page, display the menu for your function, and click View logs.

Your log history appears.

Screenshot showing Logs field with 200 code, indicating success

What's next