Certified Jenkins Engineer

AWS Lambda and Advanced Deployment Techniques

Demo Using GenAI to Generate Steps for Lambda Deployment

Before automating AWS Lambda deployment with a Jenkins pipeline, it’s crucial to adapt a Node.js application (originally designed for Docker on EC2 and Kubernetes) to run seamlessly in a serverless environment. Instead of sifting through extensive AWS documentation, we used Microsoft Copilot to outline high-level steps. Below are the questions we asked and the concise answers that guided our Lambda-ready changes and pipeline design.


1. What are the steps required to deploy a Node.js application to AWS Lambda using a Jenkins pipeline?

The image shows a Visual Studio Code interface with a markdown file open, containing steps and questions related to deploying a Node.js app to AWS Lambda using a Jenkins pipeline. The terminal at the bottom is connected to a server.

Copilot mapped out these key stages:

  1. Configure AWS and Jenkins

    • Ensure AWS CLI is installed and credentials are set:
      aws configure
    • Install Jenkins plugins: Git, NodeJS, AWS Lambda, Pipeline
  2. Install Dependencies

    npm install
    
  3. Run Tests (if applicable)

    npm test
    
  4. Package the Application

    zip -r my-node-app.zip index.js node_modules
    
  5. Deploy to Lambda

    aws lambda create-function \
      --function-name my-node-app \
      --zip-file fileb://my-node-app.zip \
      --handler index.handler \
      --runtime nodejs14.x \
      --role arn:aws:iam::<AWS_ACCOUNT_ID>:role/<ROLE_NAME>
    
  6. Define Jenkins Pipeline (Jenkinsfile)

    pipeline {
      agent any
      stages {
        stage('Checkout') { steps { git url: 'https://repo-url.git' } }
        stage('Install')  { steps { sh 'npm install' } }
        stage('Test')     { steps { sh 'npm test' } }
        stage('Package')  { steps { sh 'zip -r my-node-app.zip index.js node_modules' } }
        stage('Deploy') {
          steps {
            withAWS(credentials: 'aws-creds') {
              sh '''
              aws lambda update-function-code \
                --function-name my-node-app \
                --zip-file fileb://my-node-app.zip \
                --publish
              '''
            }
          }
        }
      }
    }
    

Warning

Ensure that your Jenkins aws-creds credential has the necessary IAM permissions to create and update Lambda functions.

To summarize, here’s a quick overview of each pipeline stage:

StageDescriptionCommand
CheckoutClone the Git repositorygit clone https://repo-url.git
InstallInstall Node.js dependenciesnpm install
TestRun unit and integration testsnpm test
PackageArchive the application code and dependencieszip -r my-node-app.zip index.js node_modules
DeployUpdate AWS Lambda function codeaws lambda update-function-code --function-name my-node-app --zip-file fileb://my-node-app.zip --publish

2. Are there any specific Lambda or serverless dependencies to add?

To adapt Express for Lambda’s event-driven model, install these packages:

npm install express serverless-http
# (Optional) Install the Serverless Framework CLI globally
npm install -g serverless

Note

The serverless-http library wraps your Express application into a handler function that AWS Lambda can invoke.


3. Do we need to modify the application code (app.js) for AWS Lambda?

Yes. Wrap your Express app using serverless-http and export the handler:

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

app.get('/', (req, res) => {
  res.send('Hello, AWS Lambda!');
});

module.exports.handler = serverless(app);

The image shows a webpage from Copilot, explaining how to build AWS Lambda functions using Node.js and Express. It details the use of `module.exports`, `handler`, and `serverless(app)` for adapting applications to AWS Lambda.


4. What is the AWS CLI command to update a Lambda function’s code?

If your function already exists, run:

aws lambda update-function-code \
  --function-name my-node-app \
  --zip-file fileb://my-node-app.zip \
  --publish

Or update from an S3 bucket:

aws lambda update-function-code \
  --function-name my-node-app \
  --s3-bucket my-bucket \
  --s3-key my-node-app.zip

5. How can we retrieve the Function URL configuration via AWS CLI?

To fetch the function URL settings after deployment:

aws lambda get-function-url-config \
  --function-name my-node-app

With these steps, you now have a clear roadmap:

  • Integrate serverless-http into your Node.js app and export a Lambda handler.
  • Zip your application code and dependencies.
  • Use AWS CLI commands within a Jenkins pipeline to deploy and update your Lambda function.
  • Retrieve and test the Function URL configuration with a simple CLI call.

In the next session, we’ll manually execute these commands to confirm the deployment before fully automating the workflow using our Jenkins pipeline.

Watch Video

Watch video content

Previous
AWS Lambda Basics