Create and deploy an HTTP Cloud Run function with Ruby

This guide takes you through the process of writing a Cloud Run function using the Ruby runtime. There are two types of Cloud Run functions:

  • An HTTP function, which you invoke from standard HTTP requests.
  • An event-driven function, which you use to handle events from your Cloud infrastructure, such as messages on a Pub/Sub topic, or changes in a Cloud Storage bucket.

For more detail, read writing HTTP functions and writing event-driven functions.

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, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  7. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.

    Enable the APIs

  8. Install and initialize the Google Cloud SDK.
  9. Update and install gcloud components with the following command.
    gcloud components update
  10. Prepare your development environment.

    Go to the Ruby setup guide

Create your function

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

    Linux or Mac OS X

    mkdir ~/helloworld
    cd ~/helloworld
    

    Windows

    mkdir %HOMEPATH%\helloworld
    cd %HOMEPATH%\helloworld
    
  2. Create an app.rb file in the helloworld directory with the following contents:

    require "functions_framework"
    require "cgi"
    require "json"
    
    FunctionsFramework.http "hello_http" do |request|
      # The request parameter is a Rack::Request object.
      # See https://www.rubydoc.info/gems/rack/Rack/Request
      name = request.params["name"] ||
             (request.body.rewind && JSON.parse(request.body.read)["name"] rescue nil) ||
             "World"
      # 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 #{CGI.escape_html name}!"
    end

    This example function takes a name supplied in the HTTP request and returns a greeting, or "Hello World!" when no name is supplied.

Specify dependencies

Dependencies in Ruby are managed with bundler and expressed in a file called Gemfile.

When you deploy your function, Cloud Run functions downloads and installs the dependencies declared in the Gemfile and Gemfile.lock using bundler.

The Gemfile lists the packages required by your function, along with any optional version constraints. For a Cloud Run function, one of these packages must be the functions_framework gem.

For this exercise, create a file named Gemfile in the same directory as the app.rb file that contains your function code, with the following contents:

source "https://rubygems.org"
gem "functions_framework", "~> 0.7"

Run the following command to install the functions_framework gem and other dependencies:

bundle install

Build and test your function locally

Before deploying your function, you can build and test it locally.

  1. Run the following command to use the functions-framework-ruby executable to start a local web server running your hello_http function:

    bundle exec functions-framework-ruby --target hello_http
    
  2. Test your function by visiting http://localhost:8080 in a browser or by running curl localhost:8080 from another window.

    See Sending requests to local functions for more detail.

See Testing Functions in the Ruby Functions Framework documentation.

Deploy your function

To deploy your function, run the following command in the helloworld directory:

  gcloud functions deploy ruby-http-function \
    --gen2 \
    --runtime=ruby32 \
    --region=REGION \
    --entry-point=hello_http \
    --source=. \
    --trigger-http \
    --allow-unauthenticated

Replace REGION with the name of the Google Cloud region where you want to deploy your function (for example, us-west1).

The optional --allow-unauthenticated flag lets you reach your function without authentication.

Test your deployed function

  1. After the function deploys, note the uri property from the output of the gcloud functions deploy command, or retrieve it with the following command:

        gcloud functions describe ruby-http-function \
          --region=REGION
    

    Replace REGION with the name of the Google Cloud region where you deployed your function (for example, us-west1).

  2. Visit this URL in your browser. The function returns a "Hello World!" message.

View your function's logs

You can review your function's logs with the Cloud Logging UI or through the Google Cloud CLI.

View the logs with the command-line tool

To view logs for your function with the gcloud CLI, use the logs read command:

    gcloud functions logs read \
      --gen2 \
      --limit=10 \
      --region=REGION \
      ruby-http-function

Replace REGION with the name of the Google Cloud region where you deployed your function (for example, us-west1).

The output resembles the following:

LEVEL: I
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.477
LOG: Default STARTUP TCP probe succeeded after 1 attempt for container "hello__http-1" on port 8080.

LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.451
LOG: I, [2023-06-01T00:09:41.451784 #1]  INFO -- : FunctionsFramework: Serving function "hello_http" on port 8080...

LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.364
LOG: I, [2023-06-01T00:09:41.363923 #1]  INFO -- : FunctionsFramework: Starting server...

LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.363
LOG: I, [2023-06-01T00:09:41.363855 #1]  INFO -- : FunctionsFramework: Looking for function name "hello_http"...

LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.354
LOG: I, [2023-06-01T00:09:41.354150 #1]  INFO -- : FunctionsFramework: Loading functions from "./app.rb"...

View logs with the logging dashboard

To view the logs for your function with the logging dashboard, open the Cloud Run functions Overview page and click the name of your function from the list, then click the Logs tab.