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
- Creating a GCP project using Cloud SDK
- Creating a function
- Specifying dependencies
- Building and testing locally
- Deploying the function
- Testing the deployed function
Creating a GCP project using Cloud SDK
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Cloud Functions and Cloud Build APIs.
- Install and initialize the Cloud SDK.
- Update and install
gcloud
components:gcloud components update
- Prepare your development environment.
Need a command prompt? You can use the Google Cloud Shell. The Google Cloud Shell is a command line environment that already includes the Google Cloud SDK, so you don't need to install it. The Google Cloud SDK also comes preinstalled on Google Compute Engine Virtual Machines.
Creating a function
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
Create an
app.rb
file in thehelloworld
directory with the following contents: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 ruby26 --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
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
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.