# 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.

> [!IMPORTANT]
> **Learn more** : For more details, read about [HTTP functions](https://docs.cloud.google.com/functions/1stgendocs/writing/write-http-functions.md.txt) and [event-driven functions](https://docs.cloud.google.com/functions/1stgendocs/writing/write-event-driven-functions.md.txt).

## Before you begin

1. Prepare your development environment.
   [Go to the Ruby setup guide](https://docs.cloud.google.com/ruby/docs/setup.md.txt)

<br />

## Create a function

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

   ### Linux or Mac OS X

   ```ruby
   mkdir ~/helloworld
   cd ~/helloworld
   ```

   ### Windows

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

   ```ruby
   require "functions_framework"
   require "cgi/escape"
   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"] ||
            begin
              request.body.rewind
              JSON.parse(request.body.read)["name"]
            rescue StandardError
              nil
            end ||
            "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.

   > [!NOTE]
   > **Note:** Cloud Run functions looks for deployable functions in `app.rb` by default. Use the [`--source`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--source) flag when [deploying your function using `gcloud`](https://docs.cloud.google.com/functions/1stgendocs/create-deploy-http-ruby-1st-gen.md.txt#deploy_the_function) to specify a different directory containing an `app.rb` file.

## Specify dependencies

Dependencies in Ruby are managed with [bundler](https://bundler.io) 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:

```ruby
bundle install
```

> [!IMPORTANT]
> **Learn more** : For more details, read about [specifying dependencies](https://docs.cloud.google.com/functions/1stgendocs/writing/specifying-dependencies-ruby.md.txt).

## 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:

```ruby
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:

```ruby
curl localhost:8080
# Output: Hello World!
```

See [Testing Functions](https://github.com/GoogleCloudPlatform/functions-framework-ruby#documentation)
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:

```ruby
gcloud functions deploy hello_http --no-gen2 --runtime ruby40 --trigger-http --allow-unauthenticated
```

The `--allow-unauthenticated` flag lets you reach the function
[without authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/managing-access-iam.md.txt#allowing_unauthenticated_http_function_invocation).
To require
[authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/authenticating.md.txt#developers), omit the
flag.

> [!IMPORTANT]
> **Learn more** : For more details, read about [deploying Cloud Run functions](https://docs.cloud.google.com/functions/1stgendocs/deploy.md.txt#basics).

## Test the deployed function

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

   ```ruby
   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`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/logs/read) command, followed by
the name of the function:

```bash
gcloud functions logs read hello_http
```

The output should resemble the following:

```bash
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
```

> [!NOTE]
> **Note:** There is typically a slight delay between when log entries are created and when they show up in Cloud Logging.

### View logs in the Logging dashboard

You can also view logs for Cloud Run functions from the
[Google Cloud console](https://console.cloud.google.com/project/_/logs?service=cloudfunctions.googleapis.com).

> [!IMPORTANT]
> **Learn more** : For more details, read about [writing, viewing, and responding to logs](https://docs.cloud.google.com/functions/1stgendocs/monitoring/logging.md.txt).