Yes, quick recap.
This is me, Skinna Kewarkin.
It's out there.
AWS Lambda is a function as a service.
So we can take our swift code and put it in the cloud and run it.
In any way, we don't have to manage the server.
So it is serverless.
So that also just means we can put it there.
We don't have to worry about suddenly we get like data.
So we got like super popular and we have a bunch of requests.
It will just handle that all of for us.
And then quick.
Some pricing about it is it's dependent on the number of requests that you have.
And then the duration of those functions to execute.
So for the free tier, you get 1 million requests per month.
So if you just want to play around, you'll be totally fine.
And then it's also dependent on your code to execute.
But that will then depend on where you throw it.
Like as in what kind of memory you give you allocate to it.
So if you have like you know more memory, it takes more time to run.
But like, sorry, more memory, but it takes less time to run.
That pricing though will still be different than if you allocate less memory.
And it runs like longer.
Okay, let's just do some live coding because I like demos.
So let's kick this off with.
Can everybody see the font all the way in the back.
Yay. Yay. Cool.
Okay, so what we do is make a swift package.
The first thing we do is type this.
So swift package in it.
Type is executable.
And then we're just calling this my lambda.
But you can see the example that I have.
I actually called it demo with multiple O's because I had many demos going on.
So this was the demo that made it.
And we first go to our swift package.
So we have package.swift.
And this is how we add any other external packages into the application.
So what we do is add the swift lambda.
Or yes, swift AWS lambda runtime.
And that is the runtime that actually like lets our swift code interact with how AWS lambda is set up.
And then we are adding swift AWS lambda events.
And those events are how we communicate with stuff like the API gateway to use wrist network requests.
And then like every other swift package.
Does anyone not done swift here?
Cool.
This is easy then.
You know what this looks like.
So then we have our demo and we add those dependencies to it.
And that is your basic package.swift for like pretty much everything.
So here is our main.swift file has the basic yellow world when you just make a new package.
Let's turn this into an actual lambda function.
So here is our hello world of lambda.
We defined the runtime which is actually a closure as you can see and it takes two parameters.
Event which is just a string.
And then the lambda context which is what actually talks to an AWS lambda.
And then all we do is run runtime.
So let's go ahead and do that.
We can do swift run.
I don't know how to make the figure on that.
So just trust me it works.
We have swift run and it just runs hello world for us.
And did I miss?
Did I hear something?
Save smart.
There we go.
Makes sense.
Do we not have the set to auto save?
It happens.
So we are running our hello world and it's running a local lambda server.
So that's what I was expecting.
Is running our local lambda server on our Mac here.
So now let's actually call to that.
And we do that by using curl.
And we are making a post request and sending it this string.
So it'll say hello.
And then it is on port 127001 slash invoke.
And you can change that to something else if you want to.
But by default, this is what it is.
So when we do that, we get a basic hello.
So this means that we were able to send the string via a post request.
And then it just prints hello.
So that looks pretty great in all.
But let's make it a little bit more complicated.
So let's do.
So we're going to close that.
Or actually I could just run it again.
So I reran the program at the bottom here.
And then this is a slightly more complicated example.
So what we first do is again import the lambda runtime.
But we're now also importing lambda events.
So this is what allows us to now use the API gateway.
The two requests because we are using the version to AWS lambda,
which right now I'm running off of the main branch because it's not yet officially tagged as 2.0.
So if you were going to do this, you have to just use main up until the point they tag it and everything.
So we have our requests.
And then we also return a response as opposed to previously returning a string.
And all we are doing is taking this adding the header of application JSON.
And echoing back out the request that we gave it.
So you can see we are returning this V2 response.
And in the body is this event parameter, which is just the request.
So what we will get out is the exact same thing that we get in.
So I went ahead.
Did I save it in run at this time?
It is.
Looks like it is.
We'll find out.
So I run curl again.
And then we are adding application JSON.
And then I have this test event dot JSON.
So that's just hanging out over here.
Because we take in now an API gateway V2 request.
We're not taking in a string.
That whole JSON is the whole proper format of what that request looks like.
So that's why I'm just grabbing it from over there.
And we're not actually doing tests, but I just grabbed it from over there.
And then you see an output that we have is in the body.
It is just that request that we sent it.
So this whole thing is the same as that event dot JSON that we sent.
So this looks pretty good.
I mean, this is an amazing function.
It's very useful.
I mean, technically it is if you're testing something.
And so let's just go ahead and deploy this.
So the nice thing is we will be using SAM, which is the serverless application model,
to deploy everything.
So that allows us to just write a YAML file.
And then we can immediately deploy everything because that YAML file sets everything up for us.
So let's make a new file called template dot YAML.
And here is our template.
Yes.
So here is the template that we have.
So it is a Lambda function.
We call it demo with two O's because typos.
It's a function name.
And then right here is the full build path for where we are going to create a zip file.
So how we actually deploy is it we zip everything up.
Send it up to AWS Lambda.
It provisions everything properly based on this template that we're writing right here.
So we have a timeout.
This bootstrap provided AL2.
So I believe that's Amazon Linux 2, which is the operating system that's just provided by Amazon for running this environment.
We set our debug level and then this events part is what we are using for the actual API gateway for sending that specific kind of requests as opposed to sending a string.
So we do that.
And hopefully we can zip everything.
So we zip everything using Swift package archive.
We are disabling the auto update because once when I went to try this this morning,
it tries to update the operating system.
And I was like, no, I'm not doing that on on a live demo.
And then we have allow network connection requests because I think like you and had said packages run in a very siloed way.
And we don't want them doing things they don't want to do or we don't want them to do.
But we are allowing network connections for this.
And I should have already hit enter while I was explaining that and now it's not.
Give it a second.
Let's go think about it.
This internal working backs.
Okay, backspace works.
I saw it move.
Try to get an zip demo.
Oh, there we go.
Okay, it first asks us allow this plugin to run in Docker.
So what we are actually doing is compiling everything, making our zip, but we have to use Docker to do that.
Because I am developing on macOS and the environment that it ends up running in is Amazon Linux 2, which is a Linux environment.
Come in, not found.
Did it work though?
It looks like it works as archive created.
There we go.
How did it work though?
Like, you see that right?
Okay.
We're good then.
So we have our zip.
It is now at this URL, which is on my desktop.
And then you can see that URL matches this one because this one is relative from build forwards.
And so because of that, it will use this to actually find the zip file to go and upload it.
So let's upload this amazing function.
And I have to spell the play right.
Oh my gosh.
Sam deployed guided.
So once we hit enter on this, it asks us a couple of things.
So what do we want to call this?
We call it demo.
I believe this is my account.
It's set up on us east because, you know, I'm from the US.
And that's where I originally set everything up.
Confirm changes before deployment.
Yes.
Enter.
Do we want to allow Sam CLI?
I am.
Roll.
Roll creation.
So that says, do we want to allow.
Want to allow Sam to create all the proper roles necessary for creating this function?
And they answer.
Yes.
Disable robot.
Sure.
This has no authentication.
Is this okay?
So this is a demo.
So I'm not going to add authentication.
But you should when you actually make your Lambda functions.
Save arguments.
Yes.
Config.
So these will save all of these configurations that I'm going through right now into a file called
Samconfig.Tommel.
Entomel.
It's just another kind of email.
I mean.
And then it starts building.
And uploading.
So we took.
Take our zip file.
And this internet is actually pretty good.
It is taking that and uploading it to.
80 to eight WS and it's using S3 to do that.
So you could see somewhere in here.
Deploy.
Yes.
Somewhere in here it said.
It's creating an S3 bucket.
And that's where that zip file is actually living at the moment.
Did I not turn?
Do not disturb.
Hmm.
Oh, it's on.
Okay.
That can just bypass it.
Is it going to do it?
It was going pretty well.
Not too bad.
It has thick and slower.
But a lot of times too when you zip everything together.
And if you haven't done that before.
Because it's using Docker.
It has to pull down the entire operating system as well for like.
The Amazon Linux to OS to actually zip it all together and send.
So right now.
So doing pretty good.
Hopefully we can get it.
Oh, there we go.
We have successfully deployed our lovely function to the cloud.
So let's see if it actually works.
If we copy this, we should be able to write curl.
And then paste that URL.
If my auto complete.
Oh my gosh.
Doesn't freak out.
Debited during this off.
Let's see.
Second.
Man.
Quit.
Oh no.
It doesn't quit.
Got a fourth quit terminal.
Oh my gosh.
Where are you not responding?
Yeah.
Report.
That's fine.
Okay.
There we go.
Paste it at curl.
And then the link.
You're welcome.
Someone's self-aware me.
Sebastian, you're still here.
So then we have.
Curl and then pasting your URL for where it was just deployed to.
And we should be able to enter.
And we see coming back is the request that we had sent because the code that we had was
the request response.
So it's successfully deployed.
But we can make this a little bit more fun because this is just your lovely demo.
So let's make a new file called.
Food handler.
Swift.
So I had to go through and find all the great Belgian food to eat, which includes fries,
chocolate, waffles, and beer.
And I think I've had three out of the four of these already.
So it's been good.
And let's change our demo instead of echoing back.
Let's do this.
And we're just going to use our basic food handler class.
Or a struct that I had just created.
All it is is just the struct with a function in it that returns a random one.
But by default, it's for us.
If that shouldn't be the default, let me know.
We have our food handler.
And then we are just taking the food handler, giving us the best food.
And that'll just give us a random element.
So we can, again, run this with just Swift run.
And it ran and then it was.
Yeah, this one.
We just type curl.
We give our response.
And then we get back the correct answer of beer as what kind of food to have right after this.
So we did that.
And all we have to do is deploy this.
So we've already done all the initial setup to do everything.
So now to deploy, we just have to rezip everything.
Which you can, this still had the wrong shortcut.
You can put these steps together to be able to just zip and then send.
I'm doing them separately because.
But all does is takes our demo.
Again, use this Docker to build the entire zip file.
And then we will go ahead and send it off with just Sam to deploy.
We don't have to ask all the questions anymore because we have them all saved.
And the Samconfig.Tummel file.
And then it's doing the exact same thing.
So we're using a different S3 bucket because we have a different zip file that we're using.
And it will go ahead zip it all together and say, let's send it.
Waiting to be.
Created.
I wonder if it would be faster if I picked a region closer to here.
Maybe that's why it's slow.
But we said yes.
And then now again, it'll go through those steps to take that zip.
Send it to the cloud if we do a curl command.
Again, we will see.
Yep, put.
So that is the basic demo.
And what's super nice is because this is Amazon AWS Lambda.
It uses the version two, which also uses the.
We can use Swift testing.
So I couldn't think of a unit test to write for this because all it's doing is returning a basic.
Element from an array, but right here you can see an example using Swift testing, which was just released this pass to dub dub and see.
How you can unit test this.
And everything is also possible.
So when you go to the Swift AWS Lambda runtime.
It has a bunch of examples for what you can do how to do it.
So using an actual example that has authentication.
And you can check out that repo, which I will link right here.
Has the link for all the things that you need to go ahead and then.
See those examples.
And then the other link that's on here is the doc sea tutorial that's I believe on like the Swift package index.
So you can also like walk through it step by step of all the steps that we kind of went through here.
So that's it. Thank you.
Thank you.
Amazing.
You're part of it.
Yeah.
Can you tell us about the latency for the first call?
You don't think you've set on this one.
Yes.
So the latency.
Yeah.
The latency.
Yeah.
The latency.
I have seen it not be too bad between like the very first call and then subsequent calls.
Because they are running on whether they're called Amazon.
Fire via something like it's like a lightweight version of a VM.
So it's not the entire virtual machine running.
But it's like a smaller kind of box from that.
I can't remember the name of it right now.
But because of that, the latency is lower.
I don't know the exact number of it, but it's not too terrible.
We can also see because it's just deployed.
Like that. Then we get beer again.
So that's obvious answer.
So I have to go, but thank you again to everybody.
Amazing.
As always.
One of my favorite members of the community with the most infectious personality.
And the best part about Michaela is her willingness and even
this to learn in public.
And make it amazing.
So that's it.