Jenkins Project: Building CI/CD Pipeline for Scalable Web Applications

Lambda Deployment

Configuring Pipeline For Lambda

In this lesson, we outline how to set up a robust CI/CD pipeline for your AWS Lambda application using Jenkins and the SAM CLI. This guide covers installing the SAM CLI on Jenkins, configuring AWS permissions, and establishing a pipeline that automates code checkout, dependency installation, testing, building, and deployment.

Preparing Jenkins

Before proceeding, install the SAM CLI on your Jenkins server. This tool is essential for building and deploying your Lambda code to AWS, just as you would on your local machine.

The image shows a diagram illustrating the installation of SAM CLI on Jenkins, featuring a connection between a SAM CLI box and the Jenkins logo.

For detailed installation instructions, please refer to the official SAM CLI documentation.

Setting Up AWS Permissions

Proper AWS permissions are required for Jenkins to deploy Lambda functions. Start by creating an AWS user with the necessary permissions, and generate an access key and secret key. These credentials will be stored securely in Jenkins.

The image illustrates AWS permissions, showing a user with associated permissions and AWS credentials, including an access key and a secret key.

Important

Ensure that the created AWS user has only the permissions needed for deploying Lambda functions to maintain security best practices.

Pipeline Overview

The CI/CD pipeline is designed to execute the following sequential steps:

  1. Checkout the code from the repository.
  2. Install dependencies.
  3. Run tests.
  4. Build the code using the SAM CLI.
  5. Deploy the built application to AWS.

The image is a flowchart titled "Configuring Pipeline," showing steps: Checkout Code, Install Dependencies, Test Code, Build (sam build), and Deploy (sam deploy), with a note about needing AWS credentials.

Jenkins Pipeline Configuration

Below is an example Jenkins pipeline configuration. Note that this project contains two requirements.txt files. In this example, the development dependencies are installed from lambda-app/tests/requirements.txt before running tests.

Setup Stage: Installing Dependencies

pipeline {
    agent any
    stages {
        stage('Setup') {
            steps {
                sh "pip3 install -r lambda-app/tests/requirements.txt"
            }
        }
        // Run tests here (e.g., using pytest) if needed.

Build Stage: Building the Code

With the dependencies installed and tests (if any) executed, the next step is to build the Lambda application using the SAM CLI. The command uses the lambda-app/template.yaml file to define the build parameters.

        stage('Build') {
            steps {
                sh "sam build -t lambda-app/template.yaml"
            }
        }

Deploy Stage: Deploying to AWS

The deploy stage includes AWS credentials provided as environment variables. These credentials are set for this stage only to enhance security. The sam deploy command is executed with flags --no-confirm-changeset and --no-fail-on-empty-changeset to automate the deployment process without manual input.

        stage('Deploy') {
            environment {
                AWS_ACCESS_KEY_ID = credentials('aws-access-key')
                AWS_SECRET_ACCESS_KEY = credentials('aws-secret-key')
            }
            steps {
                sh "sam deploy -t lambda-app/template.yaml --no-confirm-changeset --no-fail-on-empty-changeset"
            }
        }
    }
}

Tip

Automating your deployment process with Jenkins ensures consistent and reproducible builds, reducing manual errors and accelerating your release cycles.

This configuration completes the setup of your CI/CD pipeline for AWS Lambda. With these automated steps, every aspect from dependency installation to deployment is seamlessly integrated within Jenkins.

Happy automating your deployments!

Watch Video

Watch video content

Previous
Demo SAM Serverless Application Model