In today's fast-paced development environment, setting up Continuous Integration and Continuous Deployment (CI/CD) pipelines is critical to ensuring smooth, automated workflows. Have you ever wondered how to efficiently deploy a Django application on AWS Lightsail and integrate it with GitHub Actions for seamless CI/CD? This guide will walk you through the process step-by-step, from instance creation to configuring GitHub Actions, giving you a robust and scalable deployment setup.
Introduction
Deploying web applications can be a complex task, especially when working with frameworks like Django. Whether you're a seasoned developer or just starting, ensuring that your application is deployed correctly and can handle traffic without issues is crucial. This tutorial focuses on setting up a Django application on AWS Lightsail, one of Amazon's powerful, yet user-friendly cloud services. We'll also integrate GitHub Actions for CI/CD, making your deployment process as smooth as possible.
By the end of this guide, you will have a fully functional Django application running on AWS Lightsail, automatically deployed via GitHub Actions every time you push changes to your repository. Let's dive in!
Step 1: Setting Up an AWS Lightsail Instance
- Create an Instance
- Visit the AWS Lightsail console.
- Choose Linux/Unix as the platform.
- Select OS Only and choose Ubuntu 22.04 LTS.
- Under Change SSH key pair, download the key pair for future use.
- Select the $12 plan for sufficient resources.
- Click Create Instance.
Step 2: Configuring Your Instance
Connect to Your Instance via SSH
- Download and save your
.pem
file securely. - Open your terminal (or PowerShell if you're on Windows).
- Set the correct permissions for the
.pem
file to avoid permission errors:$path = ".\Ubuntu-server.pem"
icacls.exe $path /reset icacls.exe $path /GRANT:R "$($env:USERNAME):(R)" icacls.exe $path /inheritance:r - Connect to your instance:
ssh -i "Ubuntu-server.pem" ubuntu@your-instance-ip
- Download and save your
Install Necessary Packages
- Update and install essential packages:
sudo apt update sudo apt install python3-venv python3-dev libpq-dev postgresql postgresql-contrib nginx curl
- Set up PostgreSQL:
sudo -u postgres psql CREATE DATABASE hrms; CREATE USER dbusername WITH PASSWORD 'dbpassword'; ALTER ROLE dbusername SET client_encoding TO 'utf8'; ALTER ROLE dbusername SET default_transaction_isolation TO 'read committed'; ALTER ROLE dbusername SET timezone TO 'UTC'; GRANT ALL PRIVILEGES ON DATABASE hrms TO dbusername; \q
- Update and install essential packages:
Step 3: Setting Up Your Django Project
Create and Activate a Virtual Environment
- Create a directory for your project:
mkdir ~/project-name cd ~/project-name/
python3 -m venv .venv source .venv/bin/activate - Install Django and Gunicorn:
pip install django gunicorn psycopg2-binary
- Create a directory for your project:
Run Django Server
- Allow traffic on port 8000:
sudo ufw allow 8000
- Run your Django development server:
python manage.py runserver 0.0.0.0:8000
- Allow traffic on port 8000:
Configure AWS Lightsail Firewall
- Go to your instance’s Networking tab.
- Add a custom TCP rule for port 8000.
Step 4: Setting Up GitHub Actions for CI/CD
Prepare Your GitHub Repository
- Create a production branch from the main branch.
- Clean up your repository by deleting unnecessary folders.
Add GitHub Actions Runner
- Navigate to your GitHub organization's settings, then go to Actions > Runners.
- Follow the instructions to set up a self-hosted runner
mkdir actions-runner && cd actions-runner curl -o actions-runner-linux-x64-2.317.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.317.0/actions-runner-linux-x64-2.317.0.tar.gz tar xzf ./actions-runner-linux-x64-2.317.0.tar.gz ./config.sh --url https://github.com/username --token YOUR_GITHUB_TOKEN
Add Secrets to GitHub
- Go to Settings > Secrets and variables > Actions in your repository.
- Add your Cloudflare API credentials as secrets:
CLOUDFLARE_ACCOUNT_ID
CLOUDFLARE_API_TOKEN
Create GitHub Actions for Media and Static Files
- Create workflows in your
.github/workflows
directory to upload media and static files to Cloudflare when changes are pushed:
name: Media To CDN on: push: paths: - 'media/**' jobs: deploy: runs-on: ubuntu-latest permissions: contents: read deployments: write steps: - name: Checkout uses: actions/checkout@v3 - name: Publish Media uses: cloudflare/pages-action@v1 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} projectName: 'projectName'
directory: './media/' gitHubToken: ${{ secrets.GITHUB_TOKEN }}name: Static To CDN on: push: paths: - 'static/**' jobs: deploy: runs-on: ubuntu-latest permissions: contents: read deployments: write steps: - name: Checkout uses: actions/checkout@v3 - name: Publish Static uses: cloudflare/pages-action@v1 with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} projectName: 'projectName'
directory: './static/' gitHubToken: ${{ secrets.GITHUB_TOKEN }}- Create workflows in your
Deploy Your Server with GitHub Actions
- Trigger your GitHub Actions by pushing changes to your repository.
Conclusion
Setting up Django on AWS Lightsail and integrating it with GitHub Actions for CI/CD might seem challenging at first, but with the right steps, it can be a straightforward and rewarding process. By automating your deployment process, you save time and reduce the risk of human error, ensuring that your application is always up-to-date with the latest changes. With AWS Lightsail’s simplicity and the power of GitHub Actions, you're well-equipped to manage your deployments efficiently. Happy coding!
By following this guide, you've established a robust deployment pipeline that not only saves time but also enhances your application's reliability and scalability.
Keywords:
- Django AWS Lightsail,
- Django CI/CD pipeline,
- GitHub Actions for Django,
- AWS Lightsail deployment,
- Continuous Integration Django,
- Continuous Deployment GitHub Actions,
- Python web development,
- Cloud deployment Django,
- Automate Django deployment,