Serverless functions are functions that are hosted on a managed infrastructure, otherwise known as the cloud. They take all the responsiblity of maintaining instances, uptime, and scaling onto themselves. As a developer, all I have to worry about is my functions that deliver business value. Can you imagine all the extra time you will have to code?!
Periodocally, I will get a great idea (or what I think is a great idea) and want to build it, but I don't want to spend money on it yet. In this scenerio, serverless functions have been a lifesaver. Most cloud providers will also allow you to have a certain amount of function calls per month for free, which means I can develop a live solution for free until it starts to get use. This is huge for startups!
The billing is really great, too. I only pay for the function when its running! I don't have to pay for an instance to just sit there! This may not mean a lot to a dev that is working in a big company, but this kind of information is very important if you ever want your business to adopt the technology. Financial savings are a huge part of the business.
There are many cloud computing companies that have serverless functions such as Google Cloud Platform, Amazon Web Services, and Microsoft Azure to name a few.
I can talk about serverless all day or I can build one and show you how it works. Feel free to follow along. For this example, I will be using Lambda Functions on Amazon Web Services, also commonly known as AWS.
Let's look at a simple hello world function:
const plainOldFunction = () => {
console.log('Hello World');
}
module.plainOldFunction = plainOldFunction;
After invoking...
const { plainOldFunction } = require("./functions");
plainOldFunction();
We see the expected output of "Hello World"
Here is a function using an AWS handler as the function that is invoked in the cloud. We expect an event object (reference example below). We expect to have access to an object on the body of the event with the field with the name "firstName"
const awsHandler = (event, context) => {
try {
console.log(`Hello ${event.body.firstName}`);
} catch (error) {
return console.error(error);
}
};
This is what an AWS event looks like as an argument when a Lambda function is invoked from an api gateway. More info about the api gateway can be found here.
{
"resource": "/",
"path": "/",
"httpMethod": "GET",
"requestContext": {
"resourcePath": "/",
"httpMethod": "GET",
"path": "/Prod/",
...
},
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"Host": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5e66d96f-7491f09xmpl79d18acf3d050",
...
},
"multiValueHeaders": {
"accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
],
"accept-encoding": [
"gzip, deflate, br"
],
...
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"body": {
"firstName: "Josh"
},
"isBase64Encoded": false
}
We invoke the function locally (with the example event from above) to show the function works. I also recommend adding unit tests to cover the functions.
const event = require("./lambdaEvent.mock.json");
awsHandler(event, {});
We see the expected output of "Hello {provided first name from event}"
I hope this demystifies the world of serverless functions. I recommend taking a look at the AWS Lambda documention at https://docs.aws.amazon.com/lambda/?id=docs_gateway for more in depth information. You can also read up on the various available AWS services at https://docs.aws.amazon.com/.
If you have any questions please feel free to reach out to me via Twitter or Github.
Happy Coding!
Josh Oppenheim is a Senior Software Engineer at Thisdot Labs.
Twitter: @JoshOppDev
Github: Syntactual
This Dot Labs is a modern web consultancy focused on helping companies realize their digital transformation efforts. For expert architectural guidance, training, or consulting in React, Angular, Vue, Web Components, GraphQL, Node, Bazel, or Polymer, visit thisdotlabs.com.
This Dot Media is focused on creating an inclusive and educational web for all. We keep you up to date with advancements in the modern web through events, podcasts, and free content. To learn, visit thisdot.co.
Top comments (0)