Why serverless?

The most common benefit of using serverless is less cost than normal server renting. For example, if I have service that need to use 100 unit resource in weekend and 20 unit resource in week day(different amount user). Normally, I need to rent a server which has at least 100 unit resource so I need to pay 700(100*7) unit resource per week. However, serverless architecture manage resource for you and you only pay for the resource you used, so you only pay 300(100*2 + 20*5) unit resource per week.

This article will focus hot to integrate your express project with lambda.

Project structure

|- node_modules
|- app.js
|- lambda.js
|- package.json


Express is very popular framework for node server. It is easy to integrate your express project to lambda in two steps.

  1. Remove app.listen();. Lambda will listen for you.
  2. Export your app.js.
'use strict'
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.get('/example', function(req, res) {
    res.send('Hello world');

app.get('/anotherexample', function(req, res) {
    res.send('Hello another world');

//You don't need to listen because labmda will listen for you.

// Export your Express configuration so that it can be consumed by the Lambda handler
module.exports = app;

Lambda handler

You will need a lambda handler to connect your express project to lambda.

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


Lambda is serverless compute solution provided by Amazon. Let's start create a Lambda.

  1. You will need an AWS account.
  2. Create a Lambda function.
  3. We already have our express project, so just choose blank function, and we will upload our code later.
  4. We will configure our trigger latter. So we are good to go next.
  5. Put your lambda function name. Your main lambda function should be in your Handler(if it's index.handler then you should put your main lambda function in index.js). If you use my example code, you should put lambda.handler in this field because our handler is lambda.js. You can also configure advance option and role here.
  6. Compress(you should compress your project files includes node_mudule not entire project folder, otherwise lambda cannot find your handler file.) your code and upload to lambda.
  7. You can now press the test button and see if your function work.(Your function is not yet public, so we need to figure the API gateway for your function).

API Gateway

In order to let everyone or other service to use your lambda function. You need to set up an API Gateway.

  1. Go to Trigger tab.
  2. Add trigger and choose API gateway. You can put your name of API gateway, stage and security here. Instead of use the dropdown, you can just type whatever you want. We use "Open" in here so everyone can access it for now.
  3. Your API link is the one below.
  4. Click it will show you the result.

Config your API gateway

Task is not yet completed. You may wonder how about another function /anotherexample? If you try to modify the link to another example like below.


You will see it return

{"message":"Missing Authentication Token"}

Because your API gateway only expose example function to outside. If you want to expose other function, you will need to config your API gateway.

  1. Go to API Gateway and select your api.
  2. Create a resource(which is service/fucntion, anotherexample).

  3. Select your new resource and create method.
  4. You can select get/post/any... depend on your method.
  5. Config your method.
  6. Then repeat all the steps until you have put all your service method on it. Then deploy your API, otherwise it will not work.

Hope this article will help anyone like me who feel AWS is so complicate and it doc is even more complicate.