Deploying a Python Flask Application on an AWS EC2 Instance: A Step-By-Step Guide

Posted by

How to deploy Python Flask application on AWS EC2 Instance

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:

  1. 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.
  2. 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:
    • ssh -i your-ec2-key.pem ec2-user@your-ec2-public-dns

  3. Install Python and Flask on your EC2 Instance:
    • Update the package list and install Python by running the following commands:
    • sudo yum update
      sudo yum install python3

    • Install Flask using pip:
    • pip install Flask

  4. 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:
    • python3 -m venv venv

    • Activate the virtual environment:
    • source venv/bin/activate

  5. Run your Flask application on the EC2 Instance:
    • Set the FLASK_APP environment variable to point to your Flask application:
    • export FLASK_APP=your_flask_app.py

    • Run the Flask application in debug mode:
    • flask run --host=0.0.0.0 --port=80

  6. 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.

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!

0 0 votes
Article Rating
15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@moamenmohameed1346
7 months ago

What terminal you are using? Is it Linux or Ubuntu based ? Does any one knows ?

@user-fd9hq1cx1f
7 months ago

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?

@sahilamin6031
7 months ago

HELPED ME GET MY FIRST APP RUNNING 🙂 THANKS

@willp8681
7 months ago

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.

@teamkuware3647
7 months ago

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 ?

@laxmikantparate8115
7 months ago

Thank you so much sir

@thenameisnigel
7 months ago

How do you run the app in daemon mode? What command do you use?

@nameisramm
7 months ago

send me your repository

@Maurice-W
7 months ago

bien

@maheshkumarsg3436
7 months ago

Thank you very much I was able to deploy my app using this

@roshan83564
7 months ago

Awesome. Thank you for the tutorial

@iwillbeback101
7 months ago

How to run it without a port number?

@Nature-mh2rh
7 months ago

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)

@DanyMaorMico
7 months ago

Awesome, do you have a guide on how to connect my domain to the site I uploaded?

@rameshlanke5735
7 months ago

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