Hosting a Basic Express Node.js App on AWS Lambda


Introduction:

In this blog post, we will guide beginners through the process of hosting a basic Express Node.js app on AWS Lambda, enabling it to serve HTML web pages. We will explore how to deploy the app on AWS Lambda using the AWS Management Console. By the end of this tutorial, you'll understand how to adapt your Node.js app code for deployment on AWS Lambda using the AWS Console.

Prerequisites:

Step 1: Setting up the Express Node.js App

Let's begin by creating our simple Express app that serves web pages. Create an app.js file that contains the Express app and routes to serve the HTML pages. You can refer to the provided Node.js code to set up the basic structure.

Step 2: Install Dependencies

Ensure you have Node.js installed on your machine. Navigate to the project directory and run the following command to install the required packages:

					
npm install express
					
				

Step 3: Hosting Locally

In the local hosting scenario, our Node.js code would look like this:

				
// Local Machine: app.js

const express = require('express');
const app = express();
const PORT = 3000;

app.use(express.json());

app.get('/', (req, res) => {
	res.sendFile(__dirname + '/index.html');
});

// ... Other routes and middleware ...

app.listen(PORT, () => {
	console.log(`Server running on http://localhost:${PORT}`);
});
				
			

Here, we set up an Express app and manually specify the server's port using "app.listen()". The app listens for incoming HTTP requests on "http://localhost:3000" and serves the index.html page when accessing the root URL.

Step 4: Preparing for AWS Lambda Deployment

In the previous steps, we set up our basic Express Node.js app and installed the necessary dependencies. Now, before we proceed with deploying our app on AWS Lambda, let's take a moment to understand how we can make our Node.js code compatible with AWS Lambda using "serverless-http".

To deploy our app on AWS Lambda, we'll use the "serverless-http" package, which is a middleware that enables us to wrap our Express app for serverless deployment. This middleware takes care of adapting our Express app to work seamlessly with AWS Lambda and API Gateway.

In the following code snippet, you can see the modifications we make to our app.js file to make it compatible with AWS Lambda:

				
// AWS Lambda: app.js

const express = require('express');
const serverless = require('serverless-http');
const app = express();

app.use(express.json());

app.get('/', (req, res) => {
	res.sendFile(__dirname + '/index.html');
});

// ... Other routes and middleware ...

module.exports.handler = serverless(app);
				
			

In the AWS Lambda scenario, we import the serverless-http package and create a new instance of the Express app. We then define our app's routes, middlewares, and configurations as usual.

However, the crucial difference lies in the last line: "module.exports.handler = serverless(app);". By wrapping our Express app with "serverless-http", we create a Lambda handler function that can be invoked by AWS Lambda. The "serverless-http" package abstracts away the server management and ensures our app is ready for deployment on AWS Lambda.

This Lambda handler function is what AWS Lambda will use to trigger our app in response to incoming API Gateway requests. It acts as the entry point for our app's logic when hosted on AWS Lambda, and there's no need to manually specify a port or start a server, as the server management is handled by AWS Lambda itself.

With this modification in place, our Express app is now ready for deployment on AWS Lambda, making it easy to scale, maintain, and serve web pages efficiently.

In the next steps, we will walk through the process of deploying our app on AWS Lambda using the AWS Management Console, creating an API Gateway to trigger our Lambda function, and finally testing the deployed app. Let's continue with the deployment process to make our app accessible to users on the internet.

Step 5: AWS Lambda Deployment

Now, let's deploy our app on AWS Lambda using the AWS Management Console:

  1. Sign in to the AWS Management Console and navigate to the Lambda service.
  2. Click on the "Create function" button to create a new Lambda function.
  3. Choose "Author from scratch" as the function blueprint to start from scratch.
  4. Configure the function details:
    • Function name: Enter a unique name for your Lambda function.
    • Runtime: Choose "Node.js 14.x" (or the appropriate version for your app).
    • Role: Choose an existing role with the necessary permissions or create a new one with permissions to write CloudWatch Logs and read the package from Amazon S3.
  5. Click on the "Create function" button to create the Lambda function.
  6. In the function details page, scroll down to the "Function code" section.
  7. In the "Code entry type" dropdown, select "Upload a .zip file."
  8. Click on the "Upload" button and select the ZIP file containing your Node.js app package.
  9. Scroll up and click on the "Save" button to save your Lambda function.

Step 6: Setup Trigger for Lambda

Step 7: Access Your App

After deploying the API, the API Gateway will provide an endpoint URL. You can access your Node.js app using this URL (e.g., "https://your-api-gateway-url.com/appname").

Conclusion:

In this blog post, we explored how to deploy a basic Express Node.js app on AWS Lambda using the AWS Management Console. By following these steps, you can easily deploy your application to AWS Lambda and leverage the power of serverless computing to host your web pages. The AWS Console provides a user-friendly interface to create and configure the necessary resources, allowing you to deploy your app seamlessly and enjoy the benefits of scalable and cost-effective serverless architecture on AWS Lambda. Happy deploying!