Giter Club home page Giter Club logo

yake's Introduction

λake

gem rspec coverage maintainability

Write your AWS Lambda function handlers using a Rake-like declarative syntax:

# ./lambda_function.rb
require "yake"

handler :lambda_handler do |event|
  # Your code here
end

# Handler signature: `lambda_function.lambda_handler`

You can even declare Sinatra-like API Gateway routes for a main entrypoint:

# ./lambda_function.rb
require "yake/api"

header "content-type" => "application/json"

get "/fizz" do |handler|
  respond 200, { ok: true }.to_json
end

handler :lambda_handler do |event|
  route event
rescue => err
  respond 500, { message: err.message }.to_json
end

# Handler signature: `lambda_function.lambda_handler`

Installation

Add this line to your application's Gemfile:

gem "yake"

And then execute:

bundle install

Or install it yourself as:

gem install yake

Why Is It Called "yake"?

"λ" + Rake, but "λ" is hard to type and I think "y" looks like a funny little upside-down-and-backwards Lambda symbol.

Why Use It?

So why use yake for your Lambda functions?

Zero Dependencies

yake does not depend on any other gems, using the Ruby stdlib only. This helps keep your Lambda packages slim & speedy.

Event Logging

By default, the handler function wraps its block in log lines formatted to match the style of Amazon's native Lambda logs sent to CloudWatch. Each invocation of the handler will log both the input event and the returned value, prefixed with the ID of the request:

START RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf Version: $LATEST
INFO RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf EVENT { … }
…
INFO RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf RETURN { … }
END RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf
REPORT RequestId: 149c500f-028a-4b57-8977-0ef568cf8caf	Duration: 43.97 ms	Billed Duration: 44 ms	Memory Size: 128 MB	Max Memory Used: 77 MB

Logging the request ID in this way makes gathering logs lines for a particular execution in CloudWatch much easier.

This feature can be disabled by adding a declaration in your handler file:

logging :off

API Routes

A common use of Lambda functions is as a proxy for API Gateway. Oftentimes users will deploy a single Lambda function to handle all requests coming from API Gateway.

Requiring the yake/api module will add the API-specific DSL into your handler.

Define API routes using Sinatra-like syntax

delete "/…" do |event|
  # Handle 'DELETE /…' route key events
end

get "/…" do |event|
  # Handle 'GET /…' route key events
end

head "/…" do |event|
  # Handle 'HEAD /…' route key events
end

options "/…" do |event|
  # Handle 'OPTIONS /…' route key events
end

patch "/…" do |event|
  # Handle 'PATCH /…' route key events
end

post "/…" do |event|
  # Handle 'POST /…' route key events
end

put "/…" do |event|
  # Handle 'PUT /…' route key events
end

Helper methods are also made available to help produce a response for API Gateway:

Set a default header for ALL responses:

header "content-type" => "application/json; charset=utf-8"
header "x-custom-header" => "fizz"

Produce an API Gateway-style response object:

respond 200, { ok: true }.to_json, "x-extra-header" => "buzz"
# {
#   "statusCode" => 200,
#   "body" => '{"ok":true}',
#   "headers" => { "x-extra-header" => "fizz" }
# }

Route an event to one of the declared routes:

handler :lambda_handler do |event|
  route event
rescue Yake::UndeclaredRoute => err
  respond 404, { message: err.message }.to_json
rescue => err
  respond 500 { message: err.message }.to_json
end

Development

After 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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/amancevice/yake.

License

The gem is available as open source under the terms of the MIT License.

yake's People

Contributors

amancevice avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.