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?
Copilot mapped out these key stages:
Configure AWS and Jenkins
- Ensure AWS CLI is installed and credentials are set:
aws configure
- Install Jenkins plugins: Git, NodeJS, AWS Lambda, Pipeline
- Ensure AWS CLI is installed and credentials are set:
Install Dependencies
npm install
Run Tests (if applicable)
npm test
Package the Application
zip -r my-node-app.zip index.js node_modules
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>
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:
Stage | Description | Command |
---|---|---|
Checkout | Clone the Git repository | git clone https://repo-url.git |
Install | Install Node.js dependencies | npm install |
Test | Run unit and integration tests | npm test |
Package | Archive the application code and dependencies | zip -r my-node-app.zip index.js node_modules |
Deploy | Update AWS Lambda function code | aws 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);
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.
Links and References
- AWS Lambda Developer Guide
- Jenkins Pipeline Syntax
- serverless-http on npm
- Serverless Framework Documentation
Watch Video
Watch video content