AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of the Amazon Web Services.This service runs code in response to events and automatically manages any computing resources required. It was introduced in November 2014.
Sounds great? Did not for Ruby Developers. Until 29th of November 2018, Ruby was not supported to start serverless revolution. But then, Jets came, and everything has changed.
As the official Ruby on Jets website proudly says:
“Ruby on Jets allows you to create and deploy serverless services with ease, and to seamlessly glue AWS services together with the most beautiful dynamic language: Ruby. It includes everything you need to build an API and deploy it to AWS Lambda. Jets leverages the power of Ruby to make serverless joyful for everyone”
Indeed, working with Jets, especially for someone who’s somehow experienced in Rails or Sinatra, becomes relatively easy and pleasant.
To prove it, in this article we’re going to build a simple serverless application.
What we’re gonna do, is create a very simple Jets app. This will provide us with some basic information of how to create routes, controllers and actions, and how to deploy to AWS Lambda. To keep it simple and easy, we’re gonna use just one endpoint, that will return random Chuck Norris joke. (Check http://www.icndb.com/api/).
First of all, let’s install Jets gem globally. In your console, type:
Then, create new Jets project with:
(Options —mode api and —no-database are not required, it’s just gonna speed up installation a little, since we don’t need any database or views here.)
Now, in your chuck-norris-jokes directory run:
Entering localhost:8888 in your browser, you should see something like this:
If this is what you see – congrats, you’ve done everything correctly!
Controller and routes
What we’re about to do now is create our first Jets controller.
In the console type:
This will create our JokesController with one action inside – create. By default, Jets create the „get” type route for the action, so we have to modify it.
In routes.rb file change get „jokes/create” to post ‘jokes’, to: „jokes#create”.
Fetching the request
To fetch some Chuck Norris stuff we need to make a call to the John Doe Api. Then, we’re gonna respond with json containing the fetched joke.
We can run this locally to test that we are getting the expected response. What we’re going to do is make a POST request to our local server using
(Watch out: make sure your server is still running).
Providing you have an AWS account with appropriate policies (you can find them in official Jets documentation), the last thing you need to do is simply run:
This will upload our application to AWS.
When the deploy is finished, you’ll be provided with your API gateway endpoint (most likely the last line in the console).
Test your AWS Lambda application
So now, finally it’s time to test our deployed app!
We can do this using curl again, but now replacing localhost with API gateway endpoint, we were provided with.
That’s all folks! Hope you enjoyed this brief look into Ruby on Jets and serverless world.