Building an API with Ruby and the Serverless Framework
Tags: serverless, ruby, serverlessframework, tutorial
Published: 2022-04-26 15:24:20

Originally posted at [Serverless](https://www.serverless.com/blog/api-ruby-serverless-framework/) on December 4th, 2018

![](https://cdn-images-1.medium.com/max/5200/0*SBKnp5O00p6aVlqs.png)

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.

![](https://cdn-images-1.medium.com/max/3202/0*nU1d2OE_kgHbtnEK.png)

### Getting Started

First up, we need to install the Serverless Framework (if you haven’t already), and create a new Ruby service:
{% gist https://gist.github.com/richardgrantserverless/3ecf053fb8b6c9808962c9631842f498.js %}
Navigate to your new service folder, and deploy the default hello world:
{% gist https://gist.github.com/richardgrantserverless/c1b66dee1bcc9e46e8b23929813ef53a.js %}
**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:
{% gist https://gist.github.com/richardgrantserverless/0ce8f60dcbf8e18ddf822cf7487e428b.js %}
Open test/handler_test.rb in a and copy/paste in the following code:
{% gist https://gist.github.com/richardgrantserverless/77d1ba92e9007d3a4af57c5a14dca08e.js %}
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:
{% gist https://gist.github.com/richardgrantserverless/87c0bb4a607cedd95f44fa9dc92b9313.js %}
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:
{% gist https://gist.github.com/richardgrantserverless/1bee5a4bde00a9fe8ea9b7b3a69d321b.js %}
As always, let’s update our test to make sure we’re still happy. Make test/handler_test.rb contain the following:
{% gist https://gist.github.com/richardgrantserverless/569725dff4011ad2d4aa3a27ff10201e.js %}
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!
{% gist https://gist.github.com/richardgrantserverless/278880e822d14fe3045db9ef706d9420.js %}
Grab that POST endpoint, run a curl against it, and you should see something like the following:
{% gist https://gist.github.com/richardgrantserverless/5131597e5209945078d4fa84a2df21c9.js %}
### 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](https://github.com/serverless/examples) and share it with the community.

Here are some great community examples contributions we’ve already gotten: — [aws-ruby-simple-http-endpoint](https://github.com/serverless/examples/tree/master/aws-ruby-simple-http-endpoint)

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/api-ruby-serverless-framework/).*