Test your application.

  • Once the server is restarted you can curl to instance Ip and see “Hello World”

  • You can also paste public IP in browser to see the response.

How To Deploy Flask APP On AWS EC2 Instance?

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.

Similar Reads

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

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

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

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

Step 4: Install Flask

After installing Python install Flask using below command....

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

Step 6: Test the application locally.

Curl the server to view the output. You should see “Hello World” as response....

Step 7: Install and configure nginx as reverse proxy.

Install the nginx server on instance using below command....

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

Step 9: Test your application.

Once the server is restarted you can curl to instance Ip and see “Hello World”...

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

How to Deploy Flask APP on AWS EC2 Instance – FAQ’s

Can I automate the deployment process for my Flask app on EC2?...