How to Deploy a Flask App to AWS

In this tutorial, we will walk through the process of deploying a Flask app to AWS (Amazon Web Services). AWS is a cloud computing platform that offers a wide range of services, including hosting and running applications.

Prerequisites

Before getting started, make sure you have the following prerequisites:

  • A Flask app that you want to deploy. If you don’t have one, you can create a simple Flask app by following the Flask official documentation.
  • An AWS account. If you don’t have one, you can sign up for a free tier account on the AWS website.

Step 1: Create an EC2 Instance

The first step is to create an EC2 (Elastic Compute Cloud) instance, which will act as a virtual server to host your Flask app.

  1. Go to the AWS Management Console and navigate to the EC2 service.
  2. Click on “Launch Instance” to start the instance creation process.
  3. Select an Amazon Machine Image (AMI) for your instance. For a Flask app, you can choose the “Amazon Linux 2 AMI”.
  4. Choose an instance type based on your requirements. For a small Flask app, a t2.micro instance should be sufficient.
  5. Configure instance details such as the number of instances, network settings, etc. For most cases, the default settings will work.
  6. Add storage if needed. The default storage configuration should be fine for a basic Flask app.
  7. Add tags to your instance for better organization, although this step is optional.
  8. Configure security groups to allow inbound traffic to your instance. For a Flask app, you will need to allow inbound HTTP traffic on port 80.
  9. Review the instance details and click on “Launch” to create the instance.

Once the instance is created, note down the public IP address or DNS name of your instance, as you will need it to access your Flask app later.

Step 2: Connect to the EC2 Instance

To deploy a Flask app to your EC2 instance, you need to establish a secure connection to the instance using SSH (Secure Shell).

  1. Open a terminal on your local machine (or use an SSH client).
  2. Change the permissions of the private key file that you used while launching the instance. Run the following command, replacing path/to/key.pem with the actual path to your private key file:
    chmod 400 path/to/key.pem
    
  3. Connect to the EC2 instance using the SSH command. Replace your-instance-public-ip with the actual public IP address or DNS name of your instance:
    ssh -i path/to/key.pem ec2-user@your-instance-public-ip
    

    You might see a warning about the authenticity of the host. Simply type yes to continue the connection.

Congratulations! You are now connected to your EC2 instance.

Step 3: Install Dependencies

Next, you need to install the necessary dependencies on your EC2 instance to run your Flask app.

  1. Update the package index on your EC2 instance:
    sudo yum update -y
    
  2. Install Python and pip on your EC2 instance:
    sudo yum install python3 -y
    sudo amazon-linux-extras install python3.8 -y
    sudo yum install python3-pip -y
    
  3. Install virtualenv to isolate your Flask app’s environment:
    sudo pip3 install virtualenv
    
  4. Create a new virtual environment for your Flask app:
    python3 -m venv myenv
    
  5. Activate the virtual environment:
    source myenv/bin/activate
    
  6. Install Flask and any other dependencies required by your Flask app:
    pip install Flask
    

Now your EC2 instance is ready to run your Flask app.

Step 4: Upload Your Flask App

To deploy your Flask app, you need to upload the app’s files to your EC2 instance.

  1. Open a new terminal window on your local machine (while keeping the SSH connection to your EC2 instance open).
  2. Change to the directory where your Flask app’s files are located.
  3. Use the scp command to securely copy your app’s files to your EC2 instance. Replace path/to/key.pem with the actual path to your private key file, and your-instance-public-ip with the actual public IP address or DNS name of your instance:
    scp -i path/to/key.pem -r ./* ec2-user@your-instance-public-ip:/home/ec2-user/
    
  4. Switch back to the SSH connection to your EC2 instance and navigate to the directory where you uploaded your app’s files:
    cd /home/ec2-user/
    
  5. Run your Flask app on the EC2 instance:
    python app.py
    

    If you experience any issues related to port conflicts, you can run the Flask app on a different port by specifying the -p option followed by the desired port number.

Now your Flask app should be running on your EC2 instance, accessible through the public IP address or DNS name of the instance. You can test it by opening a web browser and navigating to `http://your-instance-public-ip`.

Step 5: Run the Flask App as a Service

Currently, your Flask app is running in the foreground, which means it will be shut down if your SSH connection to the EC2 instance is closed. To keep your Flask app running even after you disconnect, you need to run it as a service.

  1. Press Ctrl+C to stop your Flask app.
  2. Create a systemd service file with the following command:
    sudo nano /etc/systemd/system/flaskapp.service
    
  3. Add the following content to the file:
    [Unit]
    Description=Flask App Service
    After=network.target
    
    [Service]
    User=ec2-user
    WorkingDirectory=/home/ec2-user/
    ExecStart=/home/ec2-user/myenv/bin/python app.py
    Restart=always
    
    [Install]
    WantedBy=default.target
    
  4. Save and close the file by pressing Ctrl+X, then Y, and finally Enter.
  5. Enable the Flask app service to start automatically at boot time:

    sudo systemctl enable flaskapp
    
  6. Start the Flask app service:
    sudo systemctl start flaskapp
    

Congratulations! Your Flask app is now running as a service on your EC2 instance.

Conclusion

In this tutorial, you learned how to deploy a Flask app to AWS using an EC2 instance. We covered the process of creating an EC2 instance, connecting to it, installing dependencies, uploading the app’s files, and running the app as a service. With your Flask app deployed on AWS, it can be accessed by anyone with an internet connection.

Keep in mind that this tutorial covers the basic steps to deploy a Flask app to AWS. Depending on your specific requirements, you might need to configure additional services or make further optimizations. Feel free to explore the AWS documentation for more advanced deployment options and best practices.

Related Post