Your First Function: Ruby

This guide takes you through the process of writing a Cloud Function using the Ruby runtime. There are two types of Cloud 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 Cloud Pub/Sub topic, or changes in a Cloud Storage bucket.

The sample shows how to create a simple HTTP function.

Guide structure

  1. Creating a GCP project using Cloud SDK
  2. Creating a function
  3. Specifying dependencies
  4. Building and testing locally
  5. Deploying the function
  6. Testing the deployed function

Creating a GCP project using Cloud SDK

  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

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

  7. Enable the Cloud Functions and Cloud Build APIs.

    Enable the APIs

  8. Install and initialize the Cloud SDK.
  9. Update and install gcloud components:
    gcloud components update
  10. Prepare your development environment.

    Go to the Ruby setup guide

Creating a 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"] ||
             (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.

Specifying dependencies

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

When you deploy your function, Cloud 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 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

Building and testing locally

Before deploying the function, you can build and test it locally. Run the following command to use the functions-framework-ruby executable to spin up a local web server running your hello_http function:

bundle exec functions-framework-ruby --target hello_http
# ...starts the web server in the foreground

If the function builds successfully, it displays the URL you can visit in your web browser to see the function in action: http://localhost:8080/. You should see a Hello World! message.

Alternatively, you can send requests to this function using curl from another terminal window:

curl localhost:8080
# Output: Hello World!

Deploying the function

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

gcloud functions deploy hello_http --runtime ruby27 --trigger-http --allow-unauthenticated

The --allow-unauthenticated flag lets you reach the function without authentication. To require authentication, omit the flag.

Testing the deployed function

  1. When the function finishes deploying, take note of the httpsTrigger.url property or find it using the following command:

    gcloud functions describe hello_http
    

    It should look like this:

    https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http
  2. Visit this URL in your browser. You should see a "Hello World!" message.

    Try passing a name in the HTTP request, for example by using the following URL:

    https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http?name=NAME

    You should see the message "Hello NAME!"

Viewing logs

Using the command-line tool

Logs for Cloud Functions are viewable in the Cloud Logging UI, and via the gcloud command-line tool.

To view logs for your function with the gcloud tool, use the logs read command, followed by the name of the function:

gcloud functions logs read hello_http

The output should resemble the following:

LEVEL  NAME       EXECUTION_ID  TIME_UTC                 LOG
D      helloHttp  rvb9j0axfclb  2019-09-18 22:06:25.983  Function execution started
D      helloHttp  rvb9j0axfclb  2019-09-18 22:06:26.001  Function execution took 19 ms, finished with status code: 200

Using the Logging dashboard

You can also view logs for Cloud Functions from the Cloud Console.