Express JS in AWS Lambda
This is a boiler plate code to deploy Express in AWS Lambda using Serverless JS. Using this you can built Express APIs in your local machine and deploy them in AWS lambda with just few commands.
Run the local.js file to test the application locally
npm install npm start
About the files and folders
# lambda.js - File which is called by AWS lambda # local.js - Run the app on localhost. # src/app.js - Your App logic # serverless.yml - Lambda function configration.
How to use
1. Both the lambda.js and local.js calls the express app in the src/app.js
2. Write your business logic (Express API) inside the src folder
3. npm start runs the local.js file to test the app in your local machine
4. serverless.yml file contains your details about your AWS region and name of your Lambda function, they are self-explanatory edit them to your needs
5. Leave the lambda.js an local.js file alone unless you know what you are doing
Prerequisites for Deploying in AWS
# An AWS account # AWS CLI installed in your machine
AWS Configrations
Download and install AWS CLI. After installing run the following command in the terminal to configure your AWS account.
aws configure
it will ask for Access Key and Secret key, you can find them in your AWS online console.
Deployment
Run the following command to deploy the app as AWS Lambda function
npm run deploy
## After deployment
# Go to your AWS console and choose Lambda from Services. # You can find your deployed function in list. # Find the URL to invoke your function in the API endpoint. # Your endpoint URL should look something like this
https://abcd123.execute-api.us-east-1.amazonaws.com/production/{proxy+}
##Invoke your function omit the ‘ {proxy+} ‘ and use the rest of the URL to call the function
https://abcd123.execute-api.us-east-1.amazonaws.com/production/
The above URL outputs the responce in app.get(‘/’, (req, res) => {}) in the app.js file
src/app.js
const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended:false}))
//GET
app.get('/', (req, res) => {
res.status(200).send('hello world!');
});
//GET req Simply sends the current time
app.get('/time', (req, res) => {
let timeNow = Date(Date.now());
res.status(200).send(timeNow.toString());
});
//POST req logs the name and sends it
//To check send a POST req with "name" and check your lambda function console
app.post('/logthis', (req, res) => {
const name = req.body.name;
const toLog = `\n --- My name is ${name} -- \n`
console.info(toLog)
res.status(200).send(toLog);
});
module.exports = app;
lambda.js
'use strict'
//This file is AWS Lambda entry point
//All your business code (ExpressJS App) should be done in src/app.js
const awsServerlessExpress = require('aws-serverless-express');
const app = require('./src/app.js');
const server = awsServerlessExpress.createServer(app)
module.exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
local.js
const app = require('./src/app.js');
const port = process.env.PORT || 8000;
// Server
app.listen(port, () => {
console.log(`Listening on: http://localhost:${port}`);
});
package.json
{
"name": "express_in_aws",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node local.js",
"deploy": "serverless deploy"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-serverless-express": "^3.3.6",
"express": "^4.17.1"
},
"devDependencies": {
"serverless": "^1.50.1"
}
}
serverless.yml
service: expressInAWSDemo #Name of your App
provider:
name: aws
runtime: nodejs10.x # Node JS version
memorySize: 512
timeout: 15
stage: production
region: us-east-1 # AWS region
functions:
api:
handler: lambda.handler
events:
- http: ANY {proxy+}
- http: ANY /
Reference
Github
Deploy NodeJS Express API as AWS Lambda Function in 20 minutes
Migrate your Existing Express Applications to AWS Lambda
How to install npm modules in AWS Lambda?
How to use layers with Lambda functions?
BiteSize Academy – File Uploader , Typescript
Create & Deploy Serverless Dot Net Core Web API with Lambda Fucntion & API Gateway by NextGlogy
youtube search – ec2 vs elastic beanstalk
AWS Sandbox – ALB, Lambda Authorizers, System Manager, Route53
Namaste Programming – Serverless Auth with AWS Lambda, MongoDB & JWT
Namaste Programming -Apache Server Configuration – Web Server Configuration
React, Node & AWS (6-X) – Serverless MongoDB CRUD API deployed on AWS
Angular + Node.js on AWS – How to Deploy a MEAN Stack App to Amazon EC2
How to upload node js lib or module on aws lambda
Deploying React_Node.js Microservices to AWS using Terraform Part 1 Code With Me!
How to use aws-serverless-express – 10 common examples
How to use the aws-serverless-express.configure function in aws-serverless-express
Lambda-Proxy vs Lambda Integration in AWS API Gateway
How can I resolve the errors that I receive when I integrate API Gateway with a Lambda function?
How do I turn on CloudWatch Logs for troubleshooting my API Gateway REST API or WebSocket API?
Deploy an Express.js app to AWS Lambda using the Serverless Framework
Using Node.js ES modules and top-level await in AWS Lambda
serverless-heaven/serverless-webpack
How to enable CloudWatch logs for API Gateway