Alternative option to host express service - Lambda + Express tutorial
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
Express is very popular framework for node server. It is easy to integrate your express project to lambda in two steps.
- Remove
app.listen();
. Lambda will listen for you. - Export your
app.js
.
//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(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/example', function(req, res) {
res.status(200);
res.send('Hello world');
});
app.get('/anotherexample', function(req, res) {
res.status(200);
res.send('Hello another world');
});
//You don't need to listen because labmda will listen for you.
//app.listen(3000);
// 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
Lambda is serverless compute solution provided by Amazon. Let's start create a Lambda.
- You will need an AWS account.
- Create a Lambda function.
- We already have our express project, so just choose blank function, and we will upload our code later.
- We will configure our trigger latter. So we are good to go next.
- 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.
- 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.
- 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.
- Go to Trigger tab.
- 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.
- Your API link is the one below.
- 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.
https://API_id.execute-api.AWS_region.amazonaws.com/dev/anotherexample
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.
- Go to API Gateway and select your api.
- Create a resource(which is service/fucntion, anotherexample).
- Select your new resource and create method.
- You can select get/post/any... depend on your method.
- Config your method.
- 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.