You can use web functions in Function Compute to easily build web applications and API services. Web functions are compatible with web frameworks for many popular languages, such as Java SpringBoot, Python Flask, and Node.js Express, which allows you to quickly migrate existing applications. Function Compute manages the underlying compute resources for you. When your service is accessed through a browser or URL, the system automatically provisions and scales instances to meet demand. When there is no traffic, the instances are automatically released, and you pay only for the resources you use.
Overview
This topic describes how to deploy a Flask application using a web function in Function Compute. The overall process is as follows:
Develop and test the web application: Create a project on your local machine using the sample code. Edit and test the API code to ensure that the API works as expected. If you want to migrate an existing application, you can skip this step.
Generate a code package: Install the required dependencies into the local project directory. Then, package the project into a ZIP file for deployment to Function Compute.
Upload and deploy the application: Configure the function creation parameters and then upload the code package to create the function.
Test the web function: Invoke the web function to verify that the Flask application is running as expected.
Prerequisites
Procedure
You can use one of the following methods to create a web function and deploy your code.
Create a function in the Function Compute console: This method lets you quickly create a single function and is suitable for building lightweight applications or for quick validation.
Use the Serverless Devs command-line tool: This method is suitable for managing complex production projects and for automated deployments. You can use a YAML configuration file to manage multiple functions and their related cloud resources. For more information, see Quick start.
To simplify the process, this topic describes how to create a function in the Function Compute console.
Step 1: Develop and test the application
If you want to migrate an existing Flask application, you can skip this step and proceed to Step 2: Generate a code package.
Open a new command-line window on your local machine and run the following command to install the required
Flaskandtabulatelibraries.pip install Flask tabulateCreate a folder named
codeand a file namedapp.pywithin it. Paste the following sample code into the file. This code implements a simple calendar service that is packaged as an API. The API accepts a year and month, such as January 2025, and returns a calendar table for that month.import calendar from flask import Flask, request from tabulate import tabulate import logging import json logger = logging.getLogger() app = Flask(__name__) @app.route("/", methods=["POST"]) def get_month_calendar(): # Get the request ID and print the log. requestId = request.headers.get('x-fc-request-id', '') logger.info("FC Invoke Start RequestId: " + requestId) # Get the request content and check the request format. data = json.loads(request.stream.read().decode('utf-8')) if not ('year' in data and 'month' in data): message = "Request must be in format like {'year': '1999', 'month': '12'}" return message # Get the calendar table. result = print_calendar(year=int(data['year']), month=int(data['month'])) # Print the log that indicates the function execution is complete before the result is returned. logger.info("FC Invoke End RequestId: " + requestId) return result def print_calendar(year, month): # Get the calendar matrix for the specified year and month. cal = calendar.monthcalendar(year, month) # Change the dates that do not belong to the month from 0 to empty strings. cal = list(map(lambda sublist: list(map(lambda x: '' if x == 0 else x, sublist)), cal)) # Create table headers. headers = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"] # Use tabulate to print the calendar table. return tabulate(cal, headers=headers, tablefmt="grid", stralign='center', numalign='center') if __name__ == "__main__": app.run(host='0.0.0.0', port=9000)Run the
python3 app.pycommand to run your Flask application. If the outputRunning on http://127.0.0.1:9000is displayed, the Flask application is running. You can then use a cURL command to test the application.python3 app.py * Serving Flask app 'app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:9000 * Running on http://47.100.XX.XX:9000 Press CTRL+C to quitOpen a new terminal window and use cURL to send a sample request. A successful command returns a calendar table.
curl -X POST http://127.0.0.1:9000 -d '{"year": "2000", "month": "1"}'
You have now created and tested the project locally.
Step 2: Generate a code package
You will build a code package locally that is suitable for Function Compute. The code package must contain the following content:
The project files required by the web application.
If your application has dependencies that are not included in the runtime, you must add them to the code package. For information about the built-in dependencies for runtimes, see Built-in dependencies.
Install dependencies: In the root directory of your web application project, run the following command to install the required dependencies (in this example,
tabulate) into that directory. This command installs the dependencies into your project's root directory.NoteThe Python web function runtime already includes the
Flasklibrary. You only need to install thetabulatelibrary for the sample application to work.pip install -t . tabulatePackage all files in the project root directory.
Linux or macOS
In the project root directory, run the
zip code.zip -r ./*command. This command packages the project files and dependencies into a ZIP file, creating the code package.NoteMake sure that you have read and write permissions on the directory.
Windows
Go to the project directory, select all files, right-click the files, and then package them into a ZIP file. This packages the project files and dependencies into a ZIP file, creating the code package.
Step 3: Create and deploy the function
Select the web function type: Sign in to the Function Compute console. In the left-side navigation pane, click . In the top navigation bar, select the region where you want to create the function. On the page that appears, click Create Function, and then follow the on-screen instructions to create a Web Function.
Select a runtime: As shown in the following figure, select . This runtime has the dependencies that are required by the Flask framework.
NoteWe recommend using a Custom Runtime for your web function. A Custom Runtime lets you specify a startup command and a listening port, and supports concurrent request handling. A Built-in Runtimes does not provide these features and is more suitable for handling events that are generated by other cloud services.
If you prefer containerized deployment, you can use a Custom Container to create a web function. This runtime also supports concurrent request handling, but you must manage the dependencies yourself.
For a detailed comparison of runtimes, see Select a runtime.
Deploy the code package: As shown in the figure, upload the ZIP code package that you created in Step 2. Set the startup command to
python3 app.pyand the listening port to9000. If you are migrating an existing Flask application, modify the configuration based on the actual startup command and port. For example, Flask's default port is 5000.Click Create and wait for the function to be created.

Step 4: Test the function
Obtain the function URL: On the details page of the newly created function, click the Trigger tab. In the HTTP trigger configuration, copy the public endpoint.
If you are in a test environment and do not need authentication, you can change the Authentication Method to No Authentication in the HTTP trigger's configuration when or after creating the trigger.

Test the function: You can use cURL, Postman, or any HTTP client to invoke the function and verify that the Flask application works as expected. The following example uses a cURL command. Replace the URL with the endpoint that you copied in the previous step.
curl https://******.cn-hangzhou.fcapp.run -d '{"year": "2025", "month": "1"}'The web API returns the calendar information for January 2025. The following output is a sample.
NoteIf you use the default HTTP trigger configuration, the command returns a "MissingRequiredHeader" error. If you are in a test environment, you can change the Authentication Method of the trigger to No Authentication.
The sample command and output in this step apply only to the sample code. If you migrate an existing application, invoke the service based on the actual API name and parameters. For more information, see Use cURL to test a function.
(Optional) Access the function from a browser: The public endpoint provided by the HTTP trigger cannot be opened directly in a browser (which may trigger a forced download) and is intended for testing purposes only. We recommend that you bind a custom domain name to the function to enable browser access and for use in a production environment. For more information, see Configure a custom domain name.
(Optional) Step 5: Clean up resources
Function Compute is a pay-as-you-go service. You are not charged for unused functions. However, you may be charged for associated cloud resources, such as data stored in OSS and File Storage NAS, and created triggers.
If you need to delete a function, sign in to the Function Compute console. In the left-side navigation pane, click . Select a region. In the Actions column of the target function, click Delete. In the dialog box that appears, confirm that the function is not bound to any resources such as triggers, and then confirm the deletion.
Advanced operations
Now that you have created and deployed a web application to a web function, you can perform the following advanced operations based on your needs.
Develop code in the console: While this topic focuses on local development, which is useful for migrating existing applications, you can also simplify deployment by using WebIDE. This method eliminates the need to generate a code package locally and is suitable for rapid development iterations. For more information, see Use WebIDE.
NoteWebIDE supports only Python, Node.js, and PHP. For other languages, such as Java and Go, you can only upload compiled and packaged ZIP files or binary files.
Continuously deploy projects by using Serverless Application Center: To automate the build and deployment (CI/CD) of your code to Function Compute, use Serverless Application Center. For more information, see Implement continuous deployment for an existing Function Compute project by using Serverless Application Center.
Configure logging: To facilitate debugging, troubleshooting, or security auditing, we recommend that you configure logging for your functions. For more information, see Configure the logging feature.
References
For more information about how to add dependency packages for different languages, see the following topics: