Using the Ruby Runtime with Serverless Functions
The Ruby runtime is a powerful option for developers looking to leverage Ruby's flexibility and syntax in their serverless functions on Epycbyte. This guide walks you through setting up, writing, and deploying Ruby-based serverless functions.
Introduction
Choosing the right runtime for your serverless functions is crucial. Ruby offers a dynamic and flexible programming experience, making it ideal for tasks that require complex logic or domain-specific languages. With Epycbyte's Ruby runtime, you can compile Ruby code into efficient serverless functions that handle HTTP requests.
Choosing a Runtime
Epycbyte supports the Ruby runtime as part of its edge computing platform. This runtime is available on all plans and allows you to create serverless functions that define a singular HTTP handler. Your Ruby files must reside in an /api directory at your project's root.
Project Structure
-
Directory Structure:
- Create an
/apidirectory at the root of your project. - Place your Ruby files (e.g.,
index.rb) inside this directory.
- Create an
-
Ruby File Requirements:
- Each Ruby file must define a handler that matches the
do |request, response|signature. - The handler can be implemented as either a
Procor a class inheriting fromWEBrick::HTTPServlet::AbstractServlet.
- Each Ruby file must define a handler that matches the
Example Implementation
Here’s an example of how to implement a simple Ruby function:
# api/index.rb
require 'cowsay'
Handler = Proc.new do |request, response|
name = request.query['name'] || 'World'
response.status = 200
response['Content-Type'] = 'text/text; charset=utf-8'
response.body = Cowsay.say("Hello #{name}!", 'cow')
end
This code defines a handler that responds to HTTP requests. It extracts the name parameter from the query string and uses the cowsay gem to generate a formatted response.
Dependency Management
To manage dependencies, you can use a Gemfile. Here’s how to set it up:
# Gemfile
source "https://rubygems.org"
gem "cowsay", "~> 0.3.0"
This file specifies that the cowsay gem should be included in your project dependencies.
Ruby Versioning
Epycbyte supports multiple Ruby versions:
- Default: Ruby 3.3.x (new deployments)
- Legacy: Ruby 2.7.x and 2.5.x are no longer supported as of July 2024
You can specify a Ruby version in your Gemfile using the ruby keyword:
source "https://rubygems.org"
ruby "~> 3.3.x"
If you omit the patch version, Epycbyte will automatically use the latest available version.
Deploying Your Function
-
Bundle Dependencies:
- Run
bundle install --deploymentto install all required dependencies in your project directory.
- Run
-
Deploy the Function:
- Upload your
/apidirectory to Epycbyte’s serverless platform. - The function will be compiled into an efficient executable that handles HTTP requests.
- Upload your
Best Practices
- Testing: Always test your Ruby functions locally before deploying them.
- Optimization: Use image optimization tools like
imagetoolsto reduce file sizes. - Caching: Enable caching for static assets and frequently accessed resources to improve performance.
Resources
- Ruby Documentation: Ruby 3.3.x Official Docs
- Epycbyte Edge Runtime: Epycbyte Function Reference
By following this guide, you can leverage the power of Ruby in your serverless functions and take advantage of Epycbyte's edge computing capabilities to deliver fast and efficient applications.