Introduction

It is a good software engineering practice to separate portions of your code function to work independently of other parts. Since your codes are made up of functions basically, serverless functions allow you to deploy those functions without the complexity of managing a server to run them.

In this tutorial, we will be learning what serverless functions are, how to write them, and how to deploy them on Netlify in minutes. We will also touch up on other uses of serverless functions.

What are Serverless Functions?

A serverless function is a function hosted on a cloud infrastructure and performs a single purpose. It is very similar to every other function you write as long as it has its own dependencies and can run on its own. It is serverless because it takes off the stress of server maintenance and scaling the function from you.

Netlify has made it very easy to write any backend task and deploy it independently or alongside your project in simple steps. Your functions can also be deployed by a simple git push command if linked with your GitHub repository. These functions are called Netlify Functions.

Serverless functions work just like your API endpoint

Let’s Build our Serverless Function

Create a project folder

Create an empty folder either manually or by running mkdir custom-netlify-functions. Use any name you feel like. Here I’m using ‘custom-netlify-functions’.

Setup CI/CD with GitHub

Initialize repo with GitHub

  • Change to the folder cd custom-netlify-functions
  • Create a readme.md file touch readme.md
  • Run git init to initialize folder with Git repository
  • Run git add . && git commit -m "initial commit" to add the file and commit changes.
  • Go to github.com/new, create a new repo — preferably with same name
  • Link the local repository with your remote repo like so: git remote add origin git@github.com:YourGithubName/your-repo-slug.git
  • Run git push -u origin master to push your files

Install Netlify CLI

We will be using the Netlify CLI as it makes it super easy to get our project up and running Run npm install -g netlify-cli

Initialize Netlify in folder

If you don’t have a netlify account, create one now

While inside the folder, run netlify init to initialize the folder with netlify.

Image for post

Image for post

The prompt will ask you “What would you like to do?”, select “Create and configure a new site”. This option creates a new site on your netlify account.

If you are already authenticated, it will ask you to select your team (Netlify Account). If you aren’t authenticated on netlify.com yet, it will open a browser and request that you enter your login credentials.

The next prompt will ask your Site name and if you’ll like to authenticate with Github. For site name, retain the same name as ‘custom-netlify-functions’ and authenticate with Github

This step connects your Netlify account and repo with the same one you just created on GitHub.

Next prompts. For “Your build command”, simply press Enter key. For Directory to deploy to, press Enter key too to use “.” (the current directory).

Image for post

#netlify #serverless #javascript #nodejs #function

How to Build and Deploy Serverless Functions to Netlify
1.20 GEEK