Search
Close this search box.

Netlify Lambda Functions

Netlify Lambda Functions let you deploy serverless Lambda functions without an AWS account. Your serverless functions are version-controlled, built, and deployed along with the rest of your Netlify site, and Netlify automatically handles service discovery through their built-in API gateway.

With Lambda, a typical workflow follows these steps:
1. You upload your code to AWS Lambda.

2. You set up your code to trigger from either other AWS services, HTTP endpoints, or in-app activity.

3. Lambda runs your code when triggered, only consuming the resources needed; it is important to realize that like most AWS services, Lambda provides continuous scaling, as needed.

4. You pay for just the compute time required.

Netlify functions have a similar workflow, let’s create and test one now..

Getting Started

Starter project: Gatsby

From the base folder, run:

  • npm install netlify-lambda (tools for Netlify Functions with a simple webpack/babel build step)
  • Include netlify.toml file in the base folder (File-based config)
[build]
  functions = "lambda"

This is the directory with our serverless Lambda functions to deploy to AWS.

We have to update our scripts in our package.json to use netlify-lambda.

Creating functions

  • In your projects base directory, add a functions folder.

Each Javascript file to be deployed as a serverless Lambda function must export a handler method with the following general syntax:

  • functions/NameOfLambdaFunction.js
exports.handler = function(event, context, callback) {
    // your server-side functionality
}
//Async syntax
exports.handler = async (event, context) => {
  return {
    statusCode: 200,
    body: "Hello, World"
  };
};
callback(null, {
    statusCode: 200,
    body: "Hello, World"
    });

I build upon our boilerplate code and create the following function. Here I use Axios, a promise-based HTTP client, to send a post request with a name to RequestBin, then our callback, and a catch for any errors.

functions/helloNetlify.js
  • npm run start:lambda
netlify-lambda serve
Postman

RequestBin

RequestBin

We see RequestBin received the post request, and in postman, we get our callback response “Hello, Netlify!”.

Now that we’ve locally tested our function, we’re ready to deploy it to a production environment.

Let’s deploy

  • Before deploying, We’ll add our lambda folder to our .gitignore. This folder will be recreated when we run our build script later.

/lambda

  • Next, we need a build script for both Gatsby and Netlify Lambda. So, I created the following script in package.json.

"prod": "npm run build; npm run build:lambda",

  • Commit and push your changes to Git.

Navigate to the projects Deploy settings, and under build settings, change the Build command:

  • Trigger a deploy.
Gatsby build
Netlify Lambda build
  • Once our deploy is complete, check the functions tab:

Let’s test our endpoint in Postman:

Success! Let’s check the function log for more details:

Let’s check RequestBin:

Now that we have an idea of how to create and deploy Netlify Lambda functions, check out some example functions! You would want to use Functions if you want to deploy scripts that can be run on-demand and return results just like an API, that run on high-powered servers for low latency, that can be written in Go or JavaScript, and that keeps the underlying code (and any secrets inside) hidden from the world. Thanks for reading!

Cassandra.Link

Cassandra.Link is a knowledge base that we created for all things Apache Cassandra. Our goal with Cassandra.Link was to not only fill the gap of Planet Cassandra but to bring the Cassandra community together. Feel free to reach out if you wish to collaborate with us on this project in any capacity.

We are a technology company that specializes in building business platforms. If you have any questions about the tools discussed in this post or about any of our services, feel free to send us an email!

Photo by Aron Visuals on Unsplash