All Products
Search
Document Center

Function Compute:Quickly create a web application using web functions

Last Updated:Oct 10, 2025

You can easily build web applications and API services using web functions in Function Compute. Web functions are compatible with popular web frameworks, such as Java SpringBoot, Python Flask, and Node.js Express. This compatibility lets you quickly migrate existing applications. Function Compute manages the underlying computing resources for you. When a service is accessed through a browser or URL, Function Compute automatically starts and scales instances as needed. When there are no requests, Function Compute automatically destroys the instances. You pay only for the resources that you use.

Overview of web function creation

This topic describes how to deploy a Flask application using a web function in Function Compute. The process is as follows:

  1. Develop and test the web application: Create a local project using the sample code. Edit and test the API code to ensure that the API functions as expected. If you want to migrate an existing application, you can skip this step.

  2. Generate a code package: Install the required dependency libraries in the local project directory. Then, package the project into a ZIP file for deployment to Function Compute.

  3. Upload and deploy the application: Configure the function creation parameters and upload the code package to create the function.

  4. Test the web function: Invoke the web function to verify that the Flask application is running correctly.

Prerequisites

Create an Alibaba Cloud account

Create an Alibaba Cloud account and complete identity verification.

Activate Function Compute

If you registered an Alibaba Cloud account and completed identity verification after August 27, 2024, you can use Function Compute directly without activation. When you log on to the Function Compute console for the first time, you can also follow the on-screen instructions to claim a free resource plan. For more information, see Trial quota.

For Alibaba Cloud accounts registered before August 27, 2024, follow these steps to activate the service.

  1. Visit the Function Compute home page.

  2. Click Console, go to the service activation page, and then click Activate Now to activate the service and go to the Function Compute console.

    Note
    • We recommend that you activate the service with an Alibaba Cloud account and manage functions and other applications using Resource Access Management (RAM) users. You can grant the required access policies to RAM users based on the Principle of Least Privilege (PoLP). For more information, see Access policies and examples.

  3. (Optional) When you log on to the Function Compute console for the first time, click OK in the Alibaba Cloud Service Authorization dialog box to create a service-linked role. This allows Function Compute to access other Alibaba Cloud services.

    After the role is created, Function Compute can access your cloud resources, such as VPC, ECS, SLS, and Container Registry.

Procedure

You can create a web function and deploy your code in one of the following ways.

  • Function Compute console: Suitable for scenarios such as building lightweight applications or performing quick validations. You can quickly create a single function.

  • Serverless Devs command line interface: Suitable for scenarios such as managing complex production projects and automating deployments. You can use a YAML configuration file to manage multiple functions and 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 web application

Note

If you want to migrate an existing Flask application, you can skip this step and go to Step 2: Generate a code package.

  1. Open a new command line window on your local machine. Run the following command to install the Flask and tabulate dependency libraries required by the application in your local environment.

    pip install Flask tabulate
  2. Create a folder named code and a file named app.py in the folder. Paste the following sample code into the file. This code implements a simple calendar service and encapsulates it as an API. When a user provides a year and month, such as January 2025, the API returns the corresponding calendar table.

    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 body and check its format.
        data = json.loads(request.stream.read().decode('utf-8'))
        if not ('year' in data and 'month' in data):
            message = "Request must be in a format such as {'year': '1999', 'month': '12'}"
            return message
    
        # Get the calendar table.
        result = print_calendar(year=int(data['year']), month=int(data['month']))
        # Print a log before returning the result to indicate that the function execution is complete.
        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)
        # Replace 0 with an empty string for days that are not in the specified month.
        cal = list(map(lambda sublist: list(map(lambda x: '' if x == 0 else x, sublist)), cal))
        # Create the table header.
        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)
    
  3. Run the python3 app.py command to start your Flask application. When the output shows Running on http://127.0.0.1:9000, your Flask application has started successfully. 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 quit
  4. Open a new terminal window and use cURL to send a sample request. If the command is executed successfully, a calendar table is returned.

    curl -X POST http://127.0.0.1:9000 -d '{"year": "2000", "month": "1"}'

    image

This completes the local project creation and testing.

Step 2: Generate a code package

