All Products
Search
Document Center

Function Compute:Configure an HTTP trigger that invokes a function with HTTP requests

Last Updated:Aug 06, 2024

Function Compute supports HTTP triggers. Functions configured with HTTP triggers can be invoked by HTTP requests. The functions receive and process the HTTP requests, and then return responses to callers. This topic describes how to configure an HTTP trigger that invokes a function with HTTP requests in the Function Compute console.

Before you start

Create a function

Step 1: Create a trigger

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

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Configurations tab. In the left-side navigation pane, click Triggers. Then, click Create Trigger.

  4. In the trigger creation panel, configure parameters and click OK.

    Parameter

    Description

    Example

    Trigger Type

    The type of the trigger that you want to create. Select HTTP Trigger.

    HTTP Trigger

    Name

    The name of the HTTP trigger.

    http-trigger

    Version or Alias

    The function version or alias for which you want to create the HTTP trigger. The default value is LATEST. If you want to create an HTTP trigger for another version or alias, select the version or alias from the Version or Alias drop-down list. For more information about versions and aliases, see Manage versions and Manage aliases.

    LATEST

    Request Methods

    The methods that can be used to trigger the HTTP trigger.

    GET, POST, PUT, DELETE

    Disable Internet URL

    Whether to allow access to the HTTP trigger by using the public endpoint. By default, this parameter is set to No.

    If you select Yes, no default public endpoint is provided for the HTTP trigger. In this case, the access denied due to function internet URL is disable error is reported if you use the public endpoint to invoke the function. You can still invoke the function by using a custom domain name.

    No

    Authentication Method

    The method that is used to authenticate HTTP requests. Value description:

    • No Authentication: Function Compute supports anonymous access to the function and does not authenticate HTTP requests. All users can invoke the function by using HTTP requests.

    • Signature Authentication: Identity authentication is required for HTTP requests. For more information about the sample code for signature authentication, see Access the URL of an HTTP trigger by using a signature in a request.

    • JWT Authentication: Function Compute authenticates HTTP requests based on JSON Web Tokens (JWTs). For more information, see Configure JWT authentication for an HTTP trigger.

    No Authentication

    After the HTTP trigger is created, you can modify some parameters, such as Version or Alias, Request Methods, Disable Internet URL, and Authentication Method, of the HTTP trigger based on your business requirements.

Step 2: Write and deploy code

After the HTTP trigger is created, you can write code for the function.

On the function details page, click the Code tab, edit the function code in the code editor, and then click Deploy. You can obtain sample code in various programming languages from User Guide > Code development in the Function Compute documentation.

Step 3: Test the function

Method 1: Test the function in the Function Compute console

On the function details page, click the Code tab.

  • Synchronous invocation

    Click Test Function.

  • Asynchronous invocation

    Click the down icon next to Test Function, select Async Invocation, and then click Test Function.

After the test is complete, view the test result on the Code tab.

Method 2: Use cURL to test the function

On the function details page, click the Configurations tab. In the left-side navigation pane, click Triggers. In the configurations of the HTTP trigger, copy the public endpoint for subsequent tests:

image

  • Synchronous invocation

    Run the following command. After the command is run, the result is returned.

    curl -v https://http-***.cn-shenzhen.fcapp.run/$path
  • Asynchronous invocation

    Run the following command in the CLI. After the command is run, the execution result of the request by Function Compute is returned. If 202 is returned, the request succeeded. If another status code is returned, an error occurred during the invocation. For more information about status codes, see Troubleshooting.

    curl -v -H "X-Fc-Invocation-Type: Async" https://http-***.cn-shenzhen.fcapp.run/$path

(Optional) Use API Gateway to protect functions

By default, Function Compute supports anonymous access to functions and does not authenticate HTTP requests. All users can invoke your functions by using HTTP requests. To prevent waste of resources and mitigate security risks caused by unauthorized access, you can enable identity authentication and connect your HTTP functions to API Gateway. You can use IP address-based access control plug-ins, JWT authentication plug-ins, or basic authentication plug-ins of API Gateway to protect your HTTP functions.

  1. In the Function Compute console, find the function that you want to manage. On the function details page, click the Configurations tab. In the left-side navigation pane, click Triggers. Then, click Modify.

  2. In the trigger modification panel, set Disable Internet URL to Yes.

  3. Log on to the API Gateway console and switch to the region in which the HTTP function resides.

  4. Create an API group. For more information, see Create an API group.

  5. Use CNAME to resolve your domain name to a second-level domain name provided by API Gateway. For more information, see Add a CNAME record.

  6. Create an API.

    The following items describe how to configure the main parameters. Retain the default values or configure custom values for other parameters based on your business requirements. For more information, see Create an API.

    • Security Authentication: Select No Authentication. Authentication will be performed based on plug-ins.

    • Request Path: Enter the root directory / and select Match All Subpaths.

    • HTTP Method: Select ANY.

    • Request Mode: Select Pass-through.

    • Backend Service Type: Select Function Compute.

    • Version: Select Function Compute V3.0.

    • Function Type: Select HTTP Function.

    • Trigger Path: Enter the internal endpoint of the HTTP trigger in Function Compute.

    • ContentType: Select Pass-through Client Content-type Header.

  7. Publish the API. Click Publish in the Actions column of the created API, select an online environment, and then click Publish.

  8. Create a backend signature plug-in. Set key and secret to the AccessKey ID and AccessKey secret of your Alibaba Cloud account. Then bind the plug-in to the API that you created. For more information, see Overview.

After you complete the preceding steps, you can access the HTTP function by using your own domain name. You can also create the following plug-ins and bind them to your API to protect your HTTP functions:

Troubleshooting

The following errors may occur when you send requests to invoke functions:

  • Request errors: A request error occurs if a request does not meet specific requirements. If a request error occurs, a 4xx code is returned in the response.

  • Function errors: A function error occurs when the function code is invalid. In this case, a 5xx code is returned.

The following table describes the scenarios in which request errors and function errors may occur. You can troubleshoot the errors based on the error causes.

Error type

HTTP status code

Cause

Billable

Request errors

400

Your request exceeds the limits on HTTP requests. For more information, see Overview.

No

400

The request for a function that requires identity authentication does not contain the date information or authorization information.

No

403

The signature of a request for a function that requires identity authentication is invalid. As a result, the authorization information is invalid. The date information is used to calculate a signature, and the signature is valid only for 15 minutes. If you use an HTTP trigger that requires access authentication, the signature becomes invalid if the date information in the request header is generated over 15 minutes ago.

No

403

Your request is sent by using a method that is not configured in the HTTP trigger. For example, if you send an HTTP request by using the POST method, but only the GET method is configured in the HTTP trigger, this error occurs.

No

404

An HTTP request is sent to a function for which no HTTP trigger is configured.

No

Traffic throttling errors

429

Your traffic is throttled. You can reduce concurrent requests or contact the technical support of Function Compute to increase the concurrency settings.

No

Function errors

502

The response of a function exceeds the limits on HTTP responses. For more information, see Overview.

Yes

502

The code of a function has a syntax error or an exception.

Yes

502

An HTTP request is sent to a function that does not use an HTTP handler function.

Yes

System errors

500

A system error occurs in Function Compute. Try again.

No

System throttling errors

503

A system throttling error occurs in Function Compute. Try again in exponential backoff mode.

No

If the issue persists, join the DingTalk group 64970014484 for technical support.