$30 off During Our Annual Pro Sale. View Details »

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. Rails Web Development
    with AWS Lambda
    2023/07/07 M3 Tech Talk #224 @ma2ge

    View Slide

  2. self-introduction
    • ma2ge@Twitter, Threads
    !"
    • ma2gedev@GitHub
    • Team SRE
    • 10+ years working at M3
    ! "

    View Slide

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

    View Slide

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

    View Slide

  5. Ideally, I would like to ...
    • Minimize vendor lock-in
    • Prioritize organized and readable code
    • Continue using our Rails development skills

    View Slide

  6. AWS Lambda Web
    Adapter
    https://github.com/awslabs/aws-lambda-web-adapter

    View Slide

  7. Supports any web
    frameworks and
    languages, no new code
    dependency to include
    — Referenced from: AWS Lambda Web Adapter's
    README.md

    View Slide

  8. How AWS Lambda Web Adapter
    works
    • Lambda Extensions API
    Image source: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-
    api.html

    View Slide

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

    View Slide

  10. Image source: https://github.com/awslabs/aws-lambda-web-adapter/blob/main/
    README.md

    View Slide

  11. ɹ

    View Slide

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

    View Slide

  13. It worked!

    View Slide

  14. Advantages
    • Can focus on Rails development
    • Mitigate cold start with API Gateway's Lambda Proxy
    Integration

    View Slide

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

    View Slide

  16. Lamby
    https://github.com/rails-lambda/lamby

    View Slide

  17. Lamby
    • An approach similar to AWS Lambda Web Adapter
    • Additionally, it operates without an extra process

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  22. Resources: Lambda x Rails
    • Ruby on Rails on Lambda - Speaker Deck https://
    speakerdeck.com/joker1007/ruby-on-rails-on-lambda

    View Slide

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

    View Slide

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

    View Slide

  25. END

    View Slide