My Latest Project Deploying Django App to EC2 with Shell Scripting

My Latest Project Deploying Django App to EC2 with Shell Scripting

Seamless Django Deployment: Simplifying Deployment with Error-Resilient Shell Scripting

Greetings! I'm thrilled to introduce you to my latest project, where I've ventured into the world of deploying Django applications using simple shell scripting techniques. Imagine a smooth, hassle-free process that ensures your web applications are up and running seamlessly – that's exactly what this project is all about. Join me as I guide you through each step, making deployment accessible to everyone.


Section 1: The Deployment Journey

Let's embark on our deployment journey together:

  1. Cloning Code from GitHub:

    • We start by fetching the code for our Django application from GitHub.

    • It's like downloading a recipe to bake a cake – you need the instructions before you can start baking!

  2. Installing Dependencies:

    • Next, we make sure we have all the ingredients (or dependencies) needed to run our application smoothly.

    • Think of it as gathering all the tools and ingredients you need to cook a delicious meal.

  3. Performing Necessary Restarts:

    • Sometimes, we need to restart certain services to make sure everything works perfectly.

    • It's like rebooting your computer to apply updates – it keeps everything running smoothly.

  4. Deploying the Application:

    • Finally, we put everything together and launch our Django application.

    • Picture pressing a button to start a machine – it's that simple!


Section 2: Error Handling Made Easy

Let's talk about how we handle errors to ensure a smooth deployment experience:

  • Cloning Error Handling:

      #!/bin/bash
    
      # Function to clone the Django app code
      code_clone() {
          echo "Cloning the Django app..."
          if [ -d "django-notes-app" ]; then
              echo "The code directory already exists. Skipping clone."
          else
              git clone https://github.com/LondheShubham153/django-notes-app.git || {
                  echo "Failed to clone the code."
                  return 1
              }
          fi
      }
    
    • This script starts by defining a function named code_clone responsible for cloning the Django app code from GitHub.

    • It checks if the code directory already exists. If it does, the script skips the cloning process to avoid duplication.

  • Dependency Installation Error Handling:

      # Function to install required dependencies
      install_requirements() {
          echo "Installing dependencies..."
          sudo apt-get update && sudo apt-get install -y docker.io nginx docker-compose || {
              echo "Failed to install dependencies."
              return 1 
          }
      }
    
    • The install_requirements function updates the package list and installs necessary dependencies like Docker, nginx, and docker-compose.

    • It uses apt-get update to refresh the package index and apt-get install -y to install packages without prompting for confirmation.

  • Necessary Restarts Error Handling:

      # Function to perform required restarts
      required_restarts() {
          echo "Performing required restarts..."
          sudo chown "$USER" /var/run/docker.sock || {
              echo "Failed to change ownership of docker.sock."
              return 1
          }
      }
    
    • The required_restarts function ensures that the current user has ownership of the Docker socket, allowing Docker commands to be executed without sudo.

    • It's essential for Docker commands to run smoothly and without permission issues.

  • Application Deployment Error Handling:

      # Function to deploy the Django app
      deploy() {
          echo "Building and deploying the Django app..."
          docker build -t notes-app . && docker-compose up -d || {
              echo "Failed to build and deploy the app."
              return 1
          }
      }
    
    • The deploy function builds a Docker image named notes-app using the Dockerfile in the current directory.

    • It then starts the Docker containers defined in the docker-compose.yml file in detached mode (-d), ensuring the application runs in the background.


You can find the complete code and instructions on GitHub: [GitHub URL]


To start, let's ensure that the script file (deploy_django_app_code.sh) has the necessary permissions to be executed. You can do this using the chmod command in the terminal. Here's how you can give execute permissions to the file:

chmod 755 deploy_django_app_code.sh

To execute the shell script file deploy_django_app_code.sh, navigate to its directory in the terminal and run ./deploy_django_app_code.sh.

./deploy_django_app_code.sh

After executing the shell script deploy_django_app_code.sh, you can verify that the deployment is successful by running the command docker ps as shown in the screenshot below:

This command displays a list of running Docker containers on your system, including the container for your deployed Django application. If the deployment was successful, you should see the Docker container for your Django application listed in the output of docker ps.

After confirming the successful deployment using docker ps, open port 8000 by editing the inbound rules of your AWS security group to allow TCP traffic on port 8000 from any source (0.0.0.0/0). This enables external access to your Django application.

Finally, to verify that the application is running, copy your server's public IP address and append ":8000" to it in a new web browser tab. This should display your Django application, confirming that it is successfully deployed and accessible externally.

In summary, deploying Django applications using shell scripting streamlines the deployment process, automating essential tasks like code cloning, dependency installation, and application deployment with Docker. By executing the deployment script, we ensure a seamless deployment experience while reducing manual errors. Additionally, configuring network settings, such as opening ports on AWS, enables external access to the application. With these streamlined processes in place, deploying Django applications becomes more efficient, reliable, and easily accessible to users.