DEV Community

Cover image for Deploy Express JS container image on Lambda AWS
Janith Disanayake
Janith Disanayake

Posted on • Edited on

Deploy Express JS container image on Lambda AWS

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
Enter fullscreen mode Exit fullscreen mode

2) Initialization

npm init
Enter fullscreen mode Exit fullscreen mode

3) Install Express

npm install express
Enter fullscreen mode Exit fullscreen mode

4) Install serverless-http

npm install serverless-http
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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"
  }
}
Enter fullscreen mode Exit fullscreen mode

3) Check all the npm packages are available or not

npm ls 
Enter fullscreen mode Exit fullscreen mode

result

example@1.0.0 /home/janith/TEST/example
β”œβ”€β”€ express@4.19.2
└── serverless-http@3.2.0
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

2) Confirm installation

npm install
Enter fullscreen mode Exit fullscreen mode

3) Build the Image

  • make sure you are in the same directory that Dockerfile file is located
docker build -t lambda-express:latest . 
Enter fullscreen mode Exit fullscreen mode

Image description

Upload Image to ECR

1) Create a ECR

  • ECR is Elastic Container Registory in AWS
  • go to ECR and create a private repository Image description

2) Upload the Image that you build locally

  • go inside of the repository
  • click View push commands

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

5) Push the image into ECR

docker push 144901196852.dkr.ecr.us-east-1.amazonaws.com/lambda-express:latest
Enter fullscreen mode Exit fullscreen mode

Image description

Deploy Image on Lambda πŸš€

1) Create a Lambda function
Image description

2) Select option as Container Image
Image description

3) Function Name
Image description

4) Container Image URI

  • Click Browse images
    Image description

  • Select your private repository and Image that you created
    Image description

5) Keep other settings as default and create the function

6) Goto configurations and create a Function URL

Image description

7) Make sure to select the Auth type as NONE

Image description

Congratulations πŸŽ‰ Just now you deployed your app

click your Function URL and enjoy

happy coding !

Top comments (1)

Collapse
 
pruthuvide profile image
Pruthuvi de Silva

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!