Migrating existing express app to AWS using serverless

Maneesha Venigalla
5 min readFeb 19, 2021

--

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile apps. It facilitates the rapid development of Node based Web applications. In this article, we are going to learn how to migrate our existing express application to AWS and host it using API gateway and AWS Lambda (λ) using Serverless Framework. Let’s get started:

Prerequisites:

1. Basic express knowledge.

2. Activated AWS account.

3. Node and npm.

If you need help to get started with express, you can check this post right here and if you need help with the installation of node and npm you can read the post here.

Getting started:

The Serverless framework is a free and open-source web framework written using Node.js. It is the first framework developed for building applications on AWS Lambda (λ), a serverless computing platform provided by Amazon as a part of Amazon web services. Despite the name, serverless apps do not run without servers. It means that businesses don’t need to manage the server side of things and instead, focus on front end development.The biggest advantage of this architecture is that provisioning of servers is done dynamically to meet the real-time computing demand.

Let’s start by opening your existing express app in the IDE of your preferred choice. If you currently, don’t have an existing app, here’s the source code for the boilerplate of basic express app. (You can find instructions to run the express app in the readme file). Firstly, we’ll begin my installing the “serverless http” module. This package acts as a middleware that handles the interface between your Node.js application and the specifics of API Gateway. This module allows you to wrap your API for serverless use. No HTTP server, no ports or sockets. To install this module, we run the following command in the command line interface from the project directory:

npm i --save serverless-http

Once the package is successfully installed, let’s begin editing the “index.js” file of the application to import serverless-http into our code as a module by using node’s inbuilt “require” function and at the end export the handler function with our app wrapped in the serverless package. The end code should look similar to this:

const serverless = require('serverless-http');const express = require('express');const app = express();app.get("/", (req, res) => {  res.send("Hello World");});module.exports.handler = serverless(app);

If you are new to express and want to understand more about what the above code does, please do read this post right here. But if you are an express veteran, you can see that there’s only two lines of code changed from the existing express app and thanks to serverless-http package for this as it takes care of all the tasks internally. Our code is now complete and we can deploy it to API gateway and Lambda. When the users makes a request to the server on the route “/” should receive a response from the server returning “Hello world”. Now, to see this particular response we need to deploy the application using “serverless” framework. Serverless is a command-line tool, providing scaffolding, workflow automation and best practices for developing and deploying your serverless architecture (API gateway and Lambda). To install Serverless, run the following command in terminal:

npm i -g serverless

Once the framework is installed globally, let’s create a serverless.yml in our app folder. The file should look like below:

service: migrating-express-app-to-serverlessprovider:  name: aws  runtime: nodejs12.xfunctions:  app:    handler: index.handler    events:      - http:        path: /        method: ANY        cors: true      - http:        path: /{proxy+}        method: ANY        cors: true

As you can see the configuration for serverless framework is pretty starightforward. If you need detailed explanation on what each attribute does, you can read it in the documentation here. Now that we are all set to deploy our application, the first thing we need to do is give serverless framework access to our AWS account by creating an IAM role and defining a set of permissions for making the AWS service requests. To do so, we run the following command in the command line interface:

serverless config --credentials --provider aws --key <place access key here> --secret <place access secret>

Running the above command will open the AWS console requesting you to provide the IAM role and policy required to give serverless framework permission to automate deployment which will look similar to this.

Once the required permissions are assigned, you can run the following command in the command line interface to deploy the application:

serverless deploy

Running the above command will provision all of the infrastructure and upload your code to it to AWS lambda and will return an API endpoint to access our server and our interface will look similar to the one below:

You can see the output response from the server by loading the endpoint in your browser and it should display the following response:

You have succesfully migrated your express app to AWS and have automated the deployment process using the serverless framework 🚀👏🏻. You can get access to the source cource by clicking here. Now that you have the basics of serverless, AWS Lambda(λ), you can expand what you have like CI/CD, deploying app on EC2 instances, some more features about Lambda, Gateway etc., Check out my other posts on AWS here.

Like this article? Follow Maneesha Venigalla.

--

--