Deploy Express JS container image on Lambda AWS



This content originally appeared on DEV Community and was authored by Janith Disanayake

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
  1. Initialization
npm init
  1. Install Express
npm install express
  1. 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);
  1. package.json => (lambda-express/app.js)
{
  "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"
  }
}
  1. 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"]
  1. Confirm installation
npm install
  1. Build the Image
  2. make sure you are in the same directory that Dockerfile file is located
docker build -t lambda-express:latest . 

Image description

Upload Image to ECR

  1. Create a ECR
  2. ECR is Elastic Container Registory in AWS
  3. go to ECR and create a private repository
    Image description

  4. Upload the Image that you build locally

  5. go inside of the repository

  6. click View push commands

Image description

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

Image description

  1. 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
  1. Push the image into ECR
docker push 144901196852.dkr.ecr.us-east-1.amazonaws.com/lambda-express:latest

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 !


This content originally appeared on DEV Community and was authored by Janith Disanayake