Running Lambda Express Apps: Going Serverless

on AWS, AWS Lambda, Data Integration, Express js, Lambda Expressions, Node js, Tutorials • April 13th, 2022 • Write for Hevo

Lambda Express Featured Image | Hevo Data

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.

Table of Contents

What is Serverless?

Lambda Express: Serverless | Hevo Data
Image Source: www.blog.runcloud.io

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.

However, as the name suggests, Serverless applications do not run without Servers. It only means that businesses or clients need not manage the server infrastructure and instead, focus on front-end development. Serverless architectures run on significantly reduced operational costs, complications, and engineering resources. However, they rely heavily on the vendors and supporting services.

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.

Simplify Your ETL with Hevo’s No-code Data Pipeline

Hevo Data a Fully-managed Data Pipeline platform, can help you automate, simplify & enrich your data replication process in a few clicks. With Hevo’s wide variety of connectors and blazing-fast Data Pipelines, you can extract & load data from 100+ Data Sources (including 30+ Free Data Sources) straight into your Data Warehouse or any Databases. To further streamline and prepare your data for analysis, you can process and enrich raw granular data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!

Get started with hevo for free

Hevo is the fastest, easiest, and most reliable data replication platform that will save your engineering bandwidth and time multifold. Try our 14-day full access free trial today to experience an entirely automated hassle-free Data Replication!

Going Serverless with AWS Serverless Express

Lambda Express: AWS Lambda | Hevo Data
Image Source: www.blog.iron.io

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 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);

What makes Hevo’s ETL Process Best-In-Class

Providing a high-quality ETL solution can be a difficult task if you have a large volume of data. Hevo’s automated, No-code platform empowers you with everything you need to have for a smooth data replication experience.

Check out what makes Hevo amazing:

  • Fully Managed: Hevo requires no management and maintenance as it is a fully automated platform.
  • Data Transformation: Hevo provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Faster Insight Generation: Hevo offers near real-time data replication so you have access to real-time insight generation and faster decision making. 
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 100+ sources (with 40+ free sources) that can help you scale your data infrastructure as required.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-day free trial!

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.
Lambda Express Deployment | Hevo Data
Image Source: www.bitbucket.org

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.

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.

visit our website to explore hevo

Hevo Data with its strong integration with 100+ Sources allows you to not only export data from multiple sources & load data to the destinations, but also transform & enrich your data, & make it analysis-ready so that you can focus only on your key business needs and perform insightful analysis.

Give Hevo Data a try and sign up for a 14-day free trial today. Hevo offers plans & pricing for different use cases and business needs, check them out!

Share your experience of Lambda Express deployment in the comments section below.

No-code Data Pipeline For Your Data Warehouse