Nowadays, most organizations and Software Developers are preferring Serverless applications for a number of reasons. Serverless applications are flexible and easy to develop due to the simplicity of their design. And, Developers are constantly on the look for transitioning to Serverless applications.
Express is a web framework for Node.js which simplifies the development of Serverless web applications, websites, and APIs. AWS Lambda, on the other hand, allows existing Express applications to be run in a Serverless fashion.
This article will take you through AWS Lambda Express Apps deployment. But beforehand let’s discuss the highly talked about term “Serverless” in brief.
What is Serverless?
Serverless is a fully-managed Cloud computing architecture that doesn’t require the application owners to buy, rent, or manage the Servers. The Cloud partner manages, handles, and maintains the infrastructure side of things for the web applications. On top of that, the Serverless environment allows the provisioning of Servers dynamically to meet the real-time computing demand. In a Serverless architecture, the backend logic is run on-demand in a stateless mode.
Why Use Serverless?
- Fully-managed Server: With Serverless architecture, users don’t need to manage or maintain the backend infrastructure. It’s being taken care of by the Cloud partners.
- Cost-effective: Serverless architectures run on a significantly reduced operational cost as users pay only for what they use. There are no additional costs involved with respect to idle capacity, resources, or maintenance.
- High Availability: Serverless architecture incorporates multiple redundancies which makes it extremely fault-tolerant.
- High Scalability: Serverless architecture offers virtually limitless and easy scalability which can be implemented by executing just a few lines of code.
Going Serverless with AWS Serverless Express
If you want your app to be immensely popular, you must get it online first. Assuming that you want people all over the world to use your app, you need to think in terms of scalability, resiliency, and many other factors. Well, in simple terms, you just need to go Serverless. With AWS Lambda and API Gateway, it is possible to have a Serverless architecture for your Express applications.
You can follow this example on the aws-serverless-express library for deploying and managing your Serverless resources.
- You can clone the library into a local directory using the following command.
git clone https://github.com/awslabs/aws-serverless-express.git
- Now, from within the
example
directory, run the following command.
npm run config <accountId> <bucketName> [region]
Note: This command basically modifies some files with your own settings.
- Now, remove “index.html” from the
package-function
command in package.json
and add “views” and “controllers”. These additional directories are required for running your app.
- Copy the following files from the
example
directory into your existing project’s directory:
Simple-proxy-api.yaml:
This Swagger file describes your API.
cloudformation.json:
This is a CloudFormation template used to create the Lambda function and API.
package.json:
This file contains useful npm scripts for managing your AWS resources and Lambda function.
api-gateway-event.json:
This file is helpful for testing your Lambda function locally.
lambda.js:
This is the Express Lambda function around your Express application.
The aws-serverless-express library
transforms the client request (via API Gateway) into a standard Node.js HTTP request object. This request is then sent to a Unix domain socket which transforms the request back for the API Gateway response. The library also starts your Express Server on the initial invocation of the Lambda function by the Unix domain socket. Here is the aws-serverless-express
library.
// lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)
Create a Simple Express Application
As discussed earlier, Express apps are flexible and easy to build. Before the deployment of the Lambda Express app, you must have an Express app in the first place. For a simple Express app, adding a few routes and route handlers will do the job. The Simplest Express app looks something like this.
mkdir sample-app && cd sample-app
npm init -y
npm install express
touch app.js
The following code goes to app.js
.
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/api/info', (req, res) => {
res.send({ application: 'sample-app', version: '1.0' });
});
app.post('/api/v1/getback', (req, res) => {
res.send({ ...req.body });
});
app.listen(3000, () => console.log(`Listening on: 3000`));
The above Express application exposes 2 APIs.
GET /api/info
returns details about the current API.
POST /api/v1/getback
returns the request body that was sent.
Now that you have created your Express app, it’s time to make it ready for the Lambda Express deployment. To do so, you will need a serverless-http module for wrapping up the Express app for Serverless use.
Run the following command to install the module.
npm i serverless-http
Now, update the app.js accordingly.
const serverless = require('serverless-http');
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/api/info', (req, res) => {
res.send({ application: 'sample-app', version: '1' });
});
app.post('/api/v1/getback', (req, res) => {
res.send({ ...req.body });
});
//app.listen(3000, () => console.log(`Listening on: 3000`));
module.exports.handler = serverless(app);
Deploying Lambda Express App
Your Express app is up and running but now comes the hard part: releasing your app to the world. Deploying your Express app on Lambda can be a tricky and tedious task. But with the Serverless Framework, it’s just a matter of a few minutes. Follow the below-mentioned steps to deploy your Lambda Express app using Serverless.
- Run the following command in the terminal to install Serverless.
npm install -g serverless
- Run the following command to verify that Serverless is installed successfully.
serverless ?version
- Next up, you need to allow Serverless to access your AWS account. Run the following command after replacing
ACCESS_KEY
and SECRET_KEY
with your AWS access and secret keys.
serverless config credentials --provider aws --key ACCESS_KEY ?secret SECRET_KEY
- Now, you need to create a Serverless Framework configuration file in the application folder.
touch serverless.yml
- The following code goes in
serverless.yml
.
service: sample-app
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: us-east-1
memorySize: 128
functions:
app:
handler: app/app.handler
events:
- http:
path: /
method: ANY
cors: true
- http:
path: /{proxy+}
method: ANY
cors: true
- Here comes the last step, deploying your application. Run the following command to deploy your Express application on the Lambda environment.
serverless deploy
- On the successful deployment of the Lambda Express app, you’ll see the following message.
This is how you can easily automate the deployment of Lambda Express apps using Serverless Framework.
Limitations of Serverless Lambda Express.js apps
- AWS Lambda doesn’t support Websockets as your server can’t exist with no requests. However, there is some limited Websockets support available through AWS IOT websockets over MQTT protocol.
- “Upload” to the file system isn’t supported either as the AWS Lambda function is read-only. However, it supports uploading files to the
/tmp
folder. The files will exist in the /tmp
folder for a short time till the function is “warm”.
- “Execution limits” can affect your Serverless Express app. API Gateway has a timeout of 30 seconds, whereas AWS Lambda’s maximum execution time can be as long as 5 minutes.
Conclusion
- Going Serverless is the need of the hour keeping in mind the scalability, security, and resiliency. AWS Lambda allows existing Express apps to go Serverless and so there is no infrastructure for Developers to manage.
- Scaling, provisioning, and configuration are fully managed in Lambda Express Apps. While discussing the benefits of AWS Lambda for Node.js applications, it’s worth noting how Lambda Express simplifies the process of making your Express.js app Serverless.
- This article introduced you to Serverless and helped you in the deployment of Lambda Express apps. However, it’s easy to become lost in a blend of data from multiple sources. Imagine trying to make heads or tails of such data. This is where Hevo comes in.
Share your experience of Lambda Express deployment in the comments section below.
Raj, a data analyst with a knack for storytelling, empowers businesses with actionable insights. His experience, from Research Analyst at Hevo to Senior Executive at Disney+ Hotstar, translates complex marketing data into strategies that drive growth. Raj's Master's degree in Design Engineering fuels his problem-solving approach to data analysis.