IT, Programming, & Web Development › Forums › Web Development › AWS › Implementing CI/CD for a Flask Web Application on AWS Lightsail Using GitHub Actions
- This topic is empty.
-
AuthorPosts
-
July 26, 2024 at 3:56 am #3114
Source: Generated taking help of ChatGPT
In modern software development, Continuous Integration (CI) and Continuous Deployment (CD) are crucial practices for automating the build, test, and deployment process. This article will guide you through setting up CI/CD for a Flask web application hosted on an AWS Lightsail instance using GitHub Actions.
1. Overview of CI/CD
- Continuous Integration (CI): Automatically builds and tests code changes in a shared repository. This ensures that the code integrates smoothly with the existing codebase.
- Continuous Deployment (CD): Automatically deploys changes to a production environment after passing tests, ensuring that the latest version of the application is always live.
2. Prerequisites
Before diving into the setup, ensure you have:
- A Flask Web Application running on an AWS Lightsail instance.
- A GitHub Repository where your Flask application code is stored.
- GitHub Actions Enabled on your repository.
- SSH Access to your AWS Lightsail instance.
3. Setting Up the GitHub Repository
- Create a Repository on GitHub:
– Navigate to GitHub and create a new repository for your Flask application.
– Clone this repository to your local development environment.- Push Existing Code:
– If you have existing code on your AWS Lightsail instance, transfer it to your local machine or directly push it to GitHub if feasible.
– Follow these commands to push your code to GitHub:git init git remote add origin https://github.com/your-username/your-repo.git git add . git commit -m "Initial commit" git push -u origin main
4. Setting Up GitHub Actions
- Create a GitHub Actions Workflow:
– In your GitHub repository, navigate to the
.github/workflows
directory. If it doesn’t exist, create it.
– Inside this directory, create a YAML file for your workflow, e.g.,deploy.yml
.- Define Your Workflow:
– Here’s a sample
deploy.yml
file to automate deployment:name: Deploy to AWS Lightsail on: push: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.x' - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - name: Deploy to AWS Lightsail env: SSH_PRIVATE_KEY: ${{ secrets.AWS_SSH_PRIVATE_KEY }} HOST: ${{ secrets.AWS_HOST }} USER: ${{ secrets.AWS_USER }} run: | echo "$SSH_PRIVATE_KEY" > private_key.pem chmod 600 private_key.pem ssh -o StrictHostKeyChecking=no -i private_key.pem $USER@$HOST << 'EOF' cd /path/to/your/application git pull origin main source venv/bin/activate pip install -r requirements.txt sudo systemctl restart your-flask-app.service EOF
- Explanation of the Workflow:
- Checkout code: Retrieves the latest code from the GitHub repository.
- Set up Python: Sets up the Python environment and installs dependencies.
- Deploy to AWS Lightsail: Connects to your Lightsail instance via SSH, pulls the latest code, installs dependencies, and restarts the Flask application.
- Add Secrets to GitHub:
– Go to your repository’s settings and add the following secrets:
–AWS_SSH_PRIVATE_KEY
: Your SSH private key used for connecting to AWS Lightsail.
–AWS_HOST
: The public IP address of your Lightsail instance.
–AWS_USER
: The SSH user for your Lightsail instance.5. Setting Up AWS Lightsail
- Prepare Your Lightsail Instance:
– Ensure that your Flask application is set up correctly and is running as a service.
– Install Git if not already present:sudo apt-get update sudo apt-get install git
- Configure SSH Access:
– Make sure your Lightsail instance accepts SSH connections using the private key added to GitHub secrets.
- Ensure Application Service:
– Set up your Flask application to be managed as a service:
sudo nano /etc/systemd/system/your-flask-app.service
- Sample service file:
[Unit] Description=Gunicorn instance to serve your Flask app After=network.target [Service] User=ubuntu Group=ubuntu WorkingDirectory=/path/to/your/application ExecStart=/path/to/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 app:app [Install] WantedBy=multi-user.target
- Reload the systemd daemon and start the service:
sudo systemctl daemon-reload sudo systemctl start your-flask-app sudo systemctl enable your-flask-app
6. Testing the Setup
- Make Changes Locally:
– Modify your Flask application code locally.
- Push Changes to GitHub:
– Commit and push your changes to the
main
branch of your GitHub repository:git add . git commit -m "Update application" git push origin main
- Monitor GitHub Actions:
– Check the Actions tab in your GitHub repository to ensure the deployment workflow runs successfully.
- Verify Deployment:
– Visit your application’s URL on AWS Lightsail to confirm that the changes are live.
Conclusion
By following these steps, you’ve set up a robust CI/CD pipeline using GitHub Actions to automate the deployment of your Flask web application to an AWS Lightsail instance. This setup ensures that every change you make is automatically tested and deployed, improving development efficiency and application reliability.
Feel free to adapt and extend the workflow to fit your specific needs and requirements. Happy coding!
-
AuthorPosts
- You must be logged in to reply to this topic.