Intermediate Gatsby, v2 Serverless Function Setup
Transcript from the "Serverless Function Setup" Lesson
>> We are now in pretty good shape for diving into these serverless functions. So serverless functions are new thing to Gatsby, not a new thing in general. So if you look at, when the Jamstack came out as a general architecture, right? There's this idea of precompile things, decouple your front end, serve it on a CDN, these are such powerful concepts, but there are things that you'll need to do that can't be done client site.
[00:00:36] You'll find yourself in a situation where you need to, for example, process a form input or send off an authenticated call to an API. If you're working with a third party service, I wanna make a request to auth zero to see if somebody is logged in. I'm going to get a token that has to be private.
[00:00:58] I can't put that in my client side code. So that creates a lot of challenges for front end developers because you'd find yourself standing up a server. For one thing, like all I need to do is make this auth call, but I have to stand up a whole server for this.
[00:01:18] And there's a lot of boilerplate that goes into it. It's and it's not just the code, like standing up an express server is pretty fast. if you've done it before, or if you look at FASTA phi or some of the other options out there, they've made it very straightforward.
[00:01:32] So this isn't knocking on Technology. It's more that after you build your express server, so you write that boilerplate, right? You've got to set up the server, you've got to set up routing, you've got to do all of that. Then you have to get that deployed somewhere. So you need to figure out how do I get a node service.
[00:01:53] Deploy to the internet. Okay, well to do that I probably need to install like Docker or something because I'm gonna need to deploy this out to whatever service and if it's gonna scale I need to be able to deploy it to a lot of machines and, well if I wanna be able to do that maybe I need Kubernetes.
[00:02:08] And suddenly you're so far away from the original problem you started out trying to handle an auth call that was what you wanted to do is send an auth call. And you've moved three, four steps deeper into the stack, trying to figure out how to make sure this auth call is globally available.
[00:02:24] And it's going to stand up to your launch day traffic because you took out a giant ad somewhere in New York City. Expecting, hundreds of 1000s of people to try this at once. There's, all these things, that just start, to pile up and suddenly you're not doing the job, you're doing all this yak shaving to get you to the job.
[00:02:41] And that is not fun, that's not what we wanna do, that's not how we wanna spend our time. So instead, we want to be looking at, how can we, Just do the thing we came to do. And that's where serverless functions emerged as a really good pattern, especially for jam stack sites.
[00:02:59] Because in the vast majority of cases, you have one off server needs, make this off call, process this form, pull this image that only comes from, you wanna circumvent cores.Something like that where you wanna do a thing, you can't do it on the client, you don't wanna set up a server.
[00:03:18] A server function, lets you write just a little bit of code that is server like And then it will automatically deploy and scale and do all the stuff that it needs to do. So all you have to do is actually write the little bit of code that you need.
[00:03:33] That is the power of this this right? I as a front end developer or front end focus developer, I spend most of my time building for the web. I can and I have done DevOps II things. I can muddle my way through whatever tutorial to figure out how to scale a server and make sure it doesn't fall down.
[00:04:16] Suddenly, I have a lot of capabilities that I otherwise would have talked myself out of, well, I'm not gonna deploy this image service because i don't want to set up a server for it. It would cost me a lot of money to run a server that could handle this type of work.
[00:04:30] So, I'm gonna skip this project. Now with serverless like serverless deploys for free you can get a whole lot of serverless usage before it starts to cost you money. So suddenly I'm unblocked, I can have a lot of ideas. I can try a lot of weird stuff. And it's fast I can deploy it, I can get a server less function deployed.
[00:04:47] Actually, why don't we just write our first server less function and just look at how fast this is to do. So I'm in my source folder I'm going to create API and let's just do like a Hello.js. And for this to work, we go export default function handler.
[00:05:04] This is going to give us a request and a response object, and I can send back something like request, send, OK. Right, now this is not a particularly fancy setup, but if I run my server now, Because we're using Gatsby functions and because netlify dev can can recognize that and run everything, we're able to locally, work with server light capabilities.
[00:05:33] And then when we deploy this, it's gonna just work. And so Gatsby sets us up where we go to API and then I go to Hello And it says, okay. And if I go in here and change it and I can say, hello, front end masters, and I'm going to refresh this, and there we go.
[00:05:48] So I'm now locally developing with node. If you've seen express before, this is kinda the express. pattern where you get the request and the response object and then you can do whatever you want with it. And like if we wanted to send back JSON we can just say, the message is Hello front end masters and now we got JSON back.
[00:06:11] So there's a lot here there's a lot that we're capable of doing without having to learn a lot, without having to figure out. If I push this it's just gonna be live I don't have to deploy it. I don't have to scale it. I don't have to think about what my uptime guarantees are.
[00:06:28] This is just up and running. And it's globally available and it's fast and I'm happy. So that's the power of what we're doing here is we're really unlocking a whole new area of innovation for web developers by introducing these serverless functions. So if you've never tried them before you looked at them you thought they were too hard or it was just, I don't wanna learn another thing.
[00:06:49] It's too late you already learned it I'm sorry. And now you can do it, so don't let this block you anymore. Go out there and try stuff go build some ambitious things.