Author: Rakugo | Alibaba Cloud Serverless Technology R&D

background

Alibaba Cloud's function computing product [ 1] supports the HTTP trigger [2 ] capability at an early stage, allowing users to use the HTTP protocol to make function calls. The Function Compute backend provides services for all clients to respond to HTTP trigger calls through a shared APIServer component, and needs to rely on the Path in the URL to route client traffic to the client's function container. The HTTP request Path received in the container will carry the routing identifier of Function Compute. If the customer deploys a REST-style application in Function Compute, the 404 problem will be encountered.

 title=

In the beginning, Function Compute was not designed for customers to run small to medium scale applications. Function Compute provides a native REST architecture that treats each function as an independent resource. Usually, a function is only responsible for a small piece of functionality, that is, an API. If a function only corresponds to one API, then there is no need to implement a set of routing logic in the function code to respond to requests of different URL Path paths.

 title=

Function Compute has introduced Custom Runtime/Custom Container Runtime [3 ] functions in the past two years. Customers can directly run their existing applications on Function Compute without splitting their applications according to the framework recommended by Function Compute. The development habit of customers and mature projects in the community is to use MVC and other architectures to develop a large number of REST APIs in one program, route according to the HTTP Path in the message in the process, and "forward" requests from different paths to different method or function for processing.

 title=

In this context, customers can run existing REST applications in Function Compute, but the applications cannot provide external services normally. Customers spend a lot of energy to transform the existing applications, and this transformation is only necessary for function calculation, which is a typical product design of the platform to carry users.

Calling a function with fcapp.run

In order to solve the above problems, and to be compatible with existing functions and customer habits, Function Compute assigns an independent domain name to each newly created HTTP trigger, such as {random-string}.cn-shanghai.fcapp.run . Using this domain name to access Function Compute, Function Compute will route according to the domain name and forward traffic to the function container to avoid intrusiveness to client code.

 title=

Local web testing with fcapp-test.run

Due to the influence of the policy in mainland China, the main domain name of Function Compute cannot provide customers with website-type services on the Internet, and all function request results will be converted to download behavior [4 ] . For pure API type functions, we believe that converting the request result to a download has no effect. But for the function of the website property, the returned HTML text and JavaScript code strongly depend on the browser's interpreter to display properly. We judge that it is a strong requirement that developers can see the page returned by the function in real time.

In production scenarios, we recommend that customers bind registered domain names to functions to solve this problem, and there are more concise solutions in the test environment. During the testing phase, this problem can be temporarily bypassed by testing the domain name fcapp-test.run and adding local host resolution. The request result will not be converted to download behavior, and web page debugging can be performed normally.

 # 1. 从页面获取fcapp.run的域名
FC_DOMAIN='wordpress-xxxxx-serverlordpress-ydziwvakfn.cn-shenzhen.fcapp.run'
FC_TEST_DOMAIN=`echo ${FC_DOMAIN} | sed 's/fcapp.run/fcapp-test.run/g'`
echo "FC域名: ${FC_DOMAIN}"
echo "FC测试域名: ${FC_TEST_DOMAIN}"

# 2. 查询域名解析的IP
FC_IP=`ping ${FC_DOMAIN} -c 1 | HEAD -1 | awk '{print $3}' | sed 's/[():]//g'`
echo "FC IP: ${FC_IP}"

# 3. 修改host文件,将测试域名的本地解析指向fcapp.run的解析
#    如果没有权限需要手动加
sudo -- sh -c  "printf '\n${FC_IP} ${FC_TEST_DOMAIN}\n' >> /etc/hosts"
cat /etc/hosts

# 4. 使用测试域名在浏览器访问函数
curl -v "${FC_TEST_DOMAIN}"

Students who understand the TCP protocol and the HTTP protocol can easily understand the technical details behind it. When the client initiates an HTTP request, it will first query the IP address corresponding to the domain name through domain name resolution, and initiate a TCP connection. Next, the HTTP Request message will be sent to the server through the established TCP connection. If the client directly initiates a TCP connection to Function Compute's server and sends the domain name that can be associated with the function to Function Compute, then Function Compute can return what the client expects.

Function Compute is processed in the backend and is compatible with access to the fcapp-test.run domain name, but does not provide authoritative DNS resolution. The customer only needs to configure the host locally or use the self-built DNS server to resolve the domain name of fcapp-test.run to the Function Compute Server, and then the function can be tested normally. This solution not only meets the requirements of laws and regulations in mainland China, but also meets the needs of customers to debug pages at a very low cost, which greatly optimizes the customer's research and development experience in function computing.

Run the official wordpress image using Function Compute

Through the above solutions, Function Compute supports customers to migrate their REST applications without modifying any business code. The following uses the official wordpress mirror for instructions.

Preconditions

  1. Open a Function Compute account.
  2. Dump the wordpress image to Alibaba Cloud Container Image Service.
  3. Create or use an existing mysql instance, and initialize the corresponding account and database for the wordpress service. If you use an Alibaba Cloud RDS instance, make sure that the VPC configuration of the function service is the same as the VPC configuration of the RDS instance, and the whitelist configuration is correct.

Create corresponding functions and services

Use the official wordpress image creation function, and set the listening port in the container to 80.

 title=

Inject database configuration

Modify the function configuration and inject the mysql database configuration into the function container in the form of environment variables.

 title=

No need to record, test the function of wordpress

View the domain name assigned by the created trigger [5 ] , configure it according to the above document, and use the fcapp-test.run domain name for testing.

 title=

 title=

Go live with your function

External provision of website type services can only be achieved through registered domain names. Function Compute users can configure the function of custom domain name [6 ] , bind the domain name to the function, and use their own domain name to provide external services. At the same time, you can also use any standard gateway type cloud products or open source products, and use the intranet domain name of fcapp.run to launch your functions.

summary

As one of the iconic products of serverless technology, function computing has long focused on improving the application scenarios of products. The introduction of the fcapp.run domain name and related product features marks a big step forward for function computing in REST and Web scenarios. From now on, customers can deploy REST applications on function computing with zero transformation, and the experience of R&D testing and the officially launched solutions have been excellent. Students are very welcome to use function computing and provide valuable opinions.

Related Links

[1] Function Compute Products

https://fcnext.console.aliyun.com/overview

[2] HTTP trigger

https://help.aliyun.com/document_detail/71229.html

[3] Custom Runtime/Custom Container Runtime

https://help.aliyun.com/document_detail/132044.html

[4] Download behavior

https://help.aliyun.com/document_detail/71229.html

[5] Assigned domain name

https://help.aliyun.com/document_detail/74769.html#h3-url-2

[6] Custom domain name

https://help.aliyun.com/document_detail/90763.html

Click here to go to the official website of Function Computing to see more!


阿里云云原生
1k 声望302 粉丝