Deploy Traditional Web Frameworks to Alibaba Cloud Function Compute (Serverless)
This guide explains how to migrate classic web frameworks such as Bottle and Flask to Alibaba Cloud Function Compute using request integration, custom runtimes, and Serverless Devs, providing step‑by‑step code examples, deployment configurations, and alternative solutions for other FaaS platforms.
Background
Serverless is a programming paradigm; deploying traditional web frameworks (e.g., Django, Flask, Express, Next.js) to serverless platforms is difficult because they lack native support.
Request Integration Scheme
The scheme forwards the original API‑gateway request directly to Alibaba Cloud Function Compute HTTP functions without additional transformation, allowing rapid deployment of existing frameworks with minimal changes.
Bottle Example
# index.py
import bottle
@bottle.route('/hello/<name>')
def index(name):
return "Hello world"
if __name__ == '__main__':
bottle.run(host='localhost', port=8080, debug=True)Add a default_app object for Function Compute:
app = bottle.default_app()Full file:
# index.py
import bottle
@bottle.route('/hello/<name>')
def index(name):
return "Hello world"
app = bottle.default_app()
if __name__ == '__main__':
bottle.run(host='localhost', port=8080, debug=True)Set the function entry to index.app when creating the function.
Flask 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=8001)Configure the function entry as index.app to run the Flask project on Function Compute.
Custom Runtime and Container
Write a bootstrap script for a custom runtime. Example for an Express project:
#!/usr/bin/env bash
export PORT=9000
npm run startAlternatively, upload a custom container image that complies with the platform specifications.
Deployment with Serverless Devs
Create index.py as shown above, then define a resource file:
edition: 1.0.0
name: functionApp
access: default
services:
bottleExample:
component: devsapp/bottle
actions:
pre-deploy:
- run: pip3 install -t . -r requirements.txt
path: ./src
props:
region: cn-shenzhen
service:
name: serverless-devs-bottle
description: Example Bottle service
function:
name: bottle
description: Bottle project
memorySize: 256
code:
src: ./src
customContainerConfig:
command: '["python3"]'
args: '["./bottle/index.py"]'Deploy with the command:
s deployThe console displays deployment progress and results.
Other Cloud Providers
On AWS, Huawei Cloud, and Tencent Cloud, deploying traditional frameworks typically requires an API gateway and a conversion layer that transforms the event and context into a WSGI‑compatible environ object.
Example conversion code for Flask on Tencent Cloud:
import sys, json
from urllib.parse import urlencode
from flask import Flask
# ... (environment construction functions) ...
class FlaskLambda(Flask):
def __call__(self, event, context):
if 'httpMethod' not in event:
return super(FlaskLambda, self).__call__(event, context)
response = LambdaResponse()
body = next(self.wsgi_app(make_environ(event), response.start_response))
return {
'statusCode': response.status,
'headers': response.response_headers,
'body': body
}Because conversion can be cumbersome, many developers prefer using tools such as Serverless Devs or the Serverless Framework to automate the migration.
Conclusion
Deploying traditional web frameworks to Alibaba Cloud Function Compute is straightforward thanks to HTTP functions and triggers, offering low‑intrusion migration paths. Available options include native request integration, custom runtimes, custom containers, and developer‑tool‑based deployments, allowing developers to select the most suitable method for their needs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
