How to Deploy AWS CloudFormation with Jenkins Pipeline

Rajan DubeyRajan Dubey
2 min read

Description

This tutorial provides a step-by-step guide on setting up a Jenkins pipeline to deploy AWS infrastructure using CloudFormation.

Prerequisites

  1. AWS Free Tier Account

  2. IAM User with CloudFormation and EC2 Permissions

  3. AWS CLI installed and configured on the Jenkins Server

  4. GitHub repository containing a CloudFormation template

Step 1: Create an IAM User in AWS

  1. Go to AWS IAM Console → IAM Users

  2. Click Users → Create User

  3. Enter User Name: jenkins-user

  4. Click Next: Permissions -> Attach policies directly

  5. Attach these policies:

    1. AWSCloudFormationFullAccess

    2. AmazonEC2FullAccess

    3. IAMFullAccess

  6. Click Next: TagsNext: ReviewCreate User

  7. Create Access Key ID and Secret Access Key under Security Credentials -> Access keys (save them securely)

Step 2: Configure AWS CLI on Jenkins Server

  1. Open Command Prompt (cmd) on Jenkins Server and run below.

    aws configure

  2. Enter credentials:

    Access Key ID: (Paste from AWS)

    Secret Access Key: (Paste from AWS)

    Default region: eu-west-2

    Output format: json

  3. To Verify credentials run.

    aws sts get-caller-identity

  4. This should return your IAM user details.

Step 3: Store AWS Credentials in Jenkins

  1. Go to Jenkins Dashboard → Manage Jenkins → Manage Credentials

  2. Click Global CredentialsAdd Credentials

  3. Select AWS Credentials

  4. Enter below

    Access Key ID

    Secret Access Key

  5. Save with ID: aws-credentials

Step 4: Set Up a GitHub Repository

  1. Create a GitHub Repository

  2. Upload your CloudFormation template (e.g., stack-template.json)

Step 5: Create a Jenkins Pipeline Job

  1. Go to Jenkins DashboardNew Item

  2. Enter Job Name → Select Pipeline → Click OK

  3. Scroll down to Pipeline Section → Select Pipeline Script

Step 6: Add the Pipeline Script

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

  1. Click Save

  2. Click Build Now

  3. Monitor the logs to check for errors

Step 8: Verify the Stack in AWS

  1. Go to AWS Console → CloudFormation

  2. Check if the stack jenkins-cloudformation-stack is created successfully

  3. Verify resources (EC2, S3, IAM, etc.)

0
Subscribe to my newsletter

Read articles from Rajan Dubey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rajan Dubey
Rajan Dubey