How to migrate and deploy traditional web frameworks to serverless architecture?

Rather than saying that the serverless architecture is a new concept, it is better to say that it is a new way of thinking, a new programming paradigm.

However, there are very few native serverless development frameworks. Taking web frameworks as an example, the current mainstream web frameworks "do not support serverless mode deployment", so we have to try to get in touch with serverless on the one hand, and on the other hand, we have no way to completely abandon traditional frameworks, so how to make traditional frameworks simpler, faster, and Deploying to the Serverless architecture more scientifically is a question worth exploring.

Request an integration solution

The request integration solution is actually to directly transmit the real API gateway request to the FaaS platform without adding any conversion logic in the middle. Taking the HTTP function of Alibaba Cloud Function Computing as an example, when you want to deploy traditional frameworks (such as Django, Flask, Express, Next.js, etc.) When it comes to bonuses such as scaling, thanks to Alibaba Cloud Function Computing's HTTP functions and HTTP triggers, users can not only quickly and easily deploy the framework to the Alibaba Cloud Function Computing platform, but also obtain the same experience as traditional development.

For example, to develop a Bottle project with Python's Bottle framework:
# index.py
import bottle
@bottle.route('/hello/')
def index(name):
return "Hello world"
if __name__ == '__main__':
bottle.run(host='localhost', port=8080, debug=True)

After that, debugging can be done locally directly. When you want to deploy the project to the Alibaba Cloud Function Computing Platform, you only need to add a default_app object:

app = bottle. default_app()
The code for the entire project is as follows:
# index.py
import bottle
@bottle.route('/hello/')
def index(name):
return "Hello world"
app = bottle. default_app()
if __name__ == '__main__':
bottle.run(host='localhost', port=8080, debug=True)

If you create a function on the Alibaba Cloud Function Compute Platform, set the entry function to index.app. In addition to the Bottle framework, the operation methods of other web frameworks are similar, and take Flask as an example:

# index.py
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=int("8001")
)

When creating a function, set the entry function to index.app to ensure that the Flask project runs on the Function Compute platform.

Of course, in addition to using the existing language-based Runtime (referring to the specific language runtime, such as Python3 runtime, Node. js12 runtime), we can also consider using Custom Runtime and Custom Container to achieve, for example, after a Web project is completed , you can write a Bootstrap file (write some startup commands in the Bootstrap file).
For example, to start an Express project, after preparing the Express project, you can directly create a Bootstrap file and configure the startup command into the file:

#!/usr/bin/env bash
export PORT=9000
npm run star

Alibaba Cloud Function Compute also provides a simpler web framework migration solution. As shown in the figure, it is an example of the traditional web framework migration function of the Alibaba Cloud Function Compute page.

Alibaba Cloud Function Compute Page Traditional Web Framework Migration Function

After selecting the corresponding environment, you only need to upload the code and make simple configurations to migrate the traditional web framework to the Alibaba Cloud Function Computing Platform.

If deploying through developer tools, take Serverless Devs as an example, first create index.py:
# -*- coding: utf-8 -*-
from bottle import route, run
@route('/')
def hello():
return "Hello World!"
run(host='0.0.0.0', debug=False, port=9000)

To sum up, it is very convenient to deploy and migrate traditional web frameworks through Alibaba Cloud Function Computing, and thanks to HTTP functions and HTTP triggers, the whole process is very low intrusive. Of course, there are many options for deploying traditional web frameworks on Alibaba Cloud.

• Programming language-based Runtime: only need to write the function entry.
• Custom Runtime: You only need to write Bootstrap.
• Custom Container: Just upload the image file directly according to the specification.
There are also various deployment paths, as follows.
• Create functions directly in the console.
• Create web applications in the application center.
• Leverage developer tools.

other plans

Compared with Alibaba Cloud's HTTP functions and HTTP triggers, other FaaS platforms need API gateways and a conversion layer to implement the deployment of traditional web frameworks to FaaS platforms.
As shown in the figure, taking the Python Web framework as an example, under normal circumstances, when using a framework such as Flask, you actually need to go through the Web Server to enter the next link, and the cloud function is a function that does not need to start the Web Server, so you can Call the wsgi_app method directly.

The environ here is the object after processing the event/context, that is, the work to be done by the so-called conversion layer; start_response can be considered as a special data structure, such as the response structure form.

Of course, the conversion work is still troublesome in some cases, so in many cases we can deploy traditional web frameworks with the help of common developer tools, such as open source developer tools Serverless Devs, Serverless Framework, etc.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us