Going Serverless With Netlify Lambda Functions

Eugene Musebe
The Startup
Published in
5 min readSep 12, 2019

What is serverless computing?

Serverless computing is a cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. Pricing is based on the actual amount of resources consumed by an application, rather than on pre-purchased units of capacity

In this article, we are going to focus on Netlify lambda functions. It’s assumed that you have an understanding of how to use Javascript and Netlify to build a Continuous Deployment from a Git repository. If you don’t have much/any experience with Netlify, follow the link to a Netlify Guide.

Getting Started

To get started, create a folder on the directory of your choice, navigate to the folder and execute the following command.

npm init -y

this will generate a package.json file that will list all the project dependencies.

For this project, we shall only be using the package Netlify lambda to demonstrate how we can locally develop Netlify functions and deploy them to Netlify servers. Run the following command on your terminal to install the package.

npm i netlify-lambda

After the installation is done, you need to configure the build and deploy scripts. This is done in the package.json file under the scripts section as displayed on the highlighted code below.

build & deploy scripts

We need to create a folder that contains all of our Lambda Functions. It doesn’t really matter where you put the folder or what you call it, I’m going to put mine in the root of my app and call it functions.

For each lambda function, we need to create a new javascript file that will perform the assigned task. i will call my file getImages.js”

Lambda function takes in three parameters :

  1. Event: where we can retrieve post data, headers.
  2. Context: information about the context in which the function was called, like certain user information.
  3. Callback: a function that we call to return a response to the user.

The first parameter that the callback function takes is an error, which is null if there is no error. If the error is present, it will be respected and handled back to the user, but if null, the second parameter gets returned to the user.

Add the following function to the getImages.js” file :

exports.handler = function(event, context, callback) {
callback(null, {
statusCode: 200,
body: 'Hello World'
});
};

When the above function is executed it will return a response with the status code “200” which indicates that everything is OK. The end-user will be able to receive the message “Hello World” which is appended to the body.

The last thing we need to do before we can run the function locally is to create a configuration file that tells Netlify where to serve the functions from. This is not the directory where we write our source code.

When we run the command “npm run lambda-serve” as documented in the “package.json” file, the source code will be compiled and the built assets will be put in the folder that we specify. So, create a netlify.toml file in the root of your directory that looks like this.

[build]
functions= "lambda"

This tells Netlify that when our functions are built the assets generated should be stored in a directory called “lambda”.

To test the application run the command :

npm run lambda-serve

The above command will generate a folder with the name “lambda” as declared in the netlify.toml file. Inside the folder, a minified version of the “getImages” file will be generated. The minified version of the function is the one that we shall deploy to netlify.

After the above command is executed on your terminal, you can test your function by visiting postman and making a GET request to the URL “http://localhost:9000/getImages”. This will produce the following response.

Lambda function response

Deploying the lambda Function

Netlify provides us with an easy continuous deployment feature that when we link a Github repository to netlify, the two will always be in sync as soon as we push our code to Github.

To initialize git in the project and make the first commit run the following commands :

git init
git add . && git commit -m "first commit"

After the above is done head over to Github, create a project and push the source code to the repository.

If you haven’t made a Netlify account yet, sign up here. After signing up, you’ll be redirected to your dashboard, which shows your list of deployed sites. If you haven’t used netlify, it’ll be empty. Click the top right button, New site from Git.

Netlify Dashboard

You’ll be switched to the next screen, where we will choose Github as our Git provider. You’ll be presented with a pop-up window (check your blocker!) and you’ll need to provide Netlify with access to your Github account.

After Authentication, you will view all of your current Github repositories. If you are working with a company repo, you can click your name to dropdown a list to choose other repo’s (you must be listed as a contributor on that repo).

Select the repository you created then the publish directory and build command you supplied in the netlify.toml file should be prefilled.

If you followed all the steps correctly, you’ll see Site deploy in progress switch to green, and display your new URL.

When you click on the URL provided you will land on an error page. This is because we are not trying to access a deployed website but the deployed lambda function. To be redirected to the cloud function, structure your URL in this format :

https://[YOUR_URL]/.netlify/functions/[YOUR_FUNCTION_NAME]

Test your URL on postman and you should be able to get the same response like the one you got on your local machine.

Conclusion

With Cloud Functions, you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your Cloud Function is triggered when an event being watched is fired. Your code executes in a fully managed environment. There is no need to provision any infrastructure or worry about managing any servers.

Watch out for the next article as we shall be building something cool using cloud functions.

The source code to this article can be found HERE

--

--

Eugene Musebe
The Startup

Software Engineer,techprenuer,event organiser & upcoming technology evangelist