What Are Serverless Functions?

A serverless function is something that is going to allow us to do server-like things without having to manage servers, but an important distinction here is that serverless does not mean that there are no servers.

The environment is actually running on top of an operating system and uses physical servers, but the responsibility for provisioning and managing the infrastructure belongs entirely to the service provider.

Some of the benefits of using serverless functions:

  • Enhanced scalability. No need for developers to implement code to scale. It scales automatically.
  • Reduced time to market. Serverless is an optimized set of tools for building quickly.
  • No boiler code. A serverless function is just a tiny piece of logic. Reducing the complexity of software.
  • More time for user experience. Serverless functions allow teams to focus resources on the user experience and not on managing servers.
  • Security. We can call third-party APIs securely without exposing important data on the client-side.

Transcript

Lecturer: [0:00] What's up, you all? I am so excited to talk to you today about serverless functions. Serverless functions are one of my favorite things. I use them all the time. There are a lot of reasons that I like serverless functions, and we're going to lay all of those out in this presentation.

[0:15] Why don't we jump right into it? With the first thing, which is, what is a serverless function? A serverless function is something that is going to allow us to do server-like things without having to manage servers, but an important distinction here is that serverless does not mean that there are no servers.

[0:36] The difference is, with serverless functions, what's actually happening is we are no longer needing to manage servers. The way this works under the hood doesn't really impact you if you choose to use these, but so that you know, a serverless function is where you, as the developer, put a little piece of logic onto somebody else's server, AWS, or Google Cloud, Azure, whatever service you're using. They've got a bunch of server space that they're not using.

[1:06] What they let us do is, with serverless functions, we can put a little bit of business logic on one of those servers. They'll automatically manage the scaling, the configuration, provisioning, all that stuff. Then we can just call that function. It's a way for them to monetize unused server space.

[1:22] For us, we don't have to deal with that initial setup. We don't have to go to AWS and figure out what kind of server we need. "Is it EC3? Is it something else? I don't know." Then you don't have to go through and figure out how to orchestrate that with your gateway, with your proxy layer, with your routing, all these different services that you'll need to get that going.

[1:42] I think you'll need at least three services. You might be able to get away with just those three, but there's a lot that goes into this to set up a server.

[1:50] Then you'd also have to worry about how it's going to scale. What happens if this thing gets really popular? What happens if you end up on the front page of Product Hunt or your site has a big launch sale and everybody shows up? You don't want your site to go down.

[2:03] We've all had this happen, where you go to a site and it's overwhelmed. Now the site gives you a 500 instead of actually serving you the content you wanted, and you just have to wait and keep refreshing and hope that you're one of the lucky few who can get that server while it's still up.

[2:17] That's not a good way to work. That's now how we want our apps to work. In serverless, because it's managed on a cloud provider, is going to auto-scale for us. It means that we don't have to worry about that. We don't have to think about scale. It's going to be managed automatically.

[2:30] It also means no boilerplate. This is maybe one of my favorite things because a serverless function is just a tiny piece of logic. All the boilerplate, the routing and all that stuff-that's managed for us.

[2:42] That gives us a ton of extra power because we don't have to think about, "Are we using Express?" or "How are we going to handle logic?" "How does the HTTP stuff work?" We don't care. It's managed for us. We just get to write some business logic.

[2:57] Serverless is a way that we can approach development that lets us worry less about how things are done, and it lets us focus on building more. I want to when I'm working on an app get straight to the point where I'm making the app do something.

[3:11] I don't want to get bogged down in that Yak shaving of having to set up the foundation, build all my boilerplate, do the testing, do the configuration. Make sure that my server actually runs, so that I can then write the business logic.

[3:25] I want to skip all that. I want to get right to the business logic and start building my app. That's where the value is. That's where the fun is. That's where all the good stuff about development lies both from a making my project or business, or product better, and for me generally enjoying things.

[3:43] Very rarely do I want to do the same task over and over, and over again. I always want to work on something that is going to challenge me. That's going to be novel that I'm going to learn something from. Serverless functions let me do that.

[3:56] I don't have to worry about the repetitive stuff, all that boilerplate, it's done. I can move right to the part of building, so it lets me move fast, and I love that.

[4:07] The way that I like to think about this is if you're looking for the best tools, if you ever seen a Formula One pit crew before. They look like they're going in fast-forward, but what they've actually done is they have removed all of the cruft around how to do a pitstop.

[4:26] Instead of having to unscrew five or six lug nuts, they have one big one. They've got all these specialized tools that allow them to go very quickly.

[4:36] They focus on just the thing they need to get done. Getting that car ready to go back out on the road and none of the mechanical junk that would slow them down. That's how I feel about serverless. Serverless is an optimized set of tools for building quickly.

[4:51] What I like about it is, like I said, it lets you focus just on your app logic. This is a whole serverless function here. We have a named handler .export.

