Skip to main content
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.

Configuring AWS Credentials in Jenkins

To securely deploy your Lambda function, first configure AWS credentials within your Jenkins instance:
  1. Navigate to Manage Jenkins > Credentials > Global Credentials.
  2. Add a new credential of type Secret Text with the ID aws-access-key. Paste your AWS access key that you normally use locally.
For enhanced security, create a dedicated AWS user with only the necessary permissions for Lambda and related services.
  1. Add a second credential of type Secret Text with the ID aws-secret-key and paste your AWS secret access key.
The image shows a Jenkins interface displaying a list of global credentials, including a production server IP, SSH key, and AWS access key, with options to add or edit credentials.

Creating and Configuring the Pipeline

After setting up the credentials, create a new pipeline from the Jenkins dashboard:
  • Name the pipeline “Lambda pipeline.”
  • Enable the GitHub hook trigger for Git SCM polling.
  • Choose Pipeline script from SCM.
  • Select Git as the SCM, paste your repository URL, and specify the branch (typically “main”).
  • Set the script path to Jenkinsfile.
The image shows a configuration screen for setting up a Git repository in a pipeline, with fields for repository URL, credentials, and branch specification. The URL points to a GitHub repository, and the branch specified is "master".
Save your configuration to complete the pipeline setup.

The Jenkinsfile and Pipeline Steps

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.

Stage Details

  • 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.

Committing and Pushing Changes

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:
Then, push the changes to the main branch:

Build and Deployment Output

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:
After deployment, the SAM CLI output will provide details including the IAM role, API Gateway endpoint, and the Lambda function ARN.
The image shows a screenshot of an AWS CloudFormation console output, detailing the deployment of a stack named "lambda-app" with information about IAM roles, API Gateway endpoint, and 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.

Updating the Lambda Function

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:
After modifying the file, commit and push your changes:
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:
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.

Watch Video