How to deploy Python Flask application on AWS EC2 Instance
Python Flask is a popular web framework for building web applications. If you have developed a Flask application and want to deploy it on an AWS EC2 Instance, follow these steps:
- Create an AWS EC2 Instance:
- Go to the AWS Management Console and navigate to the EC2 dashboard.
- Click on the ‘Launch Instance’ button to create a new EC2 instance.
- Choose an Amazon Machine Image (AMI) with Python pre-installed.
- Choose an Instance Type and configure the Instance details.
- Create a new key pair or use an existing one to secure access to your instance.
- Launch the instance.
- SSH into your EC2 Instance:
- Open a terminal and use the SSH command to connect to your EC2 instance.
- Copy the public DNS or IP address of your instance and replace ‘your-ec2-public-dns’ in the command below:
- Install Python and Flask on your EC2 Instance:
- Update the package list and install Python by running the following commands:
- Install Flask using pip:
- Upload your Flask application to the EC2 Instance:
- Copy your Flask application files to the EC2 instance using SCP or SFTP.
- Create a virtual environment for your Flask application:
- Activate the virtual environment:
- Run your Flask application on the EC2 Instance:
- Set the FLASK_APP environment variable to point to your Flask application:
- Run the Flask application in debug mode:
- Access your Flask application in a web browser:
- Open a web browser and enter the public DNS or IP address of your EC2 instance.
- Your Flask application should now be running on the AWS EC2 Instance.
ssh -i your-ec2-key.pem ec2-user@your-ec2-public-dns
sudo yum update
sudo yum install python3
pip install Flask
python3 -m venv venv
source venv/bin/activate
export FLASK_APP=your_flask_app.py
flask run --host=0.0.0.0 --port=80
Deploying a Python Flask application on an AWS EC2 Instance is a straightforward process that allows you to easily host and scale your web applications. Follow the steps above to deploy your Flask application on AWS EC2 today!
What terminal you are using? Is it Linux or Ubuntu based ? Does any one knows ?
I followed the steps. Can connect via ssh and filezilla. Cant launch the hello world app though. Says the site cant be reached. Ive verified that I am using the correct Public IP adress and have the correct security rule group setup. Anything else I can check?
HELPED ME GET MY FIRST APP RUNNING 🙂 THANKS
How did you navigate to the ec2 user directory in the terminal (at 9:20)? I am on mac and can't figure out how to navigate there to run those commands.
Awesome . Thank you for the tutorial . Facing a Problem . "/" endpoint is working but other endpoints from app.py are now working. What Could be the issue ?
Thank you so much sir
How do you run the app in daemon mode? What command do you use?
send me your repository
bien
Thank you very much I was able to deploy my app using this
Awesome. Thank you for the tutorial
How to run it without a port number?
Hi, how to do this in production env? Like if a user visits the url, then he sees the app? (Without us running the app manually)
Awesome, do you have a guide on how to connect my domain to the site I uploaded?
I did all and unable to access my app using the link
It is running in the terminal well but not in the url
Can you please help me