How to Deploy AWS CloudFormation with Jenkins Pipeline

Table of contents
- Description
- Prerequisites
- Step 1: Create an IAM User in AWS
- Step 2: Configure AWS CLI on Jenkins Server
- Step 3: Store AWS Credentials in Jenkins
- Step 4: Set Up a GitHub Repository
- Step 5: Create a Jenkins Pipeline Job
- Step 6: Add the Pipeline Script
- Step 7: Run the Jenkins Job
- Step 8: Verify the Stack in AWS

Description
This tutorial provides a step-by-step guide on setting up a Jenkins pipeline to deploy AWS infrastructure using CloudFormation.
Prerequisites
AWS Free Tier Account
IAM User with CloudFormation and EC2 Permissions
AWS CLI installed and configured on the Jenkins Server
GitHub repository containing a CloudFormation template
Step 1: Create an IAM User in AWS
Go to AWS IAM Console → IAM Users
Click Users → Create User
Enter User Name: jenkins-user
Click Next: Permissions -> Attach policies directly
Attach these policies:
AWSCloudFormationFullAccess
AmazonEC2FullAccess
IAMFullAccess
Click Next: Tags → Next: Review → Create User
Create Access Key ID and Secret Access Key under Security Credentials -> Access keys (save them securely)
Step 2: Configure AWS CLI on Jenkins Server
Open Command Prompt (cmd) on Jenkins Server and run below.
aws configure
Enter credentials:
Access Key ID: (Paste from AWS)
Secret Access Key: (Paste from AWS)
Default region: eu-west-2
Output format: json
To Verify credentials run.
aws sts get-caller-identity
This should return your IAM user details.
Step 3: Store AWS Credentials in Jenkins
Go to Jenkins Dashboard → Manage Jenkins → Manage Credentials
Click Global Credentials → Add Credentials
Select AWS Credentials
Enter below
Access Key ID
Secret Access Key
Save with ID: aws-credentials
Step 4: Set Up a GitHub Repository
Create a GitHub Repository
Upload your CloudFormation template (e.g., stack-template.json)
Step 5: Create a Jenkins Pipeline Job
Go to Jenkins Dashboard → New Item
Enter Job Name → Select Pipeline → Click OK
Scroll down to Pipeline Section → Select Pipeline Script
Step 6: Add the Pipeline Script
- Paste this pipeline script into Jenkins:
pipeline {
agent any
environment {
AWS_REGION = 'eu-west-2'
STACK_NAME = 'jenkins-cloudformation-stack'
}
stages {
stage('Checkout') {
steps {
git(
url: "https://github.com/rajandubey/DevOps_With_AWS.git",
branch: "main",
)
}
}
stage('Deploy CloudFormation Stack') {
steps {
withCredentials([aws(credentialsId: 'AWS_Jenkins_User')]) {
script {
bat ''
'
aws cloudformation deploy--stack - name % STACK_NAME % --template - file AWS_EC2_Creation.json--capabilities CAPABILITY_IAM--region % AWS_REGION %
''
'
}
}
}
}
}
}
Step 7: Run the Jenkins Job
Click Save
Click Build Now
Monitor the logs to check for errors
Step 8: Verify the Stack in AWS
Go to AWS Console → CloudFormation
Check if the stack jenkins-cloudformation-stack is created successfully
Verify resources (EC2, S3, IAM, etc.)
Subscribe to my newsletter
Read articles from Rajan Dubey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
