# Introduction to Serverless

# Serverless Functions

A serverless function is essentially a piece of business logic (it can be a module) that is going to run in the backend and has these properties:

  1. Is stateless (does not maintain data) and
  2. Is ephemeral (is used and destroyed), as it potentially only lasts for seconds, and
  3. Is designed to be triggered by an API call that happens in the front end. Examples are:
    • placing an e-commerce order,
    • uploading a text file and
    • signing up for a newsletter, etc.

To create a serverless function, a developer has to write the function code and deploy it to an environment like Netlify or Vercel.

Serverless function workflow

A typical serverless function process would look like this:

  1. The developer:
    1. Writes a function: writes a function that fulfills a specific purpose in the application code, such as a form mailer.
    2. Defines an event: defines an event that will trigger the cloud-native service provider to execute the function. An HTTP request is a common example.
  2. During running time:
    1. Event Triggers: An user triggers the event with the click of a button or some similar action.
    2. Deploying and execution of the function: Netlify starts a new instance of the function if one isn’t already running.
    3. Response: The result is passed to the client

Serverless functions open a world of possibilities for running on-demand, server-side code without having to run a dedicated server.

With Netlify Functions we can

  1. put code into a netlify/functions folder (i.e netlify/functions/hello-world.js) in our project, and
  2. Netlify will deploy it for us and
  3. give us a URL (/.netlify/functions/hello-world) which will invoke it.

Functions deployed from Netlify are immutable[1]. This means that

  • an update to a function on your production branch won’t change the version that was deployed in a branch deploy, or in a Deploy Preview

Netlify serverless functions are

  1. version-controlled,
  2. built, and
  3. deployed

along with the rest of the Netlify site, and Netlify will automatically handles service discovery through their built-in API gateway.

This eliminates overhead and brings the power of Deploy Previews[2] and rollbacks[1:1] to our functions

# References

# What is an API Gateway

API Gateway

An API gateway is an API management tool that sits between

  • a client and
  • a collection of backend services.

An API gateway acts as a reverse proxy to accept all application programming interface (API) calls, aggregate the various services required to fulfill them, and return the appropriate result.

# What is a Service Discovery

Service Discovery

Services typically need to call one another. In a traditional distributed system deployment, services run at fixed, well known locations (hosts and ports) and so can easily call one another using HTTP/REST or HTTP/GraphQL or some RPC mechanism but on a modern microservice application like the Netlify one, each serverless function runs in virtualized/containerized environment and so the number of instances of a service and their locations changes dynamically and that is why an additional service discovery must be provided.

(opens new window)

# Watching the course Up and Running with Serverless Functions with Ben Hong

Here is the course:

# Anya Kubov Tutorial on Netlify Serverless Functions

Watch Create your first Netlify Serverless Function! by Anya Kubów

  • Create your first Netlify Serverless Function! by Anya Kubów. 2 Mar 2021

Watch also How to use a Serverless Database with Serverless Functions (simple!) by Anya Kubów

  • How to use a Serverless Database with Serverless Functions (simple!) by Anya Kubów. 24 Mar 2021

# Reading "Saving data to Supabase and getting it back again"

Read the article Saving data to Supabase and getting it back again (opens new window) by Phil Hawksworth at Netlify Blog June 28, 2021

shows how to use Netlify serverless functions to save content and also to retrieve it using the Supabase database.

# References

# Footnotes


  1. In traditional web apps, when we need to rollback, we simply fetch the .zip artifact file from a previous build, upload and unzip. There are reasons why we cannot apply this rollback strategy to Serverless Framework applications. Serverless Framework artifacts are not immutable. ↩︎ ↩︎

  2. Deploy previews allow you to create a preview of frontend builds before they are merged into a production website. You get some preview URL of a deployment and you can see what changed and make sure it looks great. ↩︎

Last Updated: a year ago