Automate the deployment of a Python web application using a Jenkins pipeline on AWS.

Ankita LunawatAnkita Lunawat
3 min read

Automating the deployment of a Python web application using a Jenkins pipeline on AWS aims to streamline, secure, and scale the CI/CD process.


🎯 Purpose of the Use Case

1. Faster, Reliable Delivery

  • Automate code build, test, and deployment for faster release cycles while reducing human errors with consistent, repeatable pipelines.

2. Continuous Integration & Continuous Deployment

  • Automatically trigger builds when new code is pushed to GitHub/GitLab/Bitbucket, run unit/integration tests, and deploy to staging or production environments on AWS like EC2, ECS, or EKS.

3. Infrastructure as Code (IaC)

  • Use tools like Terraform or CloudFormation with Jenkins to deploy infrastructure consistently and reliably.

4. Scalability and High Availability

  • Deploy to scalable AWS infrastructure like EC2 Auto Scaling, Elastic Load Balancer, or EKS, and ensure zero-downtime updates with rolling deployments or blue/green strategies.

5. Security and Compliance

  • Automate security checks like linting and SAST, and control access using Jenkins credentials and AWS IAM roles.

6. Feedback and Monitoring

  • Jenkins can integrate with tools like Slack, email, or JIRA for notifications, and application monitoring can be done using CloudWatch, New Relic, or DataDog.

Prerequisites :

    • Python web app (Flask/Django/FastAPI)

      • AWS EC2 (or ECS/EKS for container-based deployment)

      • Jenkins installed and configured (on EC2 or externally)

      • Docker installed (if containerizing)

      • Git repository (GitHub, GitLab, etc.)


πŸ“Œ Create EC2 instance on AWS console

πŸ“ŒInstall Java 17 on the EC2 instance.

sudo apt update 
sudo apt install openjdk-17-jdk -y java -version

πŸ“ŒInstall Jenkins on the EC2 instance.

sudo apt update 
sudo apt install openjdk-17-jdk -y wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt update sudo apt install jenkins -y 
sudo systemctl enable jenkins 
sudo systemctl start jenkins
sudo systemctl status Jenkins

πŸ“ŒTo access Jenkins, open a web browser and go to http://your-ec2-instance-public-ip:8080.

1. Open a web browser and navigate.

http://your-server-ip:8080

2. Retrieve the initial Jenkins password.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

πŸ“Œ Install Docker on EC2

Docker installation steps - follow this article

πŸ“Œ Jenkins Pipeline Project Create

1. Open Jenkins Dashboard.β†’ Click on New Item β†’ 3. Enter a name for your pipeline project (e.g., Fintracking_Pipeline). β†’ 4. Select Pipeline as the project type. β†’ 5.Click OK. β†’ 6. Scroll down to the Pipeline section.β†’7. In the Definition dropdown, select Pipeline script.

Paste the Jenkins Pipeline Script (given above) into the script textbox.

pipeline {
    agent any

    environment {
        APP_NAME       = "fintrack_app"
        CONTAINER_NAME = "${APP_NAME}_container"
        APP_PORT       = "5001"
        IMAGE_TAG      = "${env.BUILD_NUMBER}"
    }

    stages {
        stage('Clean Workspace') {
            steps {
                cleanWs()
            }
        }

        stage('Clone Code') {
            steps {
                git branch: 'main', url: 'https://github.com/Ankita2295/FinancesTrackProject_Live'
            }
        }

        stage('Build Docker Image') {
            steps {
                script {
                    sh "docker build -t ${APP_NAME}:${IMAGE_TAG} ."
                }
            }
        }

        stage('Stop & Remove Existing Container') {
            steps {
                script {
                    sh """
                        docker stop ${CONTAINER_NAME} || true
                        docker rm ${CONTAINER_NAME} || true
                    """
                }
            }
        }

        stage('Run Docker Container') {
            steps {
                script {
                    sh "docker run -d -p 8080:80 ${APP_NAME}:${IMAGE_TAG} --name ${CONTAINER_NAME}"
                }
            }
        }
    }

    post {
        always {
            echo "Build for ${APP_NAME} finished. Container: ${CONTAINER_NAME}"
        }
    }
}

Save and build the pipeline.

πŸ“Œ Once the Docker container is running, you can access the application.

Open your browser and go to http://EC2-public Ip:5001 to access your app.


Some Screenshot glimpse :

We have successfully Automate the deployment of a Python web application using a Jenkins pipeline on AWS.

Happy Learning…!

0
Subscribe to my newsletter

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

Written by

Ankita Lunawat
Ankita Lunawat

Hi there! I'm a passionate AWS DevOps Engineer with 2+ years of experience in building and managing scalable, reliable, and secure cloud infrastructure. I'm excited to share my knowledge and insights through this blog. Here, you'll find articles on: AWS Services: Deep dives into core AWS services like EC2, S3, Lambda, and more. DevOps Practices: Best practices for CI/CD, infrastructure as code, and automation. Security: Tips and tricks for securing your AWS environments. Serverless Computing: Building and deploying serverless applications. Troubleshooting: Common issues and solutions in AWS. I'm always eager to learn and grow, and I hope this blog can be a valuable resource for fellow DevOps enthusiasts. Feel free to connect with me on [LinkedIn/Twitter] or leave a comment below!