Deploying a Flask application to AWS
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 :-
- Setting up an account on the AWS.
- Creating an AWS EC2 instance under the free tier with the required settings.
- Coding the flask application and deploying it to the EC2 instance.
- 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.
- Select the Launch Instance option under the Create Instance section.
- Select the Linux AMI machine eligible under free tier.
- Select the General Purpose t2.micro type instance under the free tier.
- Next, Leave Step 3 as it is and move to the Add Storage section.
- Leave Step 4 : Add Storage and Step 5 : Add Tags as it is and move to Step 6 : Configure Security Groups.
- In Step 6, configure the security groups as shown in the following figure :
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.
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 :
Feel free to suggest changes and point out bugs.
Hope you found this helpful
How do I run this app in background? Also is it production grade server?
I agree I feel this is incomplete
in step 7, how to create key
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?
In step6, add HTTP type. It works for me