Aws Lambda Nodejs serve http + ServerLess

Dounpct
5 min readApr 29, 2023

--

“Aws Lambda, Nodejs, http server, Serverless”

Long time ago that I have been DevOps engineer and need to maintain Cloud Function from Google Cloud. I think it ok simple. But few time that I have requirement from team vendor that implement application on AWS and they request to have simple api server to handle from webhook on cloud such as Jira and so on.

Objective

  • Create first Lambda function with simple UI
  • Create express with package serverless-http
  • Create serverless + express

Prerequisite

  • Nodejs
  • AWS account with access to console

Create first Lambda function with simple UI

  • Login to AWS console
  • Services and Lambda
  • Click Create function button
  • Select Author from scratch
  • Function name : test-first-lambda
  • Runtime: Node.js 14.x or else
  • Click Advanced setting
  • Enable function URL
  • Auth type: NONE
  • Click Create function
  • Open URL
  • Simple let go on

Create express with package serverless-http

  • On your local machine
  • Create Folder: devops-test-express
  • Create file: index.js
const express = require('express');
const port = process.env.PORT || 3000;
const app = express();

app.get('/', (req, res) => {
const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
const obj = JSON.parse(text);
console.log(obj)
res.status(200)
res.send(obj)
});

app.get('/healthcheck', (req, res) => {
res.status(200)
res.send('ok')
});

app.get('/create', (req, res) => {
res.status(201)
res.send('created')
});

app.listen(port, () => {
console.log(`Server is listening on port ${port}.`);
});
npm init -y
npm install express --save
npm start
  • Test on localhost:3000
  • Add
const serverless = require('serverless-http');
  • And
exports.handler = serverless(app);
  • Set ENV for local and on Lambda. So
const express = require('express');
const serverless = require('serverless-http');
const port = process.env.PORT || 3000;
const app = express();

app.get('/', (req, res) => {
const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
const obj = JSON.parse(text);
console.log(obj)
res.status(200)
res.send(obj)
});

app.get('/healthcheck', (req, res) => {
res.status(200)
res.send('ok')
});

app.get('/create', (req, res) => {
res.status(201)
res.send('created')
});

if (process.env.ENVIRONMENT === 'production') {
exports.handler = serverless(app);
} else {
app.listen(port, () => {
console.log(`Server is listening on port ${port}.`);
});
}
npm install serverless-http --save
  • Zip file
zip -r -y -q dist.zip .
  • Create new Lambda from scratch with same configuration
  • Click Upload from Button → .zip file
  • Choose your file and Save
  • ADD ENV
  • Test URL

Create serverless + express

npm install -g serverless
  • Configuration to connect AWS that need
 
export AWS_ACCESS_KEY_ID=<your-key-here>
export AWS_SECRET_ACCESS_KEY=<your-secret-key-here>
  • Create New Project with same express code
//index.js
const express = require('express');
const port = process.env.PORT || 3000;
const app = express();

app.get('/', (req, res) => {
const text = '{"name":"Sutee", "love":"Tech", "job":"DevOps"}';
const obj = JSON.parse(text);
console.log(obj)
res.status(200)
res.send(obj)
});

app.get('/healthcheck', (req, res) => {
res.status(200)
res.send('ok')
});

app.get('/create', (req, res) => {
res.status(201)
res.send('created')
});


app.listen(port, () => {
console.log(`Server is listening on port ${port}.`);
});
npm install aws-lambda-http-server --save
{
"name": "devops-test-serverless-express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-lambda-http-server": "^0.2.7",
"express": "^4.18.2"
}
}
  • Add aws-lambda.js
exports.proxy = require('aws-lambda-http-server')
require('./index.js')
  • Add serverless.yml
service: devops-test-serverless-express
provider:
name: aws
runtime: nodejs14.x
region: us-east-2
stage: alpha
functions:
proxy:
handler: aws-lambda.proxy
events:
- http:
path: /{proxy+}
method: any
- http:
path: ''
method: any
  • Test on Local
npm start
  • Deploy with sls
sls deploy
  • Check Lambda
  • If don’t need express. It fine
require('http').createServer((req, res) => {
if (req.url === '/ping') return res.end('pong')
if (req.url === '/hello') return res.end('world')
if (req.url === '/') return res.end('ok')
})
.listen(5000)
  • If we need remove lambda
sls remove

Troubleshooting

  • When Deploy with “sls deploy” please check about error because I need to add more Priviledge in AWS IAM
  • Have fun!!!

More Ref : https://www.npmjs.com/package/aws-lambda-http-server

— — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Credit : TrueDigitalGroup

— — — — — — — — — — — — — — — — — — — — — — — — — — — — —

--

--

Dounpct
Dounpct

Written by Dounpct

I work for TrueDigitalGroup in DevOps x Automation Team

No responses yet