Jets Simple AWS Lambda Ruby Function
Jets supports writing simple AWS Lambda functions with Ruby. You define them in the app/functions
folder. A function looks like this:
app/functions/simple.rb:
def lambda_handler(event:, context:)
puts "hello world"
end
The default handler is named lambda_handler
. The lambda function shows up in the Lambda console like this:
You can run the function in the AWS Lambda console and see the results:
More than Simple Functions
Though simple functions are supported by Jets, aside from the ability to use Ruby they do not really add much value. Other classes like controllers and jobs add many conveniences and are more powerful to use. Here are a few quick examples:
Controllers
Controllers are designed to handle web request and serve responses.
class PostsController < ApplicationController
def index
# renders Lambda Proxy structure compatiable with API Gateway
render json: {hello: "world", action: "index"}
end
end
The public methods of a Controller class get deployed as separate AWS Lambda functions.
Jobs
Jobs are designed to handle background jobs and can process work outside the web request cycle. The rate
declaration creates a CloudWatch rule and schedules the function to run periodically.
class HardJob < ApplicationJob
rate "10 hours" # every 10 hours
def dig
puts "done digging"
{done: "digging"}
end
end
Once again, the public methods of a Job class get deployed as separate AWS Lambda functions.
So you’d probably benefit more from writing Controllers and Jobs since they add so many conveniences over the simple function.
Thanks for reading this far. If you found this article useful, I'd really appreciate it if you share this article so others can find it too! Thanks 😁 Also follow me on Twitter.
Got questions? Check out BoltOps.
You might also like
More tools:
-
Kubes
Kubes: Kubernetes Deployment Tool
Kubes is a Kubernetes Deployment Tool. It builds the docker image, creates the Kubernetes YAML, and runs kubectl apply. It automates the deployment process and saves you precious finger-typing energy.
-
Jets
Jets: The Ruby Serverless Framework
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.
-
Lono
Lono: The CloudFormation Framework
Building infrastructure-as-code is challenging. Lono makes it much easier and fun. It includes everything you need to manage and deploy infrastructure-as-code.