Step 1: Remove Environment Variables from AWS Lambda
Before updating the Jenkinsfile, clear any static environment variables in the Lambda function. To do this, open the AWS Lambda configuration dashboard, navigate to the Configurations tab, and delete the environment variables. This ensures that the Jenkins pipeline will inject dynamic values during deployment. The original environment variables might have looked like:
Step 2: Prepare the Local Environment
Switch to your VM, remove any previous sandbox directories, and set up your working area. For example, remove the “sandbox” directory as follows:git fetch and git pull to merge changes from the feature branch that introduced the Jenkinsfile. This pull updates the repository with the latest Jenkinsfile and additional files.
Ensure that you are working on the main branch before triggering the build to avoid merging conflicts.
Step 3: Lambda S3 Upload and Deploy Stage in Jenkinsfile
A new stage named Lambda S3 Upload & Deploy has been added to the Jenkinsfile. This stage is executed exclusively on the main branch and proceeds through several steps:- AWS CLI Authentication: Uses AWS credentials (securely stored in Jenkins) with permissions for Lambda and S3 services.
- Modify
app.js: Executes shell commands to comment or uncomment certain lines in theapp.jsfile. - Zip the Deployment Package: Compresses the application files into a zip file that includes the Jenkins build ID.
- Upload to S3: Sends the zip file to a designated S3 bucket.
- Update Lambda Function Code: Uses the AWS CLI to update the Lambda function with the new code from the uploaded zip file.
Modifying app.js
This snippet demonstrates the use ofsed commands to modify app.js:
Zipping and Uploading the Package
The following block creates the zip file and then uploads it to S3 using thes3Upload step:
Complete Jenkinsfile Stage
Below is the consolidated Groovy stage that incorporates the AWS CLI call to update the Lambda function after the zip file has been uploaded:Step 4: Update index.html for Versioning
Before committing the Jenkinsfile changes, update theindex.html file to reflect the new version of your application. In this example, the version is updated to 5.0. Locate line 66 in the file and replace the button text as shown below:
Step 5: Build and Deployment Verification
Once the Jenkins job is triggered on the main branch, the Lambda S3 Upload & Deploy stage will execute the following process:-
Modification of app.js:
The stage usestailcommands to inspect the file before and after modifying it. The modifications include commenting out the local server startup command and ensuring that the correct export is active. Sample log output: -
S3 Upload:
The generated zip file (for example, “solar-system-lambda-2.zip”) is uploaded to the S3 bucket. Confirm the new object in the AWS S3 console.

-
AWS Lambda Update:
The AWS CLI command updates the function code. A successful update will return a JSON message similar to the following: -
Verification of Deployment:
Access the Lambda function through the AWS Lambda console. Since the environment variables have been removed, the function will emit an internal server error. CloudWatch logs will detail an error caused by the missing MongoDB URI, username, and password. An example CloudWatch error might be:
Make sure to review your AWS CloudWatch logs for any errors after deployment to ensure that all necessary configurations are applied correctly.
Step 6: Update Environment Variables in app.js
For MongoDB connectivity, the application relies on the following environment variables:Thank you for following along in this guide. With these steps, your AWS Lambda deployment is automated using Jenkins, ensuring a smoother release cycle and reducing manual configuration errors. Happy deploying!