Build an API with the Jets Ruby Serverless Framework
Build an API with the Jets Ruby Serverless Framework ๊ด๋ จ
In this blog post, Iโll cover how to build a simple API service on AWS Lambda with the Jets Ruby Serverless Framework.
Jets New API Mode
The jets new
command has a few different modes: html, api, and job. Weโll use the handy api mode for this tutorial to create a starter project designed for APIs.
jets new demo --mode api
cd demo
Weโve successfully generated a starter Jets project. The nice thing about api mode is that it generates a lighter starter project that does not include things not needed like bootstrap and webpacker assets.
We are ready to now add some API code.
Scaffold
Weโll use a scaffold to generate some basic CRUD.
jets generate scaffold Post title:string
vim .env.development # adjust to your local database creds
jets db:create db:migrate
Local Testing
Letโs seed some data by creating a db/
seeds.rb
:
3.times do |i|
Post.create(title: "Title #{i+1}")
end
puts "Seeding data completed"
Hereโs the command to load the db/
seeds.rb
data:
jets db:seed
Now letโs do some quick local testing with jets console
.
jets console
# >> item = Post.find(1)
# >> item.title = "Title 1 Edit"
# >> item.save!
We can also test with a local server with jets server
:
jets server # Check out site at http://localhost:8888/posts
Check out the API at http://localhost:8888/posts
Since this is an API, we can also test with curl.
Hereโs how we create a data.json
file and send it via curl
.
cat << 'EOF' > data.json
{
"post": {
"title": "My Test Post 1"
}
}
EOF
curl -i -X POST http://localhost:8888/posts -H 'Content-Type: application/json' --data @data.json
The curl
output looks something like this:
curl -i -X POST http://localhost:8888/posts -H 'Content-Type: application/json' --data @data.json
# HTTP/1.1 201 Created
# Access-Control-Allow-Origin: *
# Access-Control-Allow-Credentials: true
# Content-Type: application/json
# X-Jets-Base64: no
# X-Runtime: 0.115564
# Server: WEBrick/1.4.2 (Ruby/2.5.3/2018-10-18)
# Date: Sun, 13 Jan 2019 19:21:00 GMT
# Content-Length: 113
# Connection: Keep-Alive
#
# {"id":4,"title":"My Test Post 1","created_at":"2019-01-13T19:20:59.000Z","updated_at":"2019-01-13T19:20:59.000Z"}
Deploy
Before we deploy, we need to create a database that AWS Lambda will have access to. You can follow the AWS RDS docs. Step 1: Create an RDS DB Instance. It is also briefly mentioned in this video: Jets Tutorial Deploy to AWS Lambda Part 2: AWS Lambda Ruby.
Weโll also have to create and migrate the RDS database.
vim .env.development.remote # configure a remote RDS DB
JETS_ENV_REMOTE=1 jets db:create db:migrate
Once that is completed, letโs deploy our application to AWS Lambda.
jets deploy
#
# 07:28:53PM UPDATE_COMPLETE AWS::CloudFormation::Stack demo-dev
# Stack success status: UPDATE_COMPLETE
# Time took for stack deployment: 2m 27s.
# Prewarming application.
# API Gateway Endpoint: https://7bfi3qwj61.execute-api.us-west-2.amazonaws.com/dev/
At the end of the deploy, youโll see the API Gateway url that looks something like this https://7bfi3qwj61.execute-api.us-west-2.amazonaws.com/dev/
. Note, your URL will be different.
Testing It
Weโll use the curl commands again to create some posts with AWS Lambda using the data.json
file we created earlier.:
curl -X POST https://7bfi3qwj61.execute-api.us-west-2.amazonaws.com/dev/posts -H 'Content-Type: application/json' --data @data.json
#
# {"id":7,"title":"My Test Post 1","body":null,"published":null,"created_at":"2019-01-13T19:34:29.000Z","updated_at":"2019-01-13T19:34:29.000Z"}
Extra Environments
An interesting benefit of running applications on AWS Lambda is that you only get charged for actual requests. So extra environments are likely in the AWS free tier. You could do this:
JETS_ENV_EXTRA=2 jets deploy
JETS_ENV_EXTRA=3 jets deploy
...
JETS_ENV_EXTRA=8 jets deploy
You essentially get unlimited free environments, each of them taking a few minutes to provision.
Live Demo
A live demo of this tutorial is available at https://api.demo.rubyonjets.com.
Thatโs it! Thatโs all it took to create a RESTful API on AWS Lambda with the Jets Ruby Serverless Framework.
Hope youโve enjoyed this article. If you find Ruby on Jets interesting, please give it โญ๏ธ on tongueroo/jets
. Iโd appreciate it. ๐