How To Deploy Flask APP On AWS EC2 Instance?
Last Updated :
06 Jan, 2025
In this article, we will study how we can deploy our existing Flask application in AWS EC2. We will also see how to use the public IP of the EC2 instance to access the flask application. For this article, you should know about setting up EC2 in AWS. So, let's begin with the deployment.
Primary Terminologies:
- Flask: A micro web framework for Python used to build web applications. Flask is known for its simplicity and ease of use.
- AWS (Amazon Web Services): A cloud computing platform that provides a wide range of services, including computing power, storage, and databases, allowing users to deploy and scale applications.
- EC2 Instance: Elastic Compute Cloud (EC2) instances are virtual servers in the AWS cloud. They can be used to run applications and host websites.
Prerequisites:
- An AWS account with EC2 access.
- An EC2 instance allows HTTP traffic (port 80/443).
- A Flask application that you want to deploy.
Steps to Deploy Flask Application in AWS EC2 with Ubuntu Server
Step 1: Create an EC2 instance allowing HTTP traffic.
- First, we have to create an Amazon EC2 instance running any Linux distribution of your choice.
- Under Launch Instance on AWS, give your instance a name and select Ubuntu Server as the OS.
- Specify other options as per your choice.
.png)
- Make sure you allow HTTP traffic to the instance. You can also allow HTTPS if you want.
.png)
- After specifying all options correctly click launch instance.
Step 2: Connect to instance
- Once the instance is started successfully copy the public IP address assigned you will need it later. You may have assigned an elastic ip address which is also fine.
- Connect to the instance using PuTTY or OpenSSH.
ssh -i <key-file-name> username@<public-ip-address>
- Once you connect to the instance we can start next steps.
Step 3: Install Python Setup the enviroment.
- Download and install Python and other Flask requirements according to your project. Ubuntu may have already installed Python. If it is not there install using the below command.
sudo apt install python3
Step 4: Install Flask
- After installing Python install Flask using below command.
pip install flask
.jpg)
Step 5: Create simple flask application.
- Now add your project to the instance or you can create a new flask project.
- For this article we will setup a simple flask project. Create a hello.py file in your project folder in your favorite editor and paste a simple flask code as below.
nano hello.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
- Save and exit the file. Then run the file to start flask server.
python hello.py
- Send the server to background to keep it running.
.png)
Step 6: Test the application locally.
- Curl the server to view the output. You should see "Hello World" as response.
curl http://127.0.0.1:5000
Step 7: Install and configure nginx as reverse proxy.
- Install the nginx server on instance using below command.
sudo apt install nginx
- Check the server is installed and running by running curl to instance public Ip address. You should see nginx landing page.
.png)
Step 8: Configure Reverse proxy.
- Now go to /etc/nginx/sites-enabled. Inside this folder create a file with name as your Instance public IP address.
.png)
- Paste the server configuration in this file for reverse proxy. the content will look like below.
server {
listen 80;
listen [::]:80;
server_name <YOUR INSTANCE IP>;
location / {
proxy_pass http://127.0.0.1:5000;
include proxy_params;
}
}
- save and close the file .
- restart nginx server. After restart check the status of server which should be running.
sudo systemctl restart nginx
sudo systemctl status nginx
.jpg)
Step 9: Test your application.
- Once the server is restarted you can curl to instance Ip and see "Hello World"
.png)
- You can also paste public IP in browser to see the response.
.png)
Conclusion:
We have successfully deployed our flask application on EC2 instance in AWS. Deploying application on EC2 makes it easier for web applications deployment. EC2 instances can be configured for more secure and upgraded web application servers.
Troubleshooting
- If you can't access the landing page, make sure your Nginx server is allowed on the firewall. If not add Nginx to the allowed apps in the firewall.
- If you get an access denied error, make sure HTTP access is enabled for the EC2 instance.
Similar Reads
Deploy Flask App on AWS EC2 Windows Do you want to host a Windows-based Python Flask application on Amazon Web Services (AWS) Elastic Compute Cloud (EC2)? Flask is a frequently utilized framework for building web apps, APIs, and prototypes because of its ease of use and versatility. AWS EC2 provides virtual machines that are scalable,
5 min read
How to Deploy Flask App on Kubernetes? A lightweight Python framework providing simplicity and flexibility in development, Flask can be utilized to create web applications. In contrast, Kubernetes is an effective framework for container orchestration that facilitates the deployment, scaling, and management of containerized applications.
6 min read
How To Install Docker On AWS EC2 ? You can use the following instructions to install Docker on an AWS EC2 instance. Note that depending on the Linux distribution your EC2 instance is running, there may be differences in certain commands or processes. What is Docker?Docker is an OS-level virtualization that provides tools for building
3 min read
How To Deploy Python Application In AWS? In this article, we will explore how one as a Python developer can deploy the application by harnessing the capabilities of AWS. AWS, a leading cloud computing platform, offers a wide range of services to help developers build, deploy, and manage applications at scale EC2. It provides scalable compu
4 min read
How to Deploy Django Application in AWS EC2? In this article, we will study how we can deploy our existing Django web application to Windows Server in AWS EC2. We will also see how to use the public IP of the EC2 instance to access the Django application. For this article, you should know about setting up EC2 in AWS. We will see how to deploy
3 min read