Skip to main content
Before deploying a Node.js application to AWS Lambda via a Jenkins pipeline, it is essential to modify the application for Lambda compatibility. Originally built and deployed on AWS EC2 using Docker and later on Kubernetes, the application requires adjustments in its business logic to work as a Lambda function. For a detailed understanding of these changes, please refer to the official AWS Lambda documentation. Alternatively, if you need a quick overview and would like to generate a Jenkins pipeline automatically, leveraging GenAI tools like GitHub Copilot in Action can be very beneficial. Below is a series of questions and answers generated with GenAI that illustrate how to adapt your Node.js application for AWS Lambda deployment.

1. Steps to Deploy a Node.js Application to AWS Lambda Using a Jenkins Pipeline

The following steps outline the key actions involved:
  1. Setup AWS Environment
    Ensure you have an active AWS account to deploy your Lambda function.
The image shows a webpage with instructions for deploying a Node.js app to AWS Lambda using a Jenkins pipeline, including steps for setting up an AWS environment and creating a Node.js application.
  1. Prepare the Node.js Application
    Even if your application is ready, make sure to install all necessary dependencies.
  2. Package Your Application
    Package your application into a zip file for upload. For example:
  3. Deploy to AWS Lambda
    Create a Lambda function using the AWS CLI, ensuring you specify the correct IAM role:
  4. Configure Jenkins Pipeline
    In Jenkins, set up multiple stages including dependency installation, unit testing, packaging, and deployment. Consider installing plugins like Git and AWS Lambda to facilitate the process.

2. Addressing Lambda or Serverless Dependencies in the Node.js Application

When adapting your Node.js application for Lambda, you will often need to include additional dependencies. GenAI recommends adding packages such as serverless and serverless-http along with Express. You can install these dependencies using:
A typical project initialization might look like this:
And your modified app.js file should be structured as follows:
Additionally, a sample serverless.yml configuration file might be:
The serverless-http library wraps your Express application and adapts it for AWS Lambda’s event-driven architecture.
The image shows a webpage with instructions on using Node.js and AWS Lambda, specifically detailing how to export a handler function using the serverless-http library. It includes explanations of module.exports, handler, and serverless(app).

3. Modifying the app.js File for Lambda Compatibility

To ensure your application is compatible with AWS Lambda, you need to export the handler using the serverless-http library. This change adapts your Express routes to work within the event-driven model of Lambda. Update your app.js file as shown:

4. Updating a Lambda Function Using the AWS CLI

When updating your Lambda function’s code, you can use the AWS CLI command update-function-code. Based on where your deployment package is stored, you have three options:
  • Update directly with a zip file:
  • Update from an S3 bucket:
  • Update and publish the new version:

5. Retrieving the Lambda Function URL

After deployment, you can retrieve the function URL by running the following command:
This command returns the URL used to invoke your Lambda function.
The image shows a webpage with instructions on setting up a Jenkins pipeline and testing and monitoring AWS Lambda functions. It includes steps for installing Jenkins, configuring build steps, and using AWS tools for testing and monitoring.

6. Summary and Final Commands

The key steps for deploying a Node.js application to AWS Lambda using a Jenkins pipeline include:
  • Installing necessary dependencies (Express, serverless-http, etc.)
  • Packaging your application into a zip file
  • Exporting a Lambda-compatible handler from your application
  • Updating the function code using AWS CLI commands
  • Retrieving and testing your deployed Lambda function
Below is a consolidated example of commands you might execute:
For a brief modification of your application code to ensure Lambda compatibility, you can use:
Additionally, to adjust the app.js file if needed, you might run:
In the next session, these commands will be executed manually to validate the deployment process. Subsequent automation will be implemented using a Jenkins pipeline.
Thank you for following this lesson on using GenAI to streamline Lambda deployment via a Jenkins pipeline.

Watch Video