Deploying a Flask app on an EC2 Instance
Introduction
Flask is a popular, lightweight web framework for Python, perfect for small to medium-sized applications. This guide will walk you through deploying a simple Flask app on an Amazon EC2 instance.
Step 1: Install Python on Your Computer
First, ensure Python is installed on your computer. You can download the latest version of Python from the official Python website. After installation, verify it by opening the Command Prompt (cmd) on Windows and typing:
python --version
This command will display the version of Python installed on your system. If you see a version number, you’re good to go!
Step 2: Write Your Flask App Code
Open your preferred code editor, such as Visual Studio Code (VS Code), and create a new Python file (app.py
). Then, type the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello world"
@app.route('/books')
def get_books():
return "Books"
@app.route('/users')
def get_users():
return "Users"
@app.route('/users/<int:id>')
def get_user(id):
return f'User {id}'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Explanation of the Code:
Importing Flask:
- The
from flask import Flask
line imports the Flask class from the Flask module.
Creating a Flask App:
app = Flask(__name__)
initializes a new Flask application.
Defining Routes:
@app.route('/')
defines the home route, returning "Hello world".@app.route('/books')
defines the route for/books
, returning "Books".@app.route('/users')
defines the route for/users
, returning "Users".@app.route('/users/<int:id>')
defines the route for/users/<id>
, returning the user ID.
Running the App:
- The
if __name__ == '__main__':
block runs the application on host0.0.0.0
and port5000
in debug mode.
Step 3: Launch a New EC2 Instance with Ubuntu
Log in to your AWS Management Console and follow these steps:
- Navigate to the EC2 Dashboard and click “Launch Instance.”
- Choose an Amazon Machine Image (AMI) and select “Ubuntu Server 20.04 LTS (HVM), SSD Volume Type.”
- Select an instance type (t2.micro is sufficient for this use case).
- Configure the instance details (default settings are fine for now).
- Add storage (default 8 GB is sufficient).
- Add tags (optional).
- Configure the security group to allow SSH, HTTP, and HTTPS traffic.
- Review and launch the instance.
- Select or create a key pair to access the instance, then click “Launch Instance.”
Step 4: Connect to the Instance Using EC2 Instance Connect
After your instance launches, you can connect to it using EC2 Instance Connect:
- Go to the EC2 Dashboard and select your running instance.
- Click the “Connect” button.
- Choose “EC2 Instance Connect” and click “Connect.”
This will open a terminal session connected to your EC2 instance.
Step 5: Set Up Your Environment on the EC2 Instance
Once connected to your EC2 instance, execute the following commands to set up Python, Flask, and your Flask app:
# Install Python 3
sudo apt install python3
# Update the package lists
sudo apt update
# Install pip for Python 3
sudo apt install python3-pip
# Install Flask for Python 3
sudo apt install python3-flask
# Create a directory for your app
mkdir newapp
# Change to the new directory
cd newapp
# Open a new file called app.py using nano editor
nano app.py
Paste the Flask App Code in Nano:
Copy the Flask code you wrote earlier and paste it into the nano
editor. Save and exit using the following steps:
- Press
Ctrl + O
to write the changes to the file. - Press
Enter
to confirm. - Press
Ctrl + X
to exit the editor.
Running Your Flask App
Now, start your Flask app by typing:
python3 app.py
Your Flask application should now be running and accessible from the public IP address of your EC2 instance on port 5000. Open a web browser and navigate to http://<your-ec2-public-ip>:5000
to see your "Hello world" message, and explore other routes like /books
and /users
.
Congratulations! You’ve successfully deployed a Flask app on an EC2 instance.