[4:59] That is an async function that returns an HTTP status code. In this case 200 which is OK, but you could return a 404 if you wanted to say "Hey, this wasn't found" or a 301 if you wanted to send a redirect, a 500 if something goes wrong. Then we send a body.

[5:13] There's more to what we can send headers, things like that. All of which we'll cover in the serverless course, but at its essence, this is an entire serverless function. You could deploy this and nothing else, and your function would work. That's why this is so exciting.

[5:29] We get to focus just on what the function does, and not on how it works or how it's deployed.

[5:35] We also get to handle user interactions dynamically without having to set up a full-fledged server. If we want someone to have a Like button, they can click or a form they can submit, all we have to do is set up a serverless function that can listen for that submitted event.

[5:51] We submit our form, or we send an event through the HTTP POST or GET or PUT, whatever we want to use, we send it through to our handler. That handler can pull it out of the event body, and then do stuff with it. In this case, we've got some kind of a handler utility that looks for an ID sent into the event body. Based on what we get back, we can respond.

[6:11] That way, our app can be a completely static app, which is my preferred way to build. We get to build with the JAMstack. If you were moving parts, everything feels a little bit easier. Serverless is the dynamic counterpart to that. If you were moving parts, we've just focused on the business logic. We get to build something quickly. We don't have to futz with the servers.

[6:30] Another benefit is that we get to call third-party API's securely. A challenge when you start looking at the JAMstack way of building things is that you do need to use secret keys and you can't put those into public code.

[6:43] If I need to make a call to a third-party API, I need to do something authenticated, I need to do something with a privileged access token, I couldn't just put that in my client code. Somebody could inspect the source code and dig that out and then, they could impersonate me.

[6:57] What serverless functions let me do is get the benefit of protecting those keys in a server-like environment, but again, without all the boilerplate management of setting up a whole server. That's what this looks like here. This is a call to the Unsplash API, which we'll deal with in the serverless course.

[7:14] In it, we're using the Unsplash access key which needs to be kept secret as part of the Unsplash Terms of Use. We did that by putting it into our environment variables, which can then be accessed by the serverless function. That would not work on the client-side because that access key would end up being exposed.

[7:33] In a serverless function, it won't be exposed, which means that we can safely make protected calls to get resources. This serverless function effectively acts as a privileged proxy to make a call to a third-party app that we need an access token for. That's a really powerful workflow that we'll dig more into in the course.

[7:50] You can also handle user authentication. You can handle working with a custom database. There's so much that we can do with serverless functions. Pretty much anything that you can do with a server, you can get into with serverless functions.

[8:02] There are some exceptions, and we'll talk about those. Most notably, real-time. If you want to do something real-time, right now, serverless functions can keep a long running connection.

[8:11] That's really the only major limitation, there might be some other edge cases. For the most part, for the 99 percent of what you'll do in your day job, that's really the only time that you wouldn't want to look at serverless functions.

[8:24] Because it strips away so much boilerplate, because it strips away so much code that can just get in your way, serverless functions give us the ability to write our business logic and ship safely. We know that code is small and contained. It's in a GitHub repo, we have the ability to roll it back the same way that we can with the JAMstack app.

[8:45] If I commit this to my GitHub repo, and I decided that something didn't work, I can just revert that commit, redeploy, and it's rolled up again. We don't have statefulness, we don't have any consistency problems. It's just whatever is deployed in Git can be what's deployed to our serverless function. We're also able to ship simpler, we eliminated so much extra code.

[9:09] Our simplest function is like five lines of code. That's really powerful when you compare it to something where you need 150 lines of boilerplate just to get something out the door that's really functional, and it lets us ship faster. We can build our first serverless function in under a minute and get it deployed. That's a really powerful workflow.

[9:32] My whole goal here, the reason that I'm so excited about the JAMstack and about serverless is that I don't want to spend time doing the setup, I don't want to do the Yak shaving, I want to build.

[9:42] I want to build things, I want to get them out into the world, I want my team to be able to get things created and shipped so that we can start getting feedback, so that we can see the real value, and so that we can move on to the other things that we need to ship, because we've all got a big backlog. We've all got a lot of things that we need to create.

[9:58] I want to get that done. I don't want to spend a bunch of time arguing with the boilerplate or the configuration of my code. I certainly don't want to spend that time arguing with my provisioning, with my orchestration, with my scaling.

[10:10] I want all of that DevOps stuff to just be done. I want it to be done by world-class experts at cloud providers like AWS, like Google, like Azure. Any of these companies that they hire the best in the world to manage those things so I don't have to. I'm going to take advantage of that with serverless functions.

[10:28] I am so excited to walk through this course with you to teach you serverless functions. I want to give you and your team the ability to start working and shipping faster with less red tape, less worry, less PagerDuty, less of those things that slow us down and make us nervous.

[10:44] I want to be able to ship with confidence, ship with speed and get through that backlog, get us to the high-velocity, high-producing team that we all want to be.

[10:53] I'm really looking forward to the rest of this course. We'll see you in there.