Deploying a Flask Application to AWS EC2

beginner
8 min

Deploying a Flask Application to AWS EC2

Welcome to this comprehensive guide on deploying a Flask application to Amazon Web Services (AWS) EC2! By the end of this tutorial, you'll be able to deploy your own Flask app on AWS EC2, making it accessible to the world.

Let's get started!

🎯 What is AWS EC2?

Amazon Elastic Compute Cloud (EC2) is a web service that provides scalable computing capacity in the AWS cloud. It allows you to deploy your Flask application as a web service easily and securely.

📝 Prerequisites

  • An AWS account (sign up for a free trial if you don't have one)
  • Python 3.x installed on your local machine
  • Basic understanding of Flask and Python

🎯 Creating a Flask Application

Before we deploy our application, let's create a simple Flask app.

python
# app.py from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, World!" if __name__ == '__main__': app.run()

💡 Pro Tip:

  • Always name your application app.py for ease of deployment.

🎯 Setting Up AWS EC2

  1. Sign in to the AWS Management Console.
  2. Navigate to the EC2 Dashboard.
  3. Click on "Launch Instance" and select the Amazon Linux 2 AMI.
  4. Configure your instance settings and add storage if needed.
  5. Add a security group and configure it to allow HTTP traffic.
  6. Launch the instance.

📝 Note:

  • Make sure to take a note of your instance's Public IP address.

🎯 Installing Flask on EC2

  1. Connect to your instance using SSH.
  2. Update the package list by running sudo yum update -y.
  3. Install Python3 and pip by running sudo yum install python3 python3-pip -y.
  4. Install Flask by running pip3 install flask.

💡 Pro Tip:

  • Always install Flask using pip3.

🎯 Creating a Deployment Script

Create a file named deploy.sh with the following content:

bash
#!/bin/bash # Install gunicorn (a WSGI HTTP Server for Python apps) pip3 install gunicorn # Copy app.py to the appropriate directory cp app.py /var/www/html/ # Start the gunicorn server gunicorn -b 0.0.0.0:8080 app:app

💡 Pro Tip:

  • Replace app.py with the name of your Flask application file.

🎯 Making the Script Executable

Run the following command to make the deploy.sh script executable:

bash
chmod +x deploy.sh

🎯 Deploying the Application

Run the deploy.sh script to deploy your application:

bash
./deploy.sh

Your Flask application should now be accessible at your instance's Public IP address on port 8080.

📝 Note:

  • You can modify the gunicorn command in the deploy.sh script to start the server on port 80, making it accessible without specifying the port number.

💡 Pro Tip:

  • Set up an Elastic Load Balancer (ELB) to handle traffic and scale your application.

🎯 Wrapping Up

Congratulations! You've successfully deployed your Flask application to AWS EC2. With this knowledge, you can deploy any Python-based web application on AWS EC2, making it accessible to the world.

🎯 Quiz

Quick Quiz
Question 1 of 1

Which command installs gunicorn on EC2?

Quick Quiz
Question 1 of 1

What is the name of the deployment script file?