AWS Lambda is a serverless technology that lets you run code without provisioning or managing servers. It supports most popular programming languages, including Node.js.
Why go serverless? The benefits of deploying a serverless API include auto-scalling, zero server administration, the ability to pay only for what you use, increased velocity, etc. Running your Express.js API on AWS Lambda is a great way to take advantage of all these benefits.
However, Lambda doesn’t have direct support for frameworks such as Express.js built with programming languages such as Node.js. That’s where an adapter such as Claudia.js comes in.
Claudia.js sits between AWS Lambda and your Express.js app and converts Lambda invocation events from various AWS sources, such as API gateways or application load balancers, into HTTP events that your Express app can listen to.
In this tutorial, we’ll demonstrate how to use Claudia.js to deploy a serverless API to AWS. We’ll show you how to build and deploy an Express.js application to AWS Lambda.
Here’s what we’ll cover:
- What is Claudia.js?
- Getting started with Claudia.js
- Generating an Express.js app
- Configuring your Express.js app
- Deploying to AWS Lambda
The tutorial assumes that you’re conversant with Node.js, Express.js, and Lambda. Lambda expects your code to be structured a certain way. So if you want to use it to host something like an Express app, you’ll need to use an adapter like Claudia.js.
What is Claudia.js?
Claudia.js is a tool designed to simplify deployment of Node.js projects to AWS Lambda and API Gateway. It automates deployment tasks so you don’t need to worry about introducing errors and configures everything the way you would expect it to be set up out of the box.
Getting started with Claudia.js
In this section, we’ll walk you through setting up an example Node.js and Express.js app to show Claudia.js in action.
npm install -g claudia
This will make Claudia.js instantly available to all your projects. Verify that Claudia.js was installed correctly by running the following command:
If the installation works as expected, you’ll see a version number similar to the one below:
Setting up an AWS account
If you don’t already have an AWS account, you’ll need to create one. Amazon has a free tier plan, which will cover creating and testing the Express.js app in Lambda. Follow this guide to set up your AWS account
Configuring AWS access credentials
Now that you have an AWS account, you’ll need to create a new user and give the user programmatic access to your AWS account. Follow the steps outlined below:
- Sign in to the AWS Management Console and open the IAM console
- In the navigation pane, choose Users
- Create a new user, choose the Programmatic access option and follow the rest of the prompt
- At the last prompt, you’ll see new user credentials. To view the new access key pair, choose Show. You won’t have access to the secret access key again after this dialog box closes. Your credentials will look something like this:
- Access key ID:
- Secret access key:
- Access key ID:
- Download the credentials as a CSV and store the file in a safe location
- Click on the Users tab and select the newly added user
- On the Permissions tab, add the following permission:
Now, install the AWS CLI and use it to configure a local profile. Claudia will use this profile to deploy your app to Lambda:
$ aws configure AWS Access Key ID [None]: Your Access Key ID AWS Secret Access Key [None]: Your screet key Default region name [None]: us-east-1 Default output format [None]: json
Generating an Express.js app
Install the Express.js app generator using npm:
npm install express-generator -g
The Express generator is a CLI that allows you to run the
express command in your terminal:
This creates a new Express project called
claudia-demo, which is then placed inside of the
Now install all project dependencies:
Your app does not need to listen to a TCP port because API Gateway will handle the web requests. You can get rid of the
.bin folder created by the Express generator.
Configuring your Express.js app for deployment
The Express.js app will be hosted by a Lambda function, so it needs a handler to send data between the app and the API Gateway. This is where Claudia.js comes in. Claudia uses
aws-serverless-express to generate a helper function to interface between API Gateway requests and Express.js requests and responses.
Execute the command below in your project directory. If you have renamed your Express.js application module, replace
app with the name of the main Express.js application module:
$ claudia generate-serverless-express-proxy --express-module app
This will add
aws-serverless-express to your project dependencies and create the file containing your Lambda function. By default, the file will be called
Deploying to AWS Lambda
Your app is now ready to go live:
claudia create --handler lambda.handler --deploy-proxy-api --region us-east-1
Congratulations! you have your Express.js application running on Lambda.
We have seen that we can run an Express.js app on AWS Lambda, but should every Express application run on Lambda?
Express.js assumes that your application is running on a traditional server and not serverless. Certain operations that work easily in a traditional Express.js server app may not work serverless.
For example, file and image uploads would not work in an Express.js app on Lambda because the app doesn’t have access to a persistent filesystem. Also, WebSocket communication would not work on Lambda because your server doesn’t exist when there are no requests.
Like any tool, the viability of running your Express.js app on AWS Lambda depends on your use case and the unique requirements of your project. I hope this tutorial gives you some insight to help you determine when to use this method.
LogRocket: Full visibility into your web apps
LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.