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
- Create Directory for application
mkdir lambda-express
cd lambda-express
- Initialization
npm init
- Install Express
npm install express
- Install serverless-http
npm install serverless-http
Application implementation
- 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);
- 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"
}
}
- 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
- 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"]
- Confirm installation
npm install
- 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
- Create a ECR
- ECR is Elastic Container Registory in AWS
Upload the Image that you build locally
go inside of the repository
click View push commands
- 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
- 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
- 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 !
This content originally appeared on DEV Community and was authored by Janith Disanayake