AWS Elastic Beanstalk is an easy-to-use service for deploying and scaling services and web applications. Bobcares with help of our AWS support services has shown with Deploying Flask app on AWS Beanstalk.
How to deploy Flask app on AWS Beanstalk
Flask is a web framework written in python. Flask provides you with the best tools and libraries technologies that allow you to build a web application. Ultimately, this web application can be some web pages, a blog, a commercial website, a wiki, or as big as a web-based calendar application.
You can quickly deploy and manage applications in the AWS Cloud with Elastic Beanstalk. It reduces the management of complexity without restricting choice or control. This means you can simply upload your application and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and monitoring health application.
Create an AWS Elastic Beanstalk environment
First, go to AWS Elastic Beanstalk and click on “create a new application” on the top right. Now type the Application name and short description (The name can be anything.) then click create.
Creating Environment
- Click “Create one now” to create an environment for the application.
- You have to select an environment tier for this tutorial we will consider a “web server environment”.
Environment Information
- Give your environment a name
- Choose a unique name for the Domain
- Write a short description
Base Configuration
- For this configuration, we are using “python” for our preconfigured platform
- We will upload the flask app later. In the application code tab keep “sample application “selected.
- Now click on “Create environment“.
Create a Simple Flask App
Alternatively, you can skip this part by downloading “Flask Zip File” which contains all the files needed for flask app, and you can go directly jump to the Uploading a Flask App section.
The other way is by Creating a new file called "application.py"
and paste the below code to file:
from flask import Flaskapplication = Flask(__name__)
@application.route("/")
def index():
return "Your Flask App Works!"
@application.route("/hello")
def hello():
return "Hello World!"
if __name__ == "__main__":
application.run(port=5000, debug=True)
Next, create a new file called "requirements.txt"
and paste the app dependencies into it:
Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0
Lastly, create a folder named ".ebextensions"
and Copy the code below and save it as a new file called app.config
in the folder .ebextensions
.
files:
"/etc/httpd/conf.d/wsgi_custom.conf":
mode: "000644"
owner: root
group: root
content: WSGIApplicationGroup %{GLOBAL}
Now you will need to zip all the files together including the hidden folder” to upload the flask app on Elastic Beanstalk. You can name the zip file "flask_app.zip"
or any other name you want to give.
Uploading Flask App on AWS Elastic Beanstalk
You will now have the flask app in a zip file with the following folder structure:
- application.py
- requirements.txt
- /.ebextensions
- app.config
Sometimes hidden files will include when you create the zip file that are not needed. This usually will cause an error when you upload on Elastic Beanstalk, in order to fix this follow the below steps.
Open a terminal and go to where the zip file is located, then run the given line in the terminal:
zip -d flask_app.zip __MACOSX/\*
You can replace flask_app.zip
with any other name that you wish for zip file if needed. This will clean up any extra hidden files that are involved in your zip file. In some cases, you might not find any hidden files. Now we are ready to upload the zip file to Elastic Beanstalk.
Now go back to your elastic beanstalk environment dashboard and click on “Upload and Deploy”. Choose your zip file from the “Choose File” option and then click deploy. Now, wait until your health symbol becomes a green checkmark, and then click on your website link above the health symbol.
If you followed all these steps successfully, your website link should take you to a page showing Your Flask App Works!
Debug errors
To debug the error go to the left side of the Elastic Beanstalk environment dashboard and click “Logs“. Then click on “Request Logs” and select Last 100 Lines for the most recent errors.
Click “Download“ and it will take you to a page to view the last 100 lines of errors that were found.
[Need assistance with similar queries? We are here to help]
Conclusion
Following these steps, you can Deploying Flask app on AWS Beanstalk. An AWS Elastic Beanstalk is a service for deploying applications that perform various AWS services, including EC2, S3, Simple Notification Service, CloudWatch, autoscaling, and Elastic Load Balancers.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
0 Comments