Fullstack Flask: Deploying Flask Apps on AWS EC2
When it comes to deploying fullstack applications, AWS EC2 (Elastic Compute Cloud) offers a powerful and flexible environment to host your Flask backend. EC2 allows developers to launch virtual machines in the cloud and configure them to run web applications at scale. In this blog, we'll walk through the process of deploying a Flask app on an AWS EC2 instance and discuss why EC2 is a reliable choice for fullstack deployment.
Why Choose AWS EC2 for Flask Deployment?
Deploying a Flask app on EC2 provides you with full control over your infrastructure. While platforms like Heroku or Render simplify deployment, EC2 offers:
Scalability: Add more resources or instances as your app grows.
Custom Configuration: Install any libraries or services you need.
Cost Flexibility: Pay-as-you-go model based on compute usage.
Integration: Seamless integration with other AWS services like RDS, S3, and Route 53.
Step-by-Step Guide to Deploy Flask on AWS EC2
1. Launch an EC2 Instance
Log into your AWS Management Console.
Navigate to EC2 and launch a new instance.
Choose an Amazon Linux 2 or Ubuntu AMI.
Select instance type (e.g., t2.micro for free-tier users).
Create a key pair to SSH into the instance.
Allow HTTP (port 80), HTTPS (443), and SSH (22) in your security group.
2. SSH into the Instance
Once your instance is running, connect using your key pair:
bash
ssh -i "your-key.pem" ec2-user@your-ec2-public-ip
3. Set Up the Environment
Update your system and install Python, pip, and Git:
bash
Copy
Edit
sudo apt update && sudo apt upgrade
sudo apt install python3-pip python3-dev nginx git
Clone your Flask app repository or upload your files to the server.
4. Install and Run Flask App
Navigate to your project directory:
bash
cd your-flask-app/
pip3 install -r requirements.txt
Test the app locally using:
bash
Copy
Edit
python3 app.py
Your app should now be running on port 5000.
5. Use Gunicorn as WSGI Server
To serve your Flask app in production, use Gunicorn:
bash
Copy
Edit
pip3 install gunicorn
gunicorn -w 3 -b 0.0.0.0:8000 app:app
This command runs Gunicorn with 3 worker processes, binding to port 8000.
6. Configure Nginx as a Reverse Proxy
Create an Nginx configuration file:
bash
Copy
Edit
sudo nano /etc/nginx/sites-available/flaskapp
Paste the following configuration:
nginx
Copy
Edit
server {
listen 80;
server_name your-ec2-public-ip;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Enable the site and restart Nginx:
bash
Copy
Edit
sudo ln -s /etc/nginx/sites-available/flaskapp /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
7. Access Your Flask App
Now, navigate to http://your-ec2-public-ip in your browser, and your Flask app should be live!
Conclusion
Deploying your Flask app on AWS EC2 gives you full control over your server environment, allowing for custom configurations, better scalability, and tight integration with other AWS services. While the process may seem manual at first, it lays a strong foundation for learning DevOps and managing production-grade deployments. Once you're comfortable, you can expand this setup with SSL (via Certbot), a database (like RDS), and automated CI/CD pipelines.
Learn FullStack Python Training Course
Read More : Introduction to Cloud Deployment for Fullstack Python Applications
Read More : Flask Microservices: Implementing Service Mesh with Istio
Read More : Fullstack Flask: Security Challenges in Microservices and Best Practices
Visit Quality Thought Training Institute
Comments
Post a Comment