Codementor Events

Deploying a Flask application to AWS

Published Feb 12, 2018Last updated Aug 11, 2018

gpu_amazon_ec2_logo.png

What is Flask ?

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.Flask is awesome and it is the easiest way to learn web development. Start small, and then you can quickly add any upgrades you need — email, databases, forms, etc.

What is AWS ?

Amazon Web Services (AWS) is a subsidiary of Amazon.com that provides on-demand cloud computing platforms to individuals, companies and governments, on a paid subscription basis. Amazon Web Services is truly fantastic. There are a ton of cloud platforms out there, but none are more flexible than AWS. It seems like you can do anything on Amazon — host websites, create scalable databases, send emails, text messages, stream live video from your home — the possibilities are endless.

So, it seems a natural fit to create a flask application and push it to AWS.

Although, there are some excellent blog posts that help with stumbling blocks, but I couldn’t find an easy example for deploying a Flask application to AWS.

So here’s a step-by-step tutorial that will launch your Flask application to AWS EE2 instance.

The post consists of the following steps :-

  1. Setting up an account on the AWS.
  2. Creating an AWS EC2 instance under the free tier with the required settings.
  3. Coding the flask application and deploying it to the EC2 instance.
  4. Accessing the flask application from anywhere.

Now, lets dive deep into the steps :-

Step 1 - Setting up an account on AWS

Sign up on Amazon Web Services.

Note: AWS requires a credit card for registration. But our example will exist entirely on the AWS Free Tier, so you won’t be charged.

Step 2 - Creating an EC2 instance

This step consists of the following steps :-

  • Select the EC2 option under the Compute section.

1.png

  • Select the Launch Instance option under the Create Instance section.

2.png

  • Select the Linux AMI machine eligible under free tier.

3.png

  • Select the General Purpose t2.micro type instance under the free tier.

4.png

  • Next, Leave Step 3 as it is and move to the Add Storage section.

5.png

  • Leave Step 4 : Add Storage and Step 5 : Add Tags as it is and move to Step 6 : Configure Security Groups.

6.png

7.png

  • In Step 6, configure the security groups as shown in the following figure :

8.png

Finally, review and launch the instance by using an existing key pair or creating a new key pair.

Step 3 - Connecting to the instance and coding the Flask application

To connect to your EC2 instance, follow the steps discussed in this link.

Once you have connected to your EC2 instance, install flask on it using the command sudo pip install flask.

Once flask is installed, create a file called app.py and paste the following code :

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80)

Now, run the code using the following command sudo python app.py

Step 4 - Accessing the Flask application

To access the flask application, visit the IPv4 Public IP of your EC2 instance as given in the instance dashboard.

10.png

To access the flask application deployed on the instance in the above figure,visit the IP of the instance - 52.33.194.146 and get the following output :

11.png

Feel free to suggest changes and point out bugs.

Hope you found this helpful 😄

Discover and read more posts from Dushyant Rathore
get started
post commentsBe the first to share your opinion
Vijay Soni
5 years ago

How do I run this app in background? Also is it production grade server?

Valente FV
4 years ago

I agree I feel this is incomplete

Rishav Loomba
5 years ago

in step 7, how to create key

Tilek Mamutov
5 years ago

Everything goes well for me until the last step, when I try to visit the IP of my instance (let’s say 11.22.333.444_ the page just hangs for minutes and then I get:

This site can’t be reached 11.22.333.444 took too long to respond.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_TIMED_OUT

I double-checked the instance IP address, check Flask logs, and made sure HTTP traffic is allowed from any IP. What else would you recommend to check?

Keke Zhang
5 years ago

In step6, add HTTP type. It works for me

Show more replies