This article explains automating AWS Lambda function deployment using a Jenkinsfile, detailing configuration, package preparation, and code updates via AWS CLI.
In this article, we detail the process of automating the deployment of an AWS Lambda function using a Jenkinsfile. We explain how to modify the Lambda function configuration using the AWS Lambda UI, prepare the deployment package, and update the function code via the AWS CLI. Follow the steps below for a comprehensive guide.
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:
Switch to your VM, remove any previous sandbox directories, and set up your working area. For example, remove the “sandbox” directory as follows:
Copy
Ask AI
pwd/root/sandbox/solar-systemcd ~rm -rf san
Next, ensure that you are on the main branch of the “Solar System” repository. Execute a 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.
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 the app.js file.
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.
Before committing the Jenkinsfile changes, update the index.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:
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 uses tail commands 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:
Copy
Ask AI
{ "errorType": "MongooseError", "errorMessage": "The 'uri' parameter to 'openUri()' must be a string, got 'undefined'. Make sure the first parameter to 'mongoose.connect()' or 'mongoose.createConnection()' is a string.", "stack": [ "MongooseError: The 'uri' parameter ...", "... more logging details ..." ]}
Make sure to review your AWS CloudWatch logs for any errors after deployment to ensure that all necessary configurations are applied correctly.
In the Jenkinsfile, the AWS Lambda update step must inject these environment variables. In a subsequent session, update the Lambda configuration to include the secure MongoDB credentials stored in Jenkins.A sample snippet from the Jenkinsfile environment configuration might look like this:
Copy
Ask AI
pipeline { agent any environment { MONGO_URI = "mongodb+srv://supercluster.d83jj.mongodb.net/superData" MONGO_DB_CREDS = credentials('mongo-db-credentials') MONGO_USERNAME = credentials('mongo-db-username') MONGO_PASSWORD = credentials('mongo-db-password') SONAR_SCANNER_HOME = tool 'sonarqube-scanner-610' GITEA_TOKEN = credentials('gitea-api-token') } stages { stage('Installing Dependencies') { // Steps to install dependencies } stage('Dependency Scanning') { // Steps to run dependency scans } stage('Unit Testing') { // Steps to run unit tests } stage('Code Coverage') { // Steps for code coverage analysis } }}
Following this configuration update, the Lambda function will be able to establish a connection to MongoDB with the correct credentials.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!