DEV Community

Cover image for Building an API with Ruby and the Serverless Framework
We're Serverless! for Serverless Inc.

Posted on • Originally published at serverless.com

2 1

Building an API with Ruby and the Serverless Framework

Originally posted at Serverless on December 4th, 2018

On the heels of re:Invent, it’s been a great week for the serverless community. And one of the most exciting things in AWS’s re:Invent goodie basket? Ruby support for Lambda!

Personally, I love Ruby, and was really excited to play around with a Ruby deployment on the Serverless Framework.

So here you have it, Ruby fans. Your Ruby + Serverless Framework getting started template. I’m going to start by covering some Ruby + Serverless Framework basics and testing practices, and then we’ll build a fully-fledged Ruby API.

Let’s get to it.

Getting Started

First up, we need to install the Serverless Framework (if you haven’t already), and create a new Ruby service:

npm install -g serverless
serverless create -t aws-ruby -p myservice
view raw .sh hosted with ❤ by GitHub

Navigate to your new service folder, and deploy the default hello world:
cd myservice
sls deploy
sls invoke -f hello
view raw .sh hosted with ❤ by GitHub

Testing Locally

Right now the framework only supports NodeJS, Python and Java in the local testing. (We hope to get that updated soon!) In the meantime, because Ruby is awesome, we have some great testing tools and capabilities right at our fingertips with some of the built-in testing tooling.

Let’s say we want to run our local handler and make sure it returns the status code:

mkdir test
touch test/handler_test.rb
view raw .txt hosted with ❤ by GitHub

Open test/handler_test.rb in a and copy/paste in the following code:
require_relative '../handler.rb'
require 'test/unit'
class TestHandler < Test::Unit::TestCase
def test_happy_path()
response = hello(event: {}, context: {})
assert_equal 200, response[:statusCode]
assert_match /Serverless/, response[:body]
end
end
view raw .rb hosted with ❤ by GitHub

If we run ruby -I test test/handler_test.rb, we'll get a nice simulated invoke (assuming you have Ruby installed locally of course).

We’ll dive a bit more into this in the next more advanced section, building an API!

Building an API With Ruby, the Serverless Framework, and AWS Lambda

Building on what we already have in serverless.yml, let's add an event to our function:

service: aws-ruby # NOTE: update this with your service name
provider:
name: aws
runtime: ruby2.5
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
method: post
view raw .yml hosted with ❤ by GitHub

Let’s also update our handler.rb with a some more interesting logic. We're going to echo back a posted request body as part of the message.

If we don’t get a request body or something else goes wrong, we handle that as well:

require 'json'
def hello(event:, context:)
begin
puts "Received Request: #{event}"
{ statusCode: 200, body: JSON.generate("Go Serverless v1.0! Your function executed successfully! #{event['body']}") }
rescue StandardError => e
puts e.message
puts e.backtrace.inspect
{ statusCode: 400, body: JSON.generate("Bad request, please POST a request body!") }
end
end
view raw .rb hosted with ❤ by GitHub

As always, let’s update our test to make sure we’re still happy. Make test/handler_test.rb contain the following:
require_relative '../handler.rb'
require 'test/unit'
class TestHandler < Test::Unit::TestCase
def test_happy_path()
response = hello(event: {"body": "Hello, World!"}, context: {})
assert_equal 200, response[:statusCode]
assert_match /Serverless/, response[:body]
end
def test_sad_path()
response = hello(event: nil, context: {})
assert_equal 400, response[:statusCode]
assert_match /please POST/, response[:body]
end
end
view raw .rb hosted with ❤ by GitHub

After running a quick ruby -I test test/handler_test.rb to make sure all our tests still pass, then an sls deploy, we should have an API!
Serverless: Stack update finished...
Service Information
service: aws-ruby
stage: dev
region: us-east-2
stack: aws-ruby-dev
api keys:
None
endpoints:
POST - https://someid.execute-api.us-east-1.amazonaws.com/dev/hello
functions:
hello: aws-ruby-dev-hello
view raw .yml hosted with ❤ by GitHub

Grab that POST endpoint, run a curl against it, and you should see something like the following:
# curl -X POST https://someid.execute-api.us-east-2.amazonaws.com/dev/hello -d "Hello, World!"
"Go Serverless v1.0! Your function executed successfully! Hello, World!
view raw .sh hosted with ❤ by GitHub

Ruby Examples

I can’t wait to see what the serverless community builds with Ruby! When you make your first (or fifth) Ruby app, please do submit it to our Examples Repo and share it with the community.

Here are some great community examples contributions we’ve already gotten: — aws-ruby-simple-http-endpoint

Originally published at https://www.serverless.com.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more