00:00:00.399
hello everyone and thank you to be here with me today at trby day
00:00:05.799
2024 my name isano deaza and as uh Monica said today we're going to see how
00:00:12.559
to use Ruby our favorite language on an AWS Lambda
00:00:33.079
first well nobody cares but a few words about myself my name is emilano I
00:00:38.719
graduated in software engineering 2005 I co-founded uh two startups I have
00:00:46.120
worked for 12 years as an independent consultant and I've like almost 20 years
00:00:53.600
of experience in Ruby projects and Ruby ra framework I'm also the maintainer of
00:00:59.440
Ruby Sisk on GitHub that is a a library to interact with Sisk PBX uh and using
00:01:08.000
Ruby and I've also contributed to some other gems now I guess you all know there has
00:01:16.200
been a lot of backslash recently in our industry against Cloud providers and uh
00:01:23.960
cloud computing in general especially since 37 signals that has as you all
00:01:30.240
know it's the company behind base camp and the company created and actually maintains
00:01:38.479
rubian rails since they decided to quit AWS and move ever everything of their
00:01:47.799
infrastructure to a private Cloud actually it's quite a success
00:01:53.000
story because in just a few months they were able to move almost everything from
00:01:58.560
AWS as almost because I think right now just the file tier still uh resides on
00:02:07.240
s3e service also um I think you all know uh dhh is a
00:02:16.400
strong supporter of monolit well actually dhh has a strong opinion about everything in life and uh but
00:02:24.879
specifically in the monolit architecture that is uh when all your
00:02:30.840
code recite in a single application or in a single repo against a more hybrid
00:02:37.680
approach uh a more let's say Netflix approach where everything is
00:02:44.560
modelized and in microservices how they
00:02:49.840
called I guess I understand uh theh point of view still I think there is
00:02:57.239
still space in our little niche of Ruby AR programmers to explore other
00:03:05.040
approaches like the hrid one and as always uh if you ask me which is the
00:03:10.920
best approach I would say it depends now if you have been living in a
00:03:17.400
cave recently I'm going to explain to you what AWS is AWS stand stands for
00:03:23.400
Amazon web services is the main and only would say cloud service provider for
00:03:29.319
Amazon Marketplace no need to introduction here actually is the
00:03:35.519
biggest contributor of the um Amazon Global Revenue with
00:03:40.920
16 16% of total revenue also is the main
00:03:47.439
service provider in the market with a 32% of Market shs sorry Ricardo it's
00:03:53.079
it's the truth and it offers more than 200
00:03:58.640
services to to to the customers he has 26 Geographic zones
00:04:05.280
around the world with 84 availability zones to provide full tolerance and low
00:04:11.040
latency performances and Counting because they are adding more and more Geographic zones and availability
00:04:19.079
zones okay now let's dive a bit in what AWS Lambda is okay it's not over Lambda
00:04:25.960
and this is this is for sure what is an AWS Lambda a WS Lambda can be defined as
00:04:31.680
a serverless Computing Service as or FAS function as a
00:04:38.720
service it was announced the first time 2014 at the reinvent conference and
00:04:45.120
since then it has been considered as one of the biggest improvements in this
00:04:50.320
industry is event driven and it's fully integrated with almost every service
00:04:58.199
that AWS provid what does it mean event driven means you
00:05:04.600
take a service from AWS and based on an event on that
00:05:10.919
service you trigger a Lambda for example you receive uh an FTP file in transfer
00:05:17.440
family you can trigger a Lambda you uh create a file on S3 you can trigger a
00:05:26.080
Lambda or you can just call A as um as a
00:05:31.560
a URL and the Lambda is triggered it's really interesting
00:05:38.479
because it supports the most used programming languages out there java net
00:05:46.440
python um and of course Ruby and in different versions for example for Ruby
00:05:52.199
you can find CH version 2.7 3.2 3.3 and so on what's the business model
00:06:00.400
the business model is payper use meaning you pay for every millisecond
00:06:07.400
that you run that your code run you and for the memory that your
00:06:13.880
function is using for example for the first billion of requests per month you
00:06:20.520
pay 0.0000001 16667 USD per gigabyte second
00:06:30.319
now let's take a look what is the life cycle of a AWS
00:06:38.639
Lambda when you receive an event trigger at
00:06:43.880
first the code your code is downloaded okay and the new uh execution
00:06:52.360
environment is created those two Blue Block here then
00:07:01.039
the executing initialization code of your function and then finally your real
00:07:07.759
code is executed so what's the difference between blue codes and yellow code the
00:07:14.440
blue code is executed only when the when your instance is created the first
00:07:21.160
time and the yellow code of course is the request every subsequent request that arrives just executed the yellow
00:07:28.560
one it is also really interesting that AWS just charges you for the yellow part
00:07:36.720
and not for the blue part the blue part is free let's
00:07:42.039
say how how long does the instance last well actually it depends every time you
00:07:49.960
upload a new version of your code the instance is declared uh erased and the
00:07:56.199
new instance is created so for example in here you receive a request your
00:08:01.520
instance is created executed then you
00:08:06.599
upload a new version and the instance is created
00:08:13.120
again you understand that the cold start phase that creates a pre-or as they say
00:08:20.560
a pre-or environment could be both an advantage and a drawback why an
00:08:27.039
advantage I said it's free meaning that every initialization
00:08:33.399
code heavy duty heavy could be put in that phase it's free you're not paying
00:08:41.000
for it for example initialization of cry cryptographic libraries that could be a
00:08:46.279
a nice example but of course it can be a drawback especially if you need a fast
00:08:52.920
reaction to triggers this is where provision concurrency comes to help so you can ask
00:08:59.200
a AWS to keep uh a number of instances ready and pre- wared for you of course
00:09:05.800
for an extra cost for example we can ask to keep six instances
00:09:12.680
pre-worn they are initialized then it's they are ready in
00:09:19.399
the green area ready to receive requests and when a request arrives first come
00:09:26.079
first served for the first free here
00:09:33.240
that means that if you receive six requests at the exactly same time they
00:09:39.399
get served and executed in the exact same
00:09:45.000
time so for example if you just have two and six requests in a in a staggered
00:09:51.800
arrival the first is initialized and receives the
00:09:57.880
first request then you would receive the second one but the first is busy so the
00:10:03.800
second one is created and executed and so on so so they're cued and split
00:10:10.440
between the two instances accordingly what happens if we have just
00:10:16.240
one simple it's the the request the six
00:10:21.640
requests are cued and execute it one after one okay
00:10:27.560
and if you see the C start duration just happens at the
00:10:33.079
first actually it's almost true because AWS from time to time based based on
00:10:40.600
their algorithm from time to time erased working instances even if you didn't uh
00:10:47.880
upload a new function code but it's up to to them so you may ask what are the
00:10:54.320
advantages of faas well for us Developers
00:11:00.079
I would say that you can focus on code and not on
00:11:05.160
infrastructure so actually we can focus on what we like more you know and we can
00:11:12.360
take an advantage to the inent scalability of
00:11:17.639
Lambda on the other hand for managers what is really interesting is
00:11:23.720
the reduced time to Market meaning the time from writing code to code that goes
00:11:30.839
live into production is reduced and of course it's it's crucial for them and
00:11:37.360
the flexible cost no use of the function no pay you pay just for the execution
00:11:45.040
time of your code okay so let's start seeing some
00:11:51.800
Ruby code and as we said let's go back to to the basics there is nothing more basic than this one this is a simple
00:11:59.000
hello word um file example we create a
00:12:04.399
Lambda underscore function RB file with a simple function in it no object just a
00:12:10.240
function Lambda Handler with two input parameters that are predefined by AWS
00:12:17.320
event and context event cont contain contains all the information regarding
00:12:25.399
this particular event received in at this particular moment context is an object is a meta object
00:12:33.399
let's say about the function itself for example we can ask in the context how
00:12:39.480
many milliseconds are missing for timeout and behave
00:12:45.880
accordingly we love Ruby we know how does it work how J GPT the weirdest part
00:12:52.160
is returning the the the last directive is is the return okay so this function just
00:12:59.000
return turns hell word okay that was quite simple let's
00:13:08.920
take a look at a more complex uh example not that much um imagine your manager
00:13:16.760
comes to you and say okay I need a software component to send notifications
00:13:22.240
to our user using a simple HTTP request uh this component would be
00:13:28.480
called by one of the many softwares of our company written in Python Ruby Ruby
00:13:36.000
rail Sinatra and so on and uh so we need to support all
00:13:43.680
these different services so your developer say okay
00:13:50.320
let's go with the microservice approach why because first of all you want to keep it framework and language agnostic
00:13:57.480
so you can call it wherever you want and you want to take advantage of the
00:14:02.959
scalability and of the centralized
00:14:10.600
architecture at first you're going to use D for sending an not an SMS
00:14:19.120
notification so this is a a simple a simple file using the net HTTP Library built in
00:14:27.880
Ruby you create the the URI you take the from two body input parameters from the
00:14:35.720
event body uh field sender receiver and text we create a post request we add a
00:14:43.639
basic authentication please don't commit credentials in your code base I did it
00:14:49.880
just you understand but don't do it please don't try thisone we start the request we pass the
00:14:57.519
response body and we check the the Sid field that is the unique identifier of
00:15:03.800
our message okay cool this is working my
00:15:09.759
local machine we need to deploy to the cloud right great let's zip the
00:15:16.360
directory containing your code just the Lambda Handler RB will be
00:15:22.279
considered everything um else is optional actually there is a limit of of
00:15:29.720
250 megabytes to AWS so you zip directly you go to
00:15:43.160
function this form appears this is a wizard pretty simple this is just basic
00:15:49.680
then when the function will be created you are going to have some more settings to play with but let's keep it simple uh
00:15:57.480
right now so you specify the name of the function the runtime that you want to use the architecture you can choose
00:16:05.160
between x86 and arm 64 permissions and so on so we
00:16:13.199
proceed then we app close the zip file we just created to AWS we get a a URL to
00:16:21.759
send our HTTP request and this is an
00:16:27.040
example of the request specifying the input
00:16:34.040
parameters you understand this is not really uh a process you can you can do
00:16:40.160
in in every your everyday life right we want to automate because I I guess
00:16:46.880
everything agrees that every manual process can lead to errors out of
00:16:54.040
Destruction how do we automate this we can use it the servers framework
00:17:01.040
that is a library written injs unfortunately as far as I know there's nothing for Ruby but this is written in
00:17:08.160
JS it uses yam based configuration files is
00:17:14.039
declarative what is more interesting is extensible with plugins so we there's a
00:17:19.799
a long list of plugins we can use to automate and include every possible
00:17:25.120
service is really simple to install and use just install with
00:17:30.679
npm initialize the your application with L SLS and with SLS deploy you deploy to
00:17:37.640
the cloud and this is an example of a file
00:17:42.880
the service field is the name of your application in this case is HTTP to SMS
00:17:49.200
then the framework version number three last one is number four but we are using
00:17:54.640
the free right now then there is the Prov provider section with those settings that are valid for all your
00:18:02.200
functions so for example the name of the provider AWS the region where you are
00:18:07.600
going to store uh your and execute your code for example the timeout of your of your function the
00:18:15.720
size of the memory that the your function will use the architecture and so on and then a fun function section
00:18:23.960
with a list of all the lambdas that you're going to use the key of a yam is
00:18:29.799
the name of your function Handler states which is the Ruby file that you're going
00:18:35.640
to use um a stor storage size is the size
00:18:41.400
of let's say the drive of your function uh this memory is erased every time a
00:18:47.880
new instance is created so don't rely on this just for temporary
00:18:52.919
files and then we can set environmental variables for example in this case to
00:18:59.360
enable wet the new wet you just set Ruby igit enable one and this enabled the by
00:19:08.720
default then we specify it that the event that trigger the function is an
00:19:14.000
HTTP request with P send method post and enabling
00:19:21.600
course now your manager comes to you and says hey I heard about this Faraday Jam
00:19:28.760
it's pretty cool performance are are okay let's try this and you say okay she pays right so okay let's pay let's do it
00:19:36.919
we reive Faraday and this is just simple refactoring of our function I I won't go
00:19:43.120
into detail it's just the same it's doing just the same but we included a new jam right we need we need somehow to
00:19:52.280
manage all dependency so we create as good rubis we create a jam file with we
00:19:58.840
specify source the r Ruby version and we add that we want J faray at version
00:20:06.440
2.9 fine bundle install all the dependencies are taken care of we're
00:20:12.440
fine that's working unfortunately on cloud this is
00:20:18.440
not working could not find Faraday 2.9.0 in any of the source why is this
00:20:26.120
well we couldn't expect any different it WS simply cannot store any single gem or
00:20:33.120
Library out there it's impossible so we need to find a way to tell AWS to use a
00:20:40.840
specific uh the specific gem we need for our for our code and this is the reason
00:20:47.039
why in 2018 they introduced AWS Lambda layers what is a layer a layer is a
00:20:53.320
collection of gem and libraries used by your code you can have up to five layers
00:21:02.480
linked to a single function but you can use um a single layer for all the
00:21:09.240
functions you want you just need to link it and what is a layer a layer is simply
00:21:15.200
a um a a directory a simple directory with a specific a specific uh structure
00:21:23.200
like for example in rubies Ruby jams 33 for Z and and that's it we store the jam
00:21:29.640
here zip it upload to AWS too complicated let's use a plugin
00:21:37.279
serverless Ruby layer plugin for serverless we install it we include it in the plugins over
00:21:45.960
here and what does it do every time we deploy our code to AWS this plugin reads
00:21:52.960
the J file and do all the manual process for us install the bundle install in in
00:21:59.039
a file zip sand and Link
00:22:04.559
magical now there is a a third a third way to um create and manage lambdas on
00:22:15.240
AWS and is using containers and Docker and Docker
00:22:21.080
files well uh I won't explain what Docker is because I'm not the right person to do it and there there is time
00:22:29.440
enough today let's say for today that a Docker file is just like a make file to
00:22:36.120
create virtual environments uh a Docker expert will beat the hell out of me today but sorry you would excuse me just
00:22:43.960
keep with the definition we we start from a basic image taken for AWS ECR that is a
00:22:52.480
service to store to is a repository for containers uh we copy the gem file to a
00:23:00.159
specific directory run gem install bundler copy our
00:23:07.360
file and this is the entry point we specify the name of the file and the
00:23:13.880
name of the function containing it and it's easy to manage it using
00:23:21.200
serverless we just need to add an ECR section with all the information with
00:23:27.200
the the pet to the do profile and Link it in our function and serverless will
00:23:33.720
take care or build the image send to AWS
00:23:38.880
and link to your function what are the pros and cons of
00:23:45.600
fer Docker well since under the hood when you send a
00:23:52.200
simple Ruby file this is what AWS do it copies your file creates a container and
00:23:59.080
execute it so you can replicate that behavior on your local machine you build
00:24:05.840
with Docker run it and invoke the
00:24:10.919
function using this URL on your local machine so you can replicate the behavior of AWS on your machine so
00:24:19.360
there's no need anymore to test your code on on uh on cloud and there is no
00:24:26.840
need of an external plugin more because everything is taken care by the docker
00:24:32.919
file of course there there is a slower deploy time because every time you deploy your image the image has to be
00:24:40.880
built sent and so on so you have to take it this into
00:24:47.720
account now if you're truly rubis we are missing a part here right automated
00:24:54.039
testing what if you have to test automated test test your code always so
00:25:01.520
I wanted to add our spec to our G file to include test in the group development
00:25:09.399
test we need to modify the docker file because of course I don't want
00:25:14.559
development um development libraries on production so we
00:25:21.360
set bundle to exclude development and test groups and everything is just the
00:25:27.120
same and at last we create a spec
00:25:32.880
directory with the Lambda function spec in it and it simply we give valid inputs
00:25:40.640
to our function and expect uh a valid output of course since you don't want to
00:25:46.120
receive an SMS every time you run your test suit we are mocking faray connection to
00:25:53.320
receive a post request and with this response
00:26:01.480
so let's make a a more recap of what we learned today today we learn what an FAS
00:26:08.080
is a function as a Services we learned what is the basic Lambda structure for
00:26:14.559
Ruby we learn how to deploy automatically uh without effort using
00:26:20.399
serverless framework we saw how to send um u a
00:26:26.760
Lambda with code or with a Docker image and we saw how to test it with our
00:26:32.960
respect thank you very
00:26:39.600
much if you have any question please thank you so so much you're
00:26:45.120
welcome um I wish I had this talk last
00:26:51.919
year I'm one year late yeah no I was trying to figure out I was like I've never done it I had some free time I was
00:26:57.840
like okay let's let's dig into this but this is so much better than doing it on my
00:27:03.679
own um we have a few minutes before our next uh speaker are there any questions
00:27:10.679
that you might want to ask
00:27:15.960
emilano I know it's awkward silence but that's normal I'm uh how how far would you take
00:27:25.399
this approach how complicated
00:27:33.440
Soo I have the following question how far do you think this approach can be taken because
00:27:40.200
um did I okay uh so so um I'm wondering uh what
00:27:46.679
are the complexity boundaries uh obviously we cannot run a full-fledged web application well I guess I guess
00:27:53.640
there are limits you have to respect for example uh
00:27:59.360
if you ask me okay should I refactor my Ru race application and every single
00:28:06.519
controller action goes in mic microservice I will say no would you ask
00:28:11.720
me okay would you chain five or 10 lambdas one after another one no I
00:28:18.200
wouldn't do it I think as I said there is the uh all monolitic approach the dhh
00:28:26.440
favorite or the full full hybrid all microservice approach I think that the
00:28:32.919
to be wise it's mean in the middle and understand which parts are you can take
00:28:40.519
advantage of the microservice approach the more scalable uh the one that can be
00:28:47.640
used without prediction you don't know how when or how you are going to
00:28:52.840
receive when you have for example stable requests I will go with the monolitic one
00:28:58.679
okay hope you answered I I answered your question no this makes sense and if I may ask a short followup and how how
00:29:05.679
would you compare this to doing the microservices on something like kubernetes which is another popular
00:29:11.240
approach uh yeah I I think the competitors I mean uh for example you could also use fargate or kubernetes or
00:29:20.360
kqs kqs uh yes I think it's kqs the the service fors of of course those are all
00:29:28.679
competitors and uh I guess kubernetes is much more complicated when it comes to
00:29:36.679
orchestration AWS Lambda is just send us send a function and you're done so if
00:29:43.080
you want to Pursuit a a simple approach I would go with AWS if you want to try
00:29:48.279
if you want to play with it uh always being ready to change and go to forgate
00:29:55.799
to kubernetes or to an old simple bare metal server if if you see that it
00:30:03.679
you're falling behind you know thank you very much you're welcome thank you more
00:30:13.440
questions yeah oh
00:30:19.840
thanks have you ever tried Jets and uh uh how do you compare this approach with
00:30:27.440
ruon jet you tried that yeah I tried I tried using it actually is quite
00:30:34.799
interesting uh still I'm what what
00:30:39.840
concerns me more about Jets well maybe time has passed so maybe things have
00:30:46.039
have changed but Jets is really AWS Focus right you can just deploy to
00:30:53.760
AWS uh another advantage of the docker uh approach I showed just a few minutes
00:31:00.600
ago is that you could take this Docker container and change a little of course
00:31:08.399
and push it to Google to make Ricardo happy or to Azure and so
00:31:13.919
on with jets you're just bound to
00:31:18.960
AWS uh what would I prefer sincerely is
00:31:25.760
I would like to find a gem that takes a real application and push it to AWS aure and
00:31:34.679
so on I really would like to be able to do it maybe once sometimes I will try
00:31:41.240
but this I think it would be much more
00:31:59.960
J