Lambda API: A lightweight web framework that will rock your serverless world
The serverless universe is expanding once again and this time its welcomes Lambda API, a lightweight web framework for use with AWS API Gateway and AWS Lambda using Lambda Proxy Integration.
With more tools and technologies joining in, the serverless universe constantly expands!
Today we present you Lambda API, a lightweight web framework for use with AWS API Gateway and AWS Lambda using Lambda Proxy Integration created by serverless advocate Jeremy Daly.
Although it is based on and mirrors other web frameworks like Express.js and Fastify, Lambda API is quite unique in one key aspect. It has zero dependencies!
When it comes to serverless applications that need to load quickly, all these extra dependencies that you have to use with your additional Node.js modules slow down execution and use more memory than necessary. But Lambda API is here to change that!
As fast as The Flash?
Lambda API was built to be lightweight and fast. And it looks like it’s on the right track! Let’s have a closer look at its main features:
- Zero dependencies. Yup, still not a joke!
- Super lightweight and built specifically for serverless applications using AWS Lambda and API Gateway
- Provides support for API routing, serving up HTML pages, issuing redirects, serving binary files and more
- Offers a built-in logging engine that can even periodically sample requests for things like tracing and benchmarking
- It has a powerful middleware and error handling system
- Designed to work with Lambda’s Proxy Integration, automatically handling all the interaction with API Gateway for you
- It parses
RESPONSES, allowing you to focus on your application’s core functionality
- Create just one function that handled all your user management features
If you are still not convinced, let’s take a closer look at some of the framework’s functions.
Routes and HTTP Methods – Routes are defined by using convenience methods or the
METHOD method. There are currently eight convenience route methods:
Returning Responses – Supports both
async-await for returning responses to users. The RESPONSE object has several callbacks that will trigger a response (
Route Prefixing – Easy to create multiple versions of the same api without changing routes by hand. The
register()method allows you to load routes from an external file and prefix all of those routes using the
Debugging Routes – Offers a
routes() method that can be called on the main instance that will return an array containing the
PATH of every configured route. This will include base paths and prefixed routes.
RESPONSE – The
RESPONSE object is used to send a response back to the API Gateway. The
RESPONSE object contains several methods to manipulate responses. All methods are chainable unless they trigger a response. You can find the list here.
Logging – Includes a robust logging engine specifically designed to utilize native JSON support for CloudWatch Logs. Not only is it super fast, but it’s also highly configurable.
Middleware – It supports middleware to preprocess requests before they execute their matching routes. Middleware is defined using the
use method and requires a function with three parameters for the
Clean Up – It has a built-in clean up method called ‘finally()’ that will execute after all middleware and routes have been completed, but before execution is complete.
Namespaces – Allows you to map specific modules to namespaces that can be accessed from the
Lambda Proxy Integration – That’s an option in API Gateway that allows the details of an API request to be passed as the
eventparameter of a Lambda function.
If you are interested in a full walk-through on how to build a serverless API with serverless, AWS Lambda and Lambda API, check out Jeremy Daly’s tutorial.
To install Lambda API simply run
npm i lambda-api --save
Keep in mind, however, that you will need AWS Lambda running Node 8.10 and AWS API Gateway using Proxy Integration.