All Products
Search
Document Center

Function Compute (2.0):Use fcapp.run to run RESTful applications

Last Updated:Feb 28, 2024

Function Compute can assign the subdomain fcapp.run to new HTTP triggers. You can use the domain name to access your RESTful applications. This topic describes how to use fcapp.run to access your RESTful applications without modifying code.

Background

Function Compute supports HTTP triggers, which allow functions to be invoked with HTTP requests. Function Compute uses a shared API server component to respond to invocations of HTTP triggers. When you invoke a function, the path in the URL is depended on to route customer traffic to function containers of customers. Paths of HTTP requests received in the container contain route identifiers of Function Compute. If you deploy a RESTful application in Function Compute, a 404 error is reported.

image

Initially, Function Compute provided a native RESTful architecture in which each function worked as an independent resource. In this case, each function worked as an API operation to implement specified tasks. If a function corresponds to only one API operation, you do not need to use a set of routing logic in the function code to respond to requests from different URL paths.

image

Function Compute has introduced custom runtimes and Custom Container runtimes for functions. You can run your existing applications in Function Compute and do not need to split your applications based on recommended architectures in Function Compute. Developers tend to develop a large number of RESTful API operations in programs. The in-process routing logic is used to forward requests from different paths so that the requests are processed by using different methods.

image

Use fcapp.run to invoke a function

Function Compute assigns each newly created HTTP trigger a separate domain name in the following format {random_string}.{region_id}.fcapp.run. When you use the domain name to access Function Compute, Function Compute routes traffic based on the domain name and forwards the traffic to the function container. This way, the routing does not rely on paths, and the customer code is not affected.

Note

For example, if the /function1, /function2, and /function3 APIs are encapsulated in the function corresponding to the HTTP trigger, the following URLs are called by fcapp.run:

  • {random_string}.{region_id}.fcapp.run/function1

  • {random_string}.{region_id}.fcapp.run/function2

  • {random_string}.{region_id}.fcapp.run/function3

Use fcapp-test.run to debug a web page on an on-premises machine

The root domain for Function Compute is aliyuncs.com, which cannot be used to provide website services. All function request results are downloaded as attachments in browsers. For webpage functions, the returned HTML text and JavaScript code cannot be displayed as expected. In this case, you can resolve this issue by binding a custom domain name that has obtained the ICP filling for the function. For more information, see Overview.

You can also resolve the preceding issues by using the test domain fcapp-test.run and adding a domain resolution entry to the hosts file. Then, request results are no longer downloaded as attachments and you can perform debugging on webpages as expected. The following sample code provides an example of the script:

# 1. Obtain the domain name of fcapp.run from the page.
FC_DOMAIN='serviceName-functionName-vtnazzbxrr.cn-hangzhou.fcapp.run'
FC_TEST_DOMAIN=`echo ${FC_DOMAIN} | sed 's/fcapp.run/fcapp-test.run/g'`
echo "Function Compute domain name: ${FC_DOMAIN}"
echo "Function Compute test domain name: ${FC_TEST_DOMAIN}"

# 2. Query the IP address that is obtained after the domain name resolution.
FC_IP=`ping ${FC_DOMAIN} -c 1 | HEAD -1 | awk '{print $3}' | sed 's/[():]//g'`
echo "FC IP: ${FC_IP}"

# 3. Modify the hosts file to resolve the test domain name to the IP address for fcapp.run.
# If you do not have permissions, you need to manually add the content.
sudo -- sh -c  "printf '\n${FC_IP} ${FC_TEST_DOMAIN}\n' >> /etc/hosts"
cat /etc/hosts

# 4. Use the test domain name to access the function in the browser.
curl -v "${FC_TEST_DOMAIN}"
            

Use Function Compute to run WordPress images

Function Compute allows you to migrate your RESTful applications without modifying the code. In this section, a WordPress application is migrated.

Before you start

  • Function Compute

  • ApsaraDB RDS

    Note
    • You can also use an existing ApsaraDB RDS for MySQL instance and initialize the account and database for the WordPress service.

    • When you create an ApsaraDB RDS for MySQL instance, make sure that the VPC configured for the instance is the same as that of the service in Function Compute, and the CIDR block of the vSwitch configured for the service in Function Compute is added to the whitelist. For more information about network settings of Function Compute, see Configure networks. For more information about how to configure a whitelist for an ApsaraDB RDS for MySQL instance, see Step 1: Configure the IP address whitelist for the database.

  • Container Registry

    Note

    You can push and pull images by using an instance of Container Registry Personal Edition or Enterprise Edition. In this section, a Container Registry Personal Edition instance is used as an example.

Procedure

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.

  2. In the top navigation bar, select a region. On the Services page, click the desired service.

  3. On the Functions page, click Create Function and select Use Container Image. Configure the parameters and click Create.

    Select a WordPress image and set Listening Port to 80. For more information, see Create a Custom Container function.

    image.png

  4. Modify configurations of the function and inject database configurations into the function container in the form of environment variables.

    For more information about how to configure environment variables, see Environment variables.

    image.png

    The mapping between the environment variables and the database configurations is as follows:

    • WORDPRESS_DB_HOST: the internal endpoint of the database that you created for the ApsaraDB RDS for MySQL instance.

    • WORDPRESS_DB_NAME: the database account that you created.

    • WORDPRESS_DB_PASSWORD: the password of the database account that you created.

    • WORDPRESS_DB_USER: The name of the database that you created.

  5. Enter the domain name that is assigned by Function Compute to the HTTP trigger in the address bar of the browser to test the function.

    After the function is created, Function Compute automatically creates an HTTP trigger. You can obtain the domain name from the configurations of the trigger. The API corresponding to the WordPress application is called by using the domain name as the root path.

    Click the following address before you use it for tests. In the panel that appears, click to download the configuration script corresponding to the on-premises system and run the script. You can access the address through a browser only after you run the script.

    image.png

    image.png

References