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

  1. Create an empty folder, then open it with your text editor

  2. 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);
      };
  1. Install the required depencies: express and serverless-http using npm
 npm install express serverless-http
  1. 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

Step 2: Containerizing your ExpressJS Application

  1. 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" ]
  1. Run the following command to build your docker container
  docker build -t myapp .
  1. Login into your aws management console and navigate to AWS ECR(Elastic Container Registry)

  2. Navigate to Private repositories and click Create Repository, then name it myapp

  3. Navigate into the repository and click view push commands

  4. 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

  1. Visit your AWS management console and navigate to lambda

  2. Click create function

  3. Select Container image

  4. give your function a name of your choice

  5. Click on Browse images and select the latest image of the repository you have just created,then click select image.

  6. Click create function

Step 4: Creating a Function URL

  1. Open your lambda function

  2. Navigate to Configuration,then Function URL

  3. click Create function URL

  4. On Auth type, chose NONE

  5. Then click save

  6. 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