DEV Community

usufjameel
usufjameel

Posted on • Updated on

How to Deploy Existing NodeJS Express application as an AWS Lambda Function using ClaudiaJS

There are five easy steps to deploy your existing NodeJS ExpressJS application as an AWS Lambda Function using ClaudiaJS.

Step 1

Do not start the server instead export it

// app.listen(port, () => {
//     console.log(`listening On PORT -> ${port} `);
// });

// Export your Express configuration so that it can be consumed by the Lambda handler
module.exports = app
Enter fullscreen mode Exit fullscreen mode

Step 2

Create lambda.js file for creating a lambda handler

File structure

Step 3

Use aws-serverless-express for creating a lambda handler

// lambda.js
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)

Enter fullscreen mode Exit fullscreen mode

Step 4

Install aws-cli. Follow the link for details description for installation.

MacOS(Commandline installer)
1) Download the file using the curl command. The -o option specifies the file name that the downloaded package is written to. In this example, the file is written to AWSCLIV2.pkg in the current folder.

$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
Enter fullscreen mode Exit fullscreen mode

2) Run the standard macOS installer program, specifying the downloaded .pkg file as the source. Use the -pkg parameter to specify the name of the package to install, and the -target / parameter for which drive to install the package to. The files are installed to /usr/local/aws-cli, and a symlink is automatically created in /usr/local/bin. You must include sudo on the command to grant write permissions to those folders.

$ sudo installer -pkg ./AWSCLIV2.pkg -target /
Enter fullscreen mode Exit fullscreen mode

After installation is complete, debug logs are written to /var/log/install.log.

3) To verify that the shell can find and run the aws command in your $PATH, use the following commands.

$ which aws
/usr/local/bin/aws 
$ aws --version
aws-cli/2.4.5 Python/3.8.8 Darwin/18.7.0 botocore/2.4.5
Enter fullscreen mode Exit fullscreen mode

If the aws command cannot be found, you may need to restart your terminal or follow the instructions in Adding the AWS CLI to your path.

4) Configure aws-cli
The following example shows sample values. Replace them with your own values as described in the following sections.

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
Enter fullscreen mode Exit fullscreen mode

Step 5

Deploy your application using ClaudiaJS.

1) Install claudiajs

   npm install claudia -g
Enter fullscreen mode Exit fullscreen mode

2) Claudia was installed correctly by running the following command:

   claudia --version
Enter fullscreen mode Exit fullscreen mode

3) The following command will create the lambda function and api gateway application for you to access.

   claudia create --deploy-proxy-api --region <region_name 
eg. us-east-1> --handler lambda.handler --name <name_of_the_function>
Enter fullscreen mode Exit fullscreen mode
  • --deploy-proxy-api : If specified, a proxy API will be created for the Lambda function on API Gateway, and forward all requests to function.This is an alternative way to create web APIs to --api-module.
  • --region : AWS region where to create the lambda. For supported values, see https://docs.aws.amazon.com/general/latest/gr/rande.html#lambda_region for example: us-east-1
  • --handler : Main function for Lambda to execute, as module.function for example: if it is in the main.js file and exported as router, this would be main.router
  • --name : Lambda function name for example: awesome-microservice defaults to: the project name from package.json

Command for updating the lambda function

claudia update
Enter fullscreen mode Exit fullscreen mode

Top comments (0)