Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Web Development with AWS Lambda

Rails Web Development with AWS Lambda

I talked about how to develop rails web application on AWS Lambda. Slides for M3 Tech Talk #224.

Takayuki Matsubara

July 07, 2023
Tweet

More Decks by Takayuki Matsubara

Other Decks in Programming

Transcript

  1. Various serverless services running your web app on AWS •

    Fargate • App Runner • Lambda <- Today's target • Used in combination with API Gateway in our project
  2. Problems to develop code on Lambda • Vendor Lock-in •

    Complexity is getting higher • The code is scattered and lacks readability if there are many functions • Need to know more than web app knowledges • such as Runtime Interface Client, Serverless Framework, SAM and etc...
  3. Ideally, I would like to ... • Minimize vendor lock-in

    • Prioritize organized and readable code • Continue using our Rails development skills
  4. Supports any web frameworks and languages, no new code dependency

    to include — Referenced from: AWS Lambda Web Adapter's README.md
  5. How AWS Lambda Web Adapter works • Lambda Extensions API

    Image source: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions- api.html
  6. ɹ

  7. Just tried it! # Add the following into your Dockerfile

    COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.7.0 /lambda-adapter /opt/extensions/lambda-adapter
  8. Advantages • Can focus on Rails development • Mitigate cold

    start with API Gateway's Lambda Proxy Integration
  9. However ... • Cold start: Rails is not fast &

    the extension brings extra costs • Are there any servers optimized for a single process? • There are many servers that run on multiple processes/ threads, but I'm not sure about a server suitable for a single process? • Single Dockerfile (this is a minor issue) • Same Dockerfile can't be used for job processes because the extension always starts up and handles events
  10. Lamby • An approach similar to AWS Lambda Web Adapter

    • Additionally, it operates without an extra process
  11. How Lamby works Lamby is an AWS Lambda Web Adapter

    for Rack applications. — Referenced from: Lamby's README.md • Rack server converts Lambda events into Rack requests, and vice versa • So your rails app code and development style are not affected
  12. It solves the problems • Remove extra cost on cold

    starts with Lambda Web Adapter • No need to bother finding a suitable server for single process; instead, Lamby starts the Rails app • Can use Single Dockerfile by overriding CMD
  13. Some problems encountered when using Lamby • Lamby passes URL

    queries percent-decoded • We solve this with a monkey patch in our project • Cold start times are long • Following the documentation: enable Bootsnap and create cache during Docker build phase • AWS Lambda Ruby RIC does not currently support Ruby 3.x
  14. Summary • Approaches to Rails Web Development on Lambda •

    AWS Lambda Web Adapter • Lamby • Continue to use rails development skills • Code remains well-organized and keeps readability • Mitigate vendor lock-in
  15. Resources: Lambda x Rails • Ruby on Rails on Lambda

    - Speaker Deck https:// speakerdeck.com/joker1007/ruby-on-rails-on-lambda
  16. Resources: Lambda Web Adapter • GitHub - awslabs/aws-lambda-web-adapter: Run web

    applications on AWS Lambda https://github.com/awslabs/ aws-lambda-web-adapter • Lambda Extensions API - AWS Lambda https:// docs.aws.amazon.com/lambda/latest/dg/runtimes- extensions-api.html
  17. Resources: Lamby • GitHub - rails-lambda/lamby: Simple Rails & AWS

    Lambda Integration https://github.com/rails-lambda/lamby • GitHub - rack/rack: A modular Ruby web server interface. https://github.com/rack/rack
  18. END