How to Deploy Existing NodeJS Express application as an AWS Lambda Function using ClaudiaJS
Tags: node, lambda, tutorial, aws
Published: 2022-01-10 06:45:07

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
```

## Step 2
Create lambda.js file for creating a lambda handler

![File structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws7xegk2ysy4daa1us2b.png)

## Step 3
Use [aws-serverless-express](https://www.npmjs.com/package/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)

```

## Step 4
Install [aws-cli](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html). 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"
```
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 /
```
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
```
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](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-path.html).

4) [Configure aws-cli](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html)
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
```

## Step 5
Deploy your application using [ClaudiaJS](https://claudiajs.com/tutorials/installing.html).

1) Install claudiajs
  ```
   npm install claudia -g
  ```
2) Claudia was installed correctly by running the following command:
  ```
   claudia --version
  ```
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>
  ```

- `--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](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
```