Introdction
If you want to build a application but you don't want to buy and maintain server the best option is Lambda
. You still can use .zip
file to create a Expressjs Lambda but that is not a best practice to do as a developer. In this scenario you can use Container Image
to create a server.
Resources
Steps 📚
Create Express app
1) Create Directory for application
mkdir lambda-express
cd lambda-express
2) Initialization
npm init
3) Install Express
npm install express
4) Install serverless-http
npm install serverless-http
Application implementation
1) app.js file => (lambda-express/app.js)
const express = require('express');
const serverless = require('serverless-http');
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
console.log("/ endpoint is reached");
res.send('Hello World!');
});
app.get('/hello', (req, res) => {
console.log("/ endpoint is reached");
res.send('Hello from Lambda!');
});
module.exports.handler = serverless(app);
2) package.json => (lambda-express/package.json)
{
"name": "example",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node app.js"
},
"author": "janith",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.19.2",
"serverless-http": "^3.2.0"
}
}
3) Check all the npm packages are available or not
npm ls
result
example@1.0.0 /home/janith/TEST/example
├── express@4.19.2
└── serverless-http@3.2.0
Create a docker image
1) Docker file
# Use the official AWS Lambda Node.js base image
FROM amazon/aws-lambda-nodejs
# Copy function code
COPY package*.json ${LAMBDA_TASK_ROOT}
COPY node_modules ${LAMBDA_TASK_ROOT}
COPY app.js ${LAMBDA_TASK_ROOT}
RUN npm install
# Command to run the Lambda function
CMD ["app.handler"]
2) Confirm installation
npm install
3) Build the Image
- make sure you are in the same directory that
Dockerfile
file is located
docker build -t lambda-express:latest .
Upload Image to ECR
1) Create a ECR
- ECR is Elastic Container Registory in AWS
- go to ECR and create a private repository
2) Upload the Image that you build locally
- go inside of the repository
- click View push commands
3) Login first
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 144901196852.dkr.ecr.us-east-1.amazonaws.com
4) Tag the image
- you build the image previously no need to build it again if you use another name that different from this repostiry
docker tag lambda-express:latest 144901196852.dkr.ecr.us-east-1.amazonaws.com/lambda-express:latest
5) Push the image into ECR
docker push 144901196852.dkr.ecr.us-east-1.amazonaws.com/lambda-express:latest
Deploy Image on Lambda 🚀
2) Select option as Container Image
4) Container Image URI
5) Keep other settings as default and create the function
6) Goto configurations and create a Function URL
7) Make sure to select the Auth type
as NONE
Congratulations 🎉 Just now you deployed your app
click your Function URL and enjoy
happy coding !
Top comments (1)
Great article, Janith! Your explanation of deploying Express.js with Docker on AWS Lambda is clear and practical. It would be helpful to include tips on handling environment variables and reducing cold start delays. Thanks for this valuable guide!