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 HTTP request is received externally, or an event being watched is fired.
This page shows you how to create and deploy a Ruby Cloud Function using the Cloud Console. When this Ruby function is triggered by an HTTP request, it writes a message:
require "functions_framework" require "json" # This function receives an HTTP request of type Rack::Request # and interprets the body as JSON. It prints the contents of # the "message" field, or "Hello World!" if there isn't one. FunctionsFramework.http "hello_world" do |request| input = JSON.parse request.body.read rescue {} msg = input["message"].to_s msg.empty? ? "Hello World!" : msg end
Before you begin
- 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.
-
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.
Create a function
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.
Click Create function.
Name your function.
In the Trigger field, select HTTP.
In the Authentication field, select Allow unauthenticated invocations.
Click Save to save your changes, and then click Next.
In the Source code field, select Inline editor. In this exercise, you will use the default function provided in the editor.
Use the Runtime dropdown to select the desired Ruby runtime.
Deploy the function
At the bottom of the page, click Deploy.
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:
Test the function
Display the menu for your function, and click Test function.
On the testing page, click Test the function.
The Output screen displays the text
"Hello World!"
Now change the message. In the Triggering Event field, enter the text
{"message":"Hello, YOUR_NAME!"}
, replacingYOUR_NAME
with a name, and click Test the function.For example, suppose you entered the name "Rowan". In the Output field, you would see the message
Hello, Rowan!
.In the Logs field, a status code of 200 indicates success.
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.