A Guide: Deploying your Node.Js Application on Lambda Function using Docker
AWS Lambda
Lambda is a serverless computing service . It allows you to run code without provisioning or managing servers. Lambda runs your code only when needed and scales automatically, from a few requests per day to thousands per second.
One of the notable aspects of AWS Lambda is its pricing model. Lambda functions are billed based on the number of requests made to them and the time it takes for the code to execute. There are no charges when your code is not running. This "pay-per-request" pricing model can make Lambda one of the most cost-effective options for certain workloads, especially those with unpredictable usage patterns.
Step 1: Setting up your Express JS Application
Create an empty folder, then open it with your text editor
Create the app.js file and paste the following code
const express = require("express");
const serverless = require("serverless-http");
const app = express();
const port = 3000;
// Define a route handler for the GET request to '/'
app.get("/", (req, res) => {
res.send("Hello, world!");
});
// Start the server
app.listen(port, () => {
console.log(`Server is listening at ${port}`);
});
const handler = serverless(app);
module.exports.handler = async (event, context) => {
// Return the result of serverless Express app
return await handler(event, context);
};
- Install the required depencies: express and serverless-http using npm
npm install express serverless-http
- Now run the following command to start the application server
node app.js
- By now, your ExpressjS application code should be looking like the following
- now visit localhost:3000 to access your endpoint
Step 2: Containerizing your ExpressJS Application
- Create a Dockerfile file and add the following code
# Use the AWS Lambda Node.js base image
FROM public.ecr.aws/lambda/nodejs:14
# Set the working directory to the Lambda task root directory
WORKDIR /var/task
# Copy the contents of the current directory into the Lambda task root directory
COPY . /var/task
# Install serverless-http package
RUN npm install serverless-http
# Install all dependencies listed in package.json
RUN npm install
# Set the Lambda handler to your Express.js application entry point
CMD [ "app.handler" ]
- Run the following command to build your docker container
docker build -t myapp .
Login into your aws management console and navigate to AWS ECR(Elastic Container Registry)
Navigate to Private repositories and click Create Repository, then name it
myapp
Navigate into the repository and click view push commands
copy the commands, paste them in the terminal and run them one by one
Kindly note that on the above commands, you need to have logged into aws cli with your terminal
Visit
logging into aws cli
on how to gain access to ECR from your text editor
Step 3: Creating the lambda function
Visit your AWS management console and navigate to lambda
Click create function
Select
Container image
give your function a name of your choice
Click on Browse images and select the latest image of the repository you have just created,then click select image.
Click create function
Step 4: Creating a Function URL
Open your lambda function
Navigate to Configuration,then Function URL
click
Create function URL
On Auth type, chose
NONE
Then click save
Now if you checkyour right side of your lambda console, you will see a link under Function Arn, copy it and paste in your browser
- NOW YOU ARE GOOD TO GO