Quickstart: Create and deploy an HTTP Cloud Run function by using Ruby

Create and deploy an HTTP Cloud Run function by using Ruby (1st gen)

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.

The sample shows how to create a simple HTTP function.

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. Verify that billing is enabled for your Google Cloud 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. Verify that billing is enabled for your Google Cloud project .

  7. Enable the Cloud Functions and Cloud Build APIs.

    Enable the APIs

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

    Go to the Ruby setup guide

Create 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" 
     ] 
      
     || 
      
     ( 
     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 locally

Before deploying the function, you can build and test it locally. 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 
 # ...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! 
 

See Testing Functions in the Ruby Functions Framework documentation.

Deploy the function

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

  gcloud 
  
 functions 
  
 deploy 
  
 hello_http 
  
 -- 
 no 
 - 
 gen2 
  
 -- 
 runtime 
  
 ruby33 
  
 -- 
 trigger 
 - 
 http 
  
 -- 
 allow 
 - 
 unauthenticated 
 

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

Test 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 !"

View logs

You can view Cloud Run functions logs in the Cloud Logging UI or through the Google Cloud CLI.

View logs with the command-line tool

To view logs for your function with the gcloud CLI, 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 

View logs in the Logging dashboard

You can also view logs for Cloud Run functions from the Google Cloud console .

Design a Mobile Site
View Site in Mobile | Classic
Share by: