Function Compute supports HTTP triggers, which allow you to invoke functions by using gRPC requests. In this case, the function acts as a gRPC server to process gRPC requests and return the results to the client. This topic describes how to configure an HTTP trigger in the Function Compute console and use a gRPC request to invoke the function.
Usage notes
Invocation methods
Domain name: You can call gRPC functions by using a subdomain of
fcapp.runor a custom domain name. The old domain namefc.aliyuncs.comis not supported.NoteIf you use a custom domain name, you must configure a mapping between the request path and the function. We recommend that you set the request path to
/*. This forwards all gRPC requests to the corresponding gRPC function, which then routes them to the appropriate client-defined gRPC methods.Port: gRPC requests use port
8089.
Transport security
To ensure the security of gRPC requests, the Function Compute production environment supports only clients that use TLS. Otherwise, the request fails with the error
rpc error: code = Unavailable desc = connection closed before server preface received.Custom domain names support custom HTTPS certificates. The gRPC server does not need to perform TLS certificate validation because the Function Compute gateway layer performs the validation.
Request timeout
The maximum timeout for a gRPC request cannot exceed the function's Execution Timeout Period. The execution timeout is 60 seconds by default, with a maximum of 86,400 seconds.
Concurrency control
gRPC requests are controlled by the concurrency of Function Compute. A gRPC request is counted as one concurrent request. gRPC is based on the HTTP/2 protocol. In Function Compute, gRPC requests that are routed to a function instance reuse the same HTTP/2 connection. The number of concurrent streams on this connection is equal to the concurrency of the instance. You can set the concurrency per instance for a function to control the number of concurrent streams on an instance. For more information, see Set instance concurrency.
Load balancing
Function Compute distributes gRPC requests to different instances, automatically performing load balancing for them.
Billing
There are four types of gRPC requests:
Unary gRPC requests
Client-side streaming requests
Server-side streaming requests
Bidirectional streaming requests
The billing method varies based on the request type:
Unary gRPC requests: The billing method is the same as for standard HTTP requests.
For a function with a concurrency of 1, billing starts when the gRPC connection is established and ends when it terminates.
For a function with a concurrency greater than 1, instance billing starts when the first gRPC connection is established and ends when the last gRPC connection terminates. You are not charged multiple times for concurrent connections within the same instance.
For example, consider a function with a concurrency of 2. The first request arrives at T1 and ends at T3, and the second request arrives at T2 and ends at T4. The total billed duration is from T1 to T4. The overlapping period from T2 to T3 is billed only once.
Client-side streaming, server-side streaming, and bidirectional streaming requests: Instance billing starts when the first gRPC connection is established and ends when the last gRPC connection terminates.
Before you begin
Prepare the gRPC function code
You can write the code yourself or, after you install and configure the Serverless Devs tool, run the s init fc-custom-golang-grpc command to download a complete set of sample code written in the Go language for running a gRPC service in Function Compute. The complete directory structure of the code is as follows, where the ./greeter_client directory is the client-side code and the ./code directory is the server-side code.
fc-custom-golang-grpc
├── build-image
│ └── Dockerfile
├── certificate
├── code
│ ├── bootstrap
│ └── main.go
├── go.mod
├── greeter_client
│ ├── main
│ └── main.go
├── Makefile
├── privatekey
├── proto
│ ├── helloworld_grpc.pb.go
│ ├── helloworld.pb.go
│ └── helloworld.proto
├── readme.md
├── s_en.yaml
└── s.yamlPrepare a ZIP package
In the fc-custom-golang-grpc project directory, run make deploy. This generates the binary file bootstrap in the ./code directory. Package it as bootstrap.zip.
Install dependencies
In the fc-custom-golang-grpc project directory, run go mod vendor to install the gRPC client's dependencies.
Deploy a function with the console
Prerequisites
Procedure
Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, click Create Function.
On the Create Function page, select Use Custom Runtime, set the relevant parameters, and then click Create.
Configure the following required parameters and keep the defaults for other parameters. For more information, see Create a function.
Function Name: Enter a name for the function, such as grpc-demo.
Handler Type: Select HTTP Handler.
Runtime: Select Debian 9.
Code Upload Method: Select Upload ZIP.
Code Package: Select and upload your packaged
bootstrap.zipfile.Startup Command: If left empty,
./bootstrapis executed by default.Listening Port: Set this to the same port that your server listens on. In this example, the value is 8089.
On the function details page, click the Triggers tab and view the public endpoint of the trigger.

Run the following command to invoke the gRPC client and send a gRPC request to test the function.
go run ./greeter_client -addr grpc-demo-service-*********.cn-qingdao.fcapp.run:8089
You can also configure a custom domain name for the gRPC function and use it for invocations.
Deploy a function with Serverless Devs
Prerequisites
Procedure
Run the following command to initialize the project:
s init fc-custom-golang-grpc -d fc-custom-golang-grpcRun the following command to enter the
fc-custom-golang-grpcproject directory:cd fc-custom-golang-grpcOptional: Edit the s.yaml file.
The following code shows an example:
edition: 1.0.0 name: hello-world-app # access specifies the key information required by the current application. # For information about how to configure keys, see: https://www.serverless-devs.com/serverless-devs/command/config # For information about the order in which keys are used, see: https://www.serverless-devs.com/serverless-devs/tool# access: "default" vars: # Global variables region: "cn-hangzhou" # The region where you want to deploy the function. service: name: "grpc-demo" # The name of the service where your function is located. description: 'hello world by serverless devs' internetAccess: true services: helloworld: # A custom service or module name. # To perform an operation only on the helloworld module, add helloworld to the command, such as 's helloworld build'. # If you run 's build' without specifying a module, the tool sequentially builds all modules at the same level as helloworld in the YAML file. component: fc actions: # For information about how to use custom execution logic (actions), see: https://www.serverless-devs.com/serverless-devs/yaml pre-deploy: # Run before deployment. - run: make build path: ./ # - component: fc build --use-docker --dockerfile ./code/Dockerfile # The component to run. Format: [component: ComponentName Command Arguments]. You can run 's cli registry search --type Component' to get a list of components. # - run: docker build xxx # The system command to run, similar to a hook. # path: ./src # The path where the system command/hook is executed. # - plugin: myplugin # The plugin to run. You can run 's cli registry search --type Plugin' to get a list of plugins. # args: # The arguments for the plugin. # testKey: testValue # post-deploy: # Run after deployment. # - component: fc versions publish # The command to run. props: region: ${vars.region} service: ${vars.service} # logConfig: # project: mypro-dev # logstore: function-log function: name: "golang-grpc" # The name of the function to deploy. description: 'hello world by serverless devs' timeout: 30 memorySize: 512 runtime: custom codeUri: ./code instanceConcurrency: 3 caPort: 8089 triggers: - name: http2Trigger type: http config: authType: anonymous # The trigger method for the HTTP trigger. You must configure the POST method. methods: - GET - POST # customDomains: # - domainName: auto # protocol: HTTP,HTTPS # routeConfigs: # - path: /* # serviceName: "grpc-demo" # functionName: "golang-grpc" # certConfig: # certName: certtest # certificate: ./certificate # privateKey: ./privatekeyRun
s deploy -yto deploy the function.After the execution is complete, the function is deployed to Function Compute. In addition, Function Compute generates a directly accessible URL. You can use this URL to call the function for testing.
Run the following command to install the gRPC client's dependencies:
go mod vendorRun the following command to invoke the gRPC client and send a gRPC request to test the function.
Use the public endpoint of the HTTP trigger. The following example shows the command:
go run ./greeter_client -addr golang-grpc-grpc-demo-torcawakky.cn-qingdao.fcapp.run:8089
More examples
Custom runtime | Custom container |
N/A |
FAQ
Function errors
The client error rpc error: code = Internal desc = server closed the stream without sending trailers indicates that the Function Compute server abnormally closed the gRPC request. This is a function error, such as a function timeout, an unexpected exit of the function process, or an out-of-memory (OOM) error. You can check the function logs to find the specific cause of the error and troubleshoot the issue. For more information, see View invocation logs.
Non-TLS gRPC requests
You must use a TLS-enabled client to send gRPC requests. Otherwise, the request fails with the error rpc error: code = Unavailable desc = connection closed before server preface received. The following code shows an example in Golang:
var opts []grpc.DialOption
cred := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: false,
})
opts = append(opts, grpc.WithTransportCredentials(cred))
conn, err := grpc.Dial(*addr, opts...)You can set InsecureSkipVerify to true to skip TLS certificate validation, or set it to false to enforce it.
More information
In addition to the Function Compute console, you can also configure triggers by using an SDK. For more information, see SDK Reference (Recommended on 2021-04-06).
To modify or delete triggers, see Manage triggers.