Seamless Python Deployment to AWS EC2 Using GitHub Actions
Deploying a Python application to an AWS EC2 instance can be streamlined using GitHub Actions. This guide will walk you through setting up a GitHub Actions workflow to automate the deployment process. The workflow will handle copying your application code to the EC2 instance, installing dependencies, and starting the application.
Step 1: Prerequisites
Before setting up the GitHub Actions workflow, ensure you have the following:
- AWS EC2 Instance: A running EC2 instance with SSH access.
- SSH Key: The private key (example-key.pem) for SSH access to the EC2 instance.
- GitHub Repository: A repository containing your Python application code.
- Python Application: A Python application with a requirements.txt file for dependencies.
Step 2: Store SSH Private Key in GitHub Secrets
- Go to your GitHub repository.
- Navigate to Settings > Secrets and variables > Actions.
- Click New repository secret.
- Name the secret EC2_SSH_PRIVATE_KEY and paste the contents of your .pem file (e.g., example-key.pem).
- Click Add secret.
Step 3: Create the GitHub Actions Workflow
- In your repository, create a .github/workflows directory if it doesn’t already exist.
- Inside the .github/workflows directory, create a file named deploy.yml.
- Add the following code to the deploy.yml file:
name: Deploy Python App to EC2
on:
push:
branches:
- main # Trigger the workflow on pushes to the main branch
jobs:
deploy:
runs-on: ubuntu-latest # Use the latest Ubuntu runner
steps:
- name: Checkout the repository
uses: actions/checkout@v3 # Check out the repository code
- name: Set up SSH Agent
uses: webfactory/ssh-agent@v0.7.0 # Set up SSH agent for authentication
with:
ssh-private-key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} # Use the private key stored in GitHub Secrets
- name: Add EC2 instance to known_hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -H ec2-12-34-56-78.compute-1.amazonaws.com >> ~/.ssh/known_hosts # Replace with your EC2 instance's public DNS
- name: Install rsync
run: sudo apt-get install -y rsync # Install rsync for file transfer
- name: Copy files to EC2 using rsync (exclude .git)
run: |
rsync -avz --exclude='.git' ./ ec2-user@ec2-12-34-56-78.compute-1.amazonaws.com:/home/ec2-user/app # Replace with your EC2 instance's public DNS
- name: SSH into EC2 and Deploy
run: |
ssh -o StrictHostKeyChecking=no ec2-user@ec2-12-34-56-78.compute-1.amazonaws.com << 'EOF' # Replace with your EC2 instance's public DNS
# Navigate to the app directory
cd /home/ec2-user/app
# Ensure Python3 and pip3 are installed
sudo yum install -y python3 python3-pip # For Amazon Linux 2
# sudo apt-get install -y python3 python3-pip # For Ubuntu
# Install dependencies
pip3 install --user -r requirements.txt
# Create and enable the systemd service (if not already done)
echo "[Unit]
Description=Python Application
After=network.target
[Service]
User=ec2-user
WorkingDirectory=/home/ec2-user/app
ExecStart=/usr/bin/python3 /home/ec2-user/app/app.py
Restart=always
[Install]
WantedBy=multi-user.target" | sudo tee /etc/systemd/system/python_app.service
# Reload systemd, start the service, and enable it to run on boot
sudo systemctl daemon-reload
sudo systemctl start python_app
sudo systemctl enable python_app
EOF
Step 4: Explanation of the Workflow
- Trigger:
- The workflow is triggered when changes are pushed to the main branch.
- Checkout the Repository:
- The actions/checkout@v3 action checks out the repository code.
- Set Up SSH Agent:
- The webfactory/ssh-agent@v0.7.0 action sets up the SSH agent and uses the private key stored in GitHub Secrets for authentication.
- Add EC2 Instance to known_hosts:
- The ssh-keyscan command adds the EC2 instance's public DNS to the known_hosts file to avoid SSH host key verification prompts.
- Install rsync:
- The rsync utility is installed to efficiently copy files to the EC2 instance.
- Copy Files to EC2:
- The rsync command copies the application files to the EC2 instance, excluding the .git directory.
- SSH into EC2 and Deploy:
- The workflow logs into the EC2 instance and performs the following tasks:
- Installs Python and pip (if not already installed).
- Installs dependencies from requirements.txt.
- Creates a systemd service to manage the Python application.
- Starts and enables the service to run on boot.
Step 5: Customize the Workflow
- Replace ec2-12-34-56-78.compute-1.amazonaws.com with your EC2 instance's public DNS.
- Replace /home/ec2-user/app with the desired deployment directory on your EC2 instance.
- Update the ExecStart path in the systemd service to point to your application's entry point (e.g., app.py).
Step 6: Push and Test the Workflow
- Commit the deploy.yml file to your repository and push it to the main branch.
- Go to the Actions tab in your GitHub repository to monitor the workflow.
- Once the workflow completes, your Python application should be deployed and running on the EC2 instance.
Step 7: Verify the Deployment
- SSH into your EC2 instance:
ssh -i "example-key.pem" ec2-user@ec2-12-34-56-78.compute-1.amazonaws.com
Check the status of the systemd service:
sudo systemctl status python_app
- Access your application via the EC2 instance's public IP or DNS.
Conclusion
By following this guide, you’ve set up a GitHub Actions workflow to automate the deployment of your Python application to an AWS EC2 instance. This workflow ensures that your application is deployed consistently and efficiently whenever changes are pushed to the main branch. You can further customize the workflow to suit your specific needs, such as adding environment variables or running tests before deployment. Happy deploying! 🚀
0 Comments
Like 0