How to Migrate existing Express app to AWS Serverless architecture?

In the previous blog, we have discussed How to create a dynamic website using serverless architecture? Yeah its great to use AWS Serverless architecture, but what should I do if I have an existing Express app? Should I rebuild it with it from scratch? Don’t worry find out the solution here. In this blog, we will cover all the steps to migrate your existing app to AWS serverless.

Why AWS serverless?

First of all, it is important to address why serverless apps are favored over traditional server-hosted apps. There are a couple of reasons lets focus some of them

  • Low maintenance
  • Low cost
  • Easy to scale

The biggest benefit by far is that you only need to worry about your code and nothing else. And the low maintenance is a result of not having any servers to manage. You don’t need to actively ensure that your server is running properly or that you have the right security updates on it. Hence, You deal with your own application code and nothing else.

The main reason it’s cheaper to run serverless applications is that you are effectively only paying per request. So when your application is not being used, you are not being charged for it. Let’s do a quick breakdown of what it would cost for us to run our note taking an application. We’ll assume that we have 1000 daily active users making 20 requests per day to our API and storing around 10MB of files on S3.

Finally, the ease of scaling is thanks in part to DynamoDB which gives us near infinite scale and Lambda that simply scales up to meet the demand. And of course, our frontend is a simple static single page app that is almost guaranteed to always respond instantly thanks to CloudFront.

So, now focus on our main topic migrate existing express app to AWS serverless architecture

Prerequisite

  1. AWS account
  2. S3 Bucket
  3. Little bit idea about AWS API Gateway
  4. Your Express app

Step 1. Install the Express on Serverless node module

Express on Serverless this module allows you to run Node.js express on AWS Lambda, using Serverless framework. To install use the following command, the process is same as you install other modules.

npm install express-on-serverless

Step 2. Modify handler.js file


//serverless.yml
app = require('./app.js');
exports.index = require('express-on-serverless')(app);

Step 3. Modify serverless.yml file


//serverless.yml
service: aws-nodejs

provider:
  name: aws
  runtime: nodejs6.10

functions:
  index:
    handler: handler.index
    events:
      - http: any {proxy+}

Step 4. Configure AWS Credential

Check here to Set up AWS Credentials and Region for Development. If you have Linux environment you can check in the following file

nano ./aws/credential

Step 5. Deploy the Express app

sls deploy or serverless deploy

Now you can access https://API_GATEWAY_HOST/dev/{any route you have created} ! It’s too easy!!

For example, We have followed the MVC approach to develop our application so how to access the app check in the following:
Our directory structure:


Our app.js file


//add the following code 
//Separating router form main app file
app.use('/api/manager', manager);

Our router file inside route directory

//Router
var express = require('express');
var router = express.Router();
// Require controller modules
var manager_controller = require('../controllers/manager');

/* Manager Account Operation. */
router.get('/list', manager_controller.manager_list);
router.post('/', manager_controller.add);
router.put('/', manager_controller.edit);
router.delete('/:id', manager_controller.delete);

//export all routes
module.exports = router;

Our controller file inside controllers directory

/**
 * Manager Controllers
 */
var util = require('util');
var managerAccountModel = require('../models/managerAccount');
var helper = require('../middlewares/helper').user;

// Display list of all managers
exports.manager_list = function(req, res, next) {
    res.send('NOT IMPLEMENTED: managerlist goes here list');
};

So, after deploying the app access URL will be like this
https://API_GATEWAY_HOST/dev/api/manager/list

Conclusion

In conclusion, there are lots of node module and approaches available to migrate existing express app on AWS serverless. Most of all are buggy due to more module dependency or complex steps to use. After going through two or more. Finally, at the end, we found express-on-serverless, which is well suited for us and hope this also works for you like a charm. Feel free to ask any query, glad to hear from you.

Further reading

 

Leave a Comment

Scroll to Top