Build a local code package for Function Compute. The code package must contain the following items:

  • The project files required by the web application.

  • If the dependency libraries required by the application are not in the web function's runtime environment, the code package must also include these libraries. For more information about the built-in dependency libraries in the web function runtime environment, see Built-in dependencies.

  1. Install dependencies: In the root directory of the web application project, run the following command to install the required dependency libraries (tabulate in this example) in the directory. After the command is executed, the root directory of the project contains the project files and dependency libraries.

    Note

    The Python runtime for web functions already includes the Flask library. Therefore, you only need to install tabulate to meet the requirements of the sample application.

    pip install -t . tabulate
  2. Package all files in the root directory of the project.

    Linux or macOS

    In the root directory of the project, run zip code.zip -r ./*. This command packages the project files and dependency libraries into a ZIP file. This completes the code package creation.

    Note

    Make sure that you have read and write permissions on the directory.

    Windows

    Go to the project directory, select all files, right-click the selected files, and then package them into a ZIP file. This command packages the project files and dependency libraries into a ZIP file. This completes the code package creation.

Step 3: Create a web function and deploy the code package

  1. Select the web function type: Log on to the Function Compute console. In the navigation pane on the left, choose Functions > Functions. In the top navigation bar, select the region where you want to create the function. Click Create Function, select Web Function, and then follow the prompts to create the function.

  2. Select the runtime: Select Custom Runtime > Python > Python 3.10. This runtime has built-in dependency libraries for the Flask framework.image

    Note
    • For web functions, we recommend that you use a Custom Runtime. A Custom Runtime lets you customize the startup command and listening port, and supports concurrent request handling. A Built-in Runtime does not have these features and is more suitable for handling events generated by other Alibaba Cloud services.

    • If you prefer a containerized deployment, you can also use a Custom Image to create a web function. This environment also supports concurrent requests on a single instance, but you must manage the dependencies yourself.

    • For a detailed comparison of the different runtime environments, see Runtime environment comparison.

  3. Deploy the code package: Upload the ZIP code package that you obtained in Step 2. Set the startup command to python3 app.py and the listening port to 9000. If you are migrating an existing Flask application, modify the configuration to match your application's startup command and port. For example, the default port for Flask is 5000.

    After you complete the configuration, click Create and wait for the function to be created.image

Step 4: Test the function

  1. Obtain the function URL: On the function details page, click the Triggers tab. In the configuration column of the target HTTP trigger, obtain the public endpoint.

    If you are in a test environment that does not require authentication, you can set the Authentication Method to No Authentication when you create the HTTP trigger or by modifying it afterward.

    image

  2. Test the function: You can use cURL, Postman, or any HTTP client to invoke the function and verify the functionality of the Flask application. For example, to use cURL, run the following sample command in the command line. Replace the URL with the one you obtained in the previous step.

    curl https://******.cn-hangzhou.fcapp.run -d '{"year": "2025", "month": "1"}' 

    The command returns the calendar information for January 2025. The following output is a sample.

    image

    Note
    • If you use the default HTTP trigger configuration, running the preceding sample command returns a MissingRequiredHeader error. In this case, 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 migrated an existing application, invoke your service based on the actual API operation name and parameters. For more information, see Test a function using cURL.

  3. (Optional) Access the function from a browser: The public endpoint provided by the HTTP trigger cannot be accessed from a browser because it forces a file download. This endpoint 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, be aware of other associated Alibaba Cloud services or resources, such as data stored in OSS and NAS, and any configured triggers.

To delete the function, log on to the Function Compute console. In the navigation pane on the left, choose Functions > Functions. Select a region. In the Actions column of the target function, click Delete. In the dialog box that appears, confirm that no resources, such as triggers, are associated with the function, and then confirm the deletion.

Next steps

After you create a web application and deploy it to a web function, you can perform the following operations as needed.

  • Develop code in the console: To simplify the deployment process, you can use WebIDE in the Function Compute console to develop and debug code in the cloud in real time. This method is suitable for development that requires rapid iterations because you do not need to generate a code package on your local machine. For more information, see Use WebIDE.

    Note

    WebIDE 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 using Application Center: To implement continuous integration/continuous deployment (CI/CD), you can use Application Center to automatically build and deploy your code to Function Compute in the cloud. For more information, see Use Application Center to implement continuous deployment for an existing Function Compute project.

  • Configure logging: We recommend that you configure the logging feature for your function to facilitate debugging, troubleshooting, or security audits. For more information, see Configure the logging feature.

References

For steps to add dependencies for each language, see the following documents:

Python

Deploy code packages

Node.js

Deploy code packages

Java

Compile and deploy code packages

Go

Compile and deploy code packages

C#

Compile and deploy code packages

PHP

Deploy code packages