This guide explains how to configure a CI/CD pipeline in Jenkins for deploying a Lambda application using AWS credentials.
In this guide, you will learn how to configure a CI/CD pipeline in Jenkins to deploy a Lambda application using AWS credentials. We start by setting up the necessary AWS credentials in Jenkins, then move on to configuring the pipeline and finally updating the Lambda function.
The following is the final version of the Jenkinsfile used in this pipeline. It is divided into four stages: Setup, Test, Build, and Deploy. Notice that the AWS credentials are injected as environment variables to be used by the AWS CLI and SAM CLI.
Setup Stage:
Installs the testing dependencies listed in lambda-app/tests/requirements.txt. This file includes important libraries such as pytest, boto3, and requests needed for testing purposes.
Test Stage:
Runs the test suite using Pytest to ensure the application works as expected.
Build Stage:
Utilizes the SAM CLI to build the Lambda application, referencing the lambda-app/template.yaml file.
Deploy Stage:
Injects AWS credentials as environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) and deploys the updated Lambda function using the SAM CLI with flags to bypass manual confirmation.
After you have updated your Jenkinsfile and made any necessary changes to your Lambda code, commit and push your changes to your Git repository. For example:
Once the changes are pushed, Jenkins will trigger the build automatically. The console output should confirm:
The successful checkout of code.
Installation of all required packages.
Execution of tests without errors.
Successful SAM build and deployment commands.
A sample snippet from the SAM CLI output might look like:
Copy
Ask AI
pip3 install -r lambda-app/tests/requirements.txtDefaulting to user installation because normal site-packages is not writableRequirement already satisfied: pytest in /var/lib/jenkins/.local/lib/python3.9/site-packages (from -r lambda-app/tests/requirements.txt (line 1)) (7.4.4)Requirement already satisfied: boto3 in /var/lib/jenkins/.local/lib/python3.9/site-packages (from -r lambda-app/tests/requirements.txt (line 2)) (1.34.158)Requirement already satisfied: requests in /var/lib/jenkins/.local/lib/python3.9/site-packages (from -r lambda-app/tests/requirements.txt (line 3)) (2.31.0)...
After deployment, the SAM CLI output will provide details including the IAM role, API Gateway endpoint, and the Lambda function ARN.
Click the API Gateway link provided in the output to test your deployed Lambda function. You should see a message similar to “Hello World version one” on your initial run.
To deploy updates to your Lambda function (for instance, upgrading to a new version), modify the function code and push the changes to your repository. An updated version of app.py looks like this:
Jenkins will trigger a new build, and you should observe that the Lambda function is updated to version two.A sample JSON response from the updated Lambda function may look similar to:
Copy
Ask AI
{"message": "hello w0rld v2"}
This confirms that your CI/CD pipeline is properly set up to test, build, and deploy updates for your Lambda function.For more detailed guidance, refer to the official AWS SAM Documentation and Jenkins Documentation.