Deploy Flask Based Python Application on Alibaba Cloud(ECS)
Flask Deployment on Cloud Based Infrastructure
Step by step guide to install and configure flask to run on cloud infrastructure.
Python Flask on Alibaba Cloud(ECS)
This guide is designed to help you get started with Basic Python 2.7 Flask based application that runs on Alibaba Cloud Elastic Compute Service (ECS). You will learn how to configure flask application in the cloud with the step-by-step walkthrough for configuration to Deploy Flask Based Python Application.
In details this will cover the following: –
- How to Setup Linux Operating System based ECS instance
- Creating a Basic Flask Application
- Setting up required libraries & App Configuration for deployment
- Deploying Flask Application on ECS
Python flask is a web-development framework, this is one of the most-famous modules of python for web applications. It is easy to use, quick to learn, and also easy to create applications. And before you ask: It’s BSD licensed!
Miguel Grunberg’s Flask “Mega-tutorial” should teach you all the basics to get you started. You can also learn from the home website itself: Flask (A Python Microframework). The documentation is very neat, the support community is very active, and there are plenty of how to tutorials!
This tutorial will cover steps involved in the development of flask application and deployment of same on cloud-based ECS to secure & scale your application.
As a part of prerequisites, required library setup and virtual environment creation also covered step by step.
Let’s get started!
How to Setup Linux Operating System Based ECS instance
This first step involves the creation of ECS instance for deployment of flask application.
In This tutorial we have covered installation on Alibaba Cloud Elastic Compute Service (ECS) provides fast memory and the latest Intel CPUs to help you to power your cloud applications and achieve faster results with low latency. Deploy ECS instances with just a few clicks from the easy-to-use console and scale capacity up or down based on real-time demands.
This means you only pay for the resources you use and avoid the need to provision expensive IT infrastructure and hire large network teams.
You can also Get $300 New User Free Credit here on signup.
Let us look at the steps involved in running an UBUNTU ECS instance using the Alibaba Cloud Management Console
Step 1: Log in to your Console and navigate to “Elastic Compute Service” under the “Product”
Step 2: Select the instance list based on region
Step 3: Basic Configuration for ECS Instance
In this step, you need to select the basic configuration required for ECS Instance to be built. This includes the server configuration, Instance type, Operating system, Memory, vcpu etc. more details can be read at – How to Create Instance
- Select Pay as you Go
- Select Region as random
- Select Instance type –
- Architecture as x86 and Category as Entry Level Shared (You are free to select different variant as per your need and computation requirement)
Step 4: Additional Configuration for ECS Instance
On Same basic configuration page …once you scroll down you see image and storage details to be selected.
Here you can select the different variant of an operating system and Custom image.
- Select Ubuntu and Variant as 16.04
Step 5: Network Configuration
Once you are done with selection next step is the configuration for the network. You can adjust the details accordingly.
The most Important part while the configuration is security group. In this step, you need to select HTTP port 80 & HTTP PORT 443 to be open so that your application is accessible for outside network (Public)
Step 6: Give a real-world name to your ECS Instance
Let’s give some meaningful name to the instance and set the root password.
I suggest it to it later or as the preferred method to connect your instance should be ssh keys, not the user id and password. You could refer more details around this here –
Step 7: Confirmation & Activation
On the last page of configuration, make sure to review the details and accept the terms of Service.
Once done popup screen will appear with the confirmation saying your instance has been activated.
Finally, you will see the instance details like below… Please wait for few minutes before it gets started.
Set up Environment to Deploy Flask Based Python Application
Now its turn to make the environment ready to run the Simple Flask application we are going to build-
Few things to be noted:-
- Keep Note of IP Address of your Instance
- When you will connect the first time it will display the VNC Password, Make sure to note that somewhere safe
- If you are comfortable working with other SSH Client, You might need the SSH Private key to Connect to Instance, You can read more details around steps here.
- The only catch here is you need to convert .pem keys to formate that putty can understand. You can use Putty Key Generator Tool. Its pretty straightforward
- Just load the keys downloaded from Alibaba Console and save the private keys with the same name …putty will automatically add the required file extension.
Then you will be able to login Using username “root”.
Authenticating with public key "imported-openssh-key" Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-105-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage Welcome to Alibaba Cloud Elastic Compute Service ! root@techfossguru:~#
Python 2.7 Comes with the default installation on almost all the VM… If you wish to use other installation like Anaconda or MiniConda you can run all your standard installation to perform same on newly created VM…however for this blog post we will use venv (Virtual Environment)
A Virtual Environment put simply, is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. It enables multiple side-by-side installations of Python, one for each project.It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated.
lets create testapp(Root Directory for Flask Application)& Run virtualenv env to get to install virtual env. root@techfossguru:~/testapp# virtualenv env New python executable in /root/testapp/env/bin/python Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file. Installing setuptools, pip, wheel...done. root@techfossguru:~/testapp#
the above step will create a directory called “env” inside test app, which contains a directory structure similar to this. you can read more about virtual env at Real Python
|-env |---bin |---include |---lib |-----python2.7 |-------distutils |-------site-packages |---------pip |-----------commands |-----------compat |-----------models |-----------operations |-----------req |-----------utils |-----------vcs
Now you can switch to newly created env using below command. Notice the Shell prompt it will change to add (env) to depict that you are in new env.
root@techfossguru:~/testapp# source env/bin/activate (env) root@techfossguru:~/testapp#
let’s install the required library for the example.
Done! All Set to Run Flask Application on ECS.
Building Simple Flask Application & Deploy Flask Based Python Application
A Simple Flask application code looks something like this:
#Import Flask from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hey ! I am Trying to Run-Flask'
if __name__ == '__main__': app.run(debug=True)
Let’s run this on the cloud to see how it works. By Default, this will run on port 5000 which is not configured to listen to the inbound connection. so you might need to setup authorization policy for custom TCP rules for Port Range 5000.
if everything goes well you will be able to run flask application!
In this blog, we have created the ubuntu based instance on Alibaba ECS and set up small simple Flask application within a Python virtual environment.
We also need to create a WSGI entry point so that any WSGI-capable application server can interface with it, and then configured the uWSGI app server to provide this function with the help of nginx server. I will cover those in the next article!
If you need You can find out more details around here –