Hanami Lambda is a gem that provides a way to run hanami application on AWS Lambda.
Hanami::Lambda supports Ruby (MRI) 3.0+
Add this line to your application's Gemfile:
gem "hanami-lambda"And then execute:
$ bundle install
Update config/app.rb with below content
require 'hanami'
require 'hanami/lambda'
module MyApp # Rename to your app name
class Lambda < Hanami::App
extend Hanami::Lambda::Application
end
endCreate app/function.rb as handler base class
module MyApp
class Function < Hanami::Lambda::Function
end
endGenerator is comming soon.
Use config/app.Hanami::Lambda.call as the function handler
# AWS SAM
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: config/app.Hanami::Lambda.call
Runtime: ruby3.2If the lambda function isn't trigger by APIGateway, we can use delegate method to define the handler function.
Create config/lambda.rb with below content
module MyApp
class Lambda < Hanami::Lambda::Dispatcher
delegate "MyFunction", to: "daily_task"
end
endThe IaC generated function will be
my-app-MyFunction-r8faNAo3iUqxtherefore the dispatcher will use includeMyFunctionto find targeted function
Add app/functions/daily_task.rb to define the handle action
module MyApp
module Functions
class DailyTask < MyApp::Function
def handle(event, context)
# ...
end
end
end
endAfter checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/elct9620/hanami-lambda.
The gem is available as open source under the terms of the MIT License.