Deploy AWS SAM Applications with Jenkins CI/CD Pipeline

In this article we are going to cover Creating Jenkins Pipeline for AWS SAM Application Deployment | Deploy AWS SAM Applications with Jenkins CI/CD Pipeline.

In modern DevOps workflows, automation is key to efficient and reliable deployments. AWS Serverless Application Model (AWS SAM) streamlines the development and deployment of serverless applications, while Jenkins provides a robust CI/CD automation framework.

This article guides you through building a Jenkins pipeline for AWS SAM, covering setup, pipeline configuration, and deployment stages. By the end, you’ll have a fully automated pipeline that builds, tests, and deploys serverless applications seamlessly to AWS Lambda.

Prerequisites

Before setting up a Jenkins pipeline for AWS SAM deployment, ensure you have the following:

  1. AWS Account – An active AWS account with permissions to deploy AWS Lambda and related resources.
  2. Jenkins Installed – A running Jenkins server with access to install necessary plugins.
  3. AWS SAM CLI – Installed to build and package serverless applications.
  4. GitHub Repository – A source code repository to store your AWS SAM application.
  5. Docker – Docker should be installed.

Below is the Workflow Diagram:

Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 1

Step #1:Initialize a New AWS SAM Project using GitHub

Create a Repository in GitHub:

Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 2

Set up a fresh AWS SAM project in the repository directory:

sam init

Select the following options:

Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 3

go to the repository by using below command:

cd sam-app

You can see new files in the repository:

Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 4

Step #2:Commit and Push Changes to GitHub

git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/username/repo.git
git push -u origin main
Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 5

Step #3:Allow Jenkins Users to Access the Docker Socket

Use below commands to allow Jenkins users to access the docker socket:

sudo usermod -aG docker jenkins

After that restart jenkins:

sudo systemctl restart jenkins

Without the above configurations, Jenkins will not be able to access Docker Hub.

Step #4:Download Plugins in Jenkins for AWS SAM Application

Download the following plugins Manage plugins > Plugins > Available Plugins:

  • Pipeline Stage View
  • AWS Credentials
  • AWS SAM
  • Pipeline: AWS Steps
  • Docker Pipeline
Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 6
Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 7
Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 8
Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 9
Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 10

Step #5:Add AWS IAM Credentials in Jenkins

Add AWS Credentials by Manage Jenkins > Credentials > global > Add Credentials

Select AWS Credentials in Kind and then add ID, Access Key ID and Secret Access Key

Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 11

Step #5:Jenkins CI/CD for AWS SAM Application using Jenkins

Jenkins Pipeline Code to deploy AWS SAM application

pipeline {
agent {
docker {
image 'public.ecr.aws/sam/build-python3.13' // Uses AWS-provided SAM CLI image
args '-u root' // Run as root inside the container
}
}

environment {
AWS_REGION = 'ap-south-1'
}

stages {
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/harish981/aws-sam-jenkins.git'
}
}

stage('Install AWS SAM CLI') {
steps {
sh '''
sam --version # Check if SAM CLI is already installed
'''
}
}

stage('Build SAM Application') {
steps {
sh '''
sam build
'''
}
}

stage('Deploy to AWS') {
steps {
withAWS(credentials: 'aws-credentials-id', region: "${AWS_REGION}") {
sh '''
sam deploy --stack-name my-sam-app --resolve-s3 --capabilities CAPABILITY_IAM --no-confirm-changeset
'''
}
}
}
}

post {
failure {
echo 'Deployment failed! ❌'
}
success {
echo 'Deployment successful! ✅'
}
}
}

Use the above Pipeline code in Jenkins Configuration and replace the “aws-credentials-id” with your credentials id which you gave in the AWS Credentials.

After Build you’ll get the output as shown below:

Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 12

Access the URL in Console Output:

Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 13
Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 14

You can also review the deployment details, including the resources created as part of the my-sam-app and aws-sam-cli-managed-default stacks in CloudFormation:

Deploy AWS SAM Applications with Jenkins CI/CD Pipeline 15

Remove the resources (two stacks in CloudFormation) if they are no longer needed.

Conclusion:

Setting up a Jenkins pipeline for AWS SAM makes deploying serverless applications easier and more efficient. By automating the process, you ensure consistent and reliable deployments. With Jenkins and AWS SAM working together, managing serverless applications becomes simple and streamlined.

Related Articles:

Deploy AWS SAM Applications with GitLab CI/CD Pipeline

Reference:

Using Jenkins to deploy with AWS SAM AWS

Harish Reddy

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap