Slide 1

Slide 1 text

Rails Web Development with AWS Lambda 2023/07/07 M3 Tech Talk #224 @ma2ge

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

ɹ

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

It worked!

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

END