All Products
Search
Document Center

API Gateway:Use API Gateway to trigger a web function in Function Compute

Last Updated:Sep 27, 2024

This topic provides a quick guide on how to create an API in API Gateway and call the API to invoke a web function in Function Compute. In the call, the AppCode of an application is used according to the Alibaba Cloud App authentication method.

Process

You must perform the following steps in sequence:

  1. Create a web function

  2. Create a backend service

  3. Create an API group

  4. Create an API

  5. Create an application and authorize the application to call the API

  6. Debug the API

  7. Call the API

Create a web function

  1. Create a web function in Function Compute. For more information, see Create a web function.

    Important

    When you create the web function, take note that the function must reside in the same region as your API Gateway instance, Runtime must be set to Java 8, and Code Upload Method must be set to Use Sample Code.

  2. On the Functions page of the Function Compute console, click the created function to go to its details page. Click the Configurations tab. In the left-side navigation tree, click Triggers. Find the trigger that you want to use and copy its internal endpoint https://***-helloworld-**.cn-hangzhou.fcapp.run in the Configurations column.

Create a backend service

API Gateway allows you to configure backend services as resources of a region to simplify management. You need only to create a backend service in the API Gateway console and configure different URLs for the backend service for different environments. Then, you can directly select the configured backend service to create an API.

  1. Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs > Backend Services. On the page that appears, click Create Backend Service in the upper-right corner.

  2. In the Create Backend Service dialog box, set Name to FC-backend, Type to Function Compute, Version to Function Compute V3.0, and Function Type to HTTP Function. Then, click Confirm.

  3. On the Backend Services page, click the created FC-backend service. On the page that appears, select the Production tab and then click Create in the upper-right corner.

  4. In the Basic Information section of the page that appears, set Trigger Path to https://***-helloworld-**.cn-hangzhou.fcapp.run that is copied earlier in the Create a web function step. Then, click Publish.

    image

Create an API group

APIs are managed in API groups. Before you create an API, you must create an API group.

  1. Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs < API Groups. On the API Groups page, click Create Group in the upper-right corner.

  2. In the Create Group dialog box, select an instance from the Instances drop-down list, set Group Name to FC-Group and BasePath to /, and then click Confirm.

    image

    Note

    The API group must be in the same region as the Function Compute function.

Create an API

  1. Log on to the API Gateway console. In the left-side navigation pane, choose Manage APIs > API Groups.

  2. On the API Groups page, find the created FC-test group and click Manage APIs in the Actions column.

  3. On the APIs page, click Create API in the upper-right corner.

  4. In the Basic Information step of the Create API wizard, configure the following parameters and click Next.

    Parameter

    Example

    Group

    FC-Group

    API Name

    FC-test

    Security Authentication

    Alibaba Cloud App

    AppCode Authentication

    Enable AppCode Authentication (Header & Query)

    Signature Algorithm

    HMAC_SHA256

    image

  5. In the Define API Request step, configure the following parameters and click Next.

    image

  6. In the Define Backend Service step, configure the following parameters and click Next.

    Parameter

    Example

    Configuration Mode

    Use Existing Backend Service

    Backend Service Type

    Function Compute

    Version

    Function Compute V3.0

    Function Type

    HTTP Function

    Backend Service

    FC-backend

    Backend Request Path

    /

    HTTP Method

    GET

    Backend Service Timeout Period

    10000

    image

    Note

    In this step, you configure the type and URL of the backend service to which API Gateway sends the requests that are received from a client and how parameters are mapped and processed. In this example, the Backend Service Type parameter is set to Function Compute.

  7. In the Define Response step, configure the parameters on the page based on your business requirements and then click Create.

  8. In the message that appears, click Publish.

  9. In the Publish API dialog box that appears, set Environment to Release, enter remarks in the Remarks section, and then click Publish.

    image

Create an application and authorize the application to call the API

An application is an identity that you use to call an API. In the Create an API step of this example, the authentication method is set to Alibaba Cloud App. Therefore, after the API is published, you must create an application and grant the application the permissions to call the API.

Create an application

  1. Log on to the API Gateway console. In the top navigation bar, select a region. In the left-side navigation pane, choose Call APIs > Apps.

  2. On the Apps page, click Create App in the upper-right corner.

  3. In the Create App dialog box, set App Name to fcAPP and click Confirm.

    image

  4. On the Apps page, click the created fcApp application. On the App Details page, you can see that two authentication methods are available: AppKey and AppCode. The AppKey method includes a pair of AppKey and AppSecret, which works similarly to a pair of username and password. When you call the API, you must pass the AppKey as a parameter. The AppSecret is used to calculate signatures. API Gateway verifies the AppKey-AppSecret pair to authenticate your identity.

    image

Authorize the application to call the API

  1. Log on to the API Gateway console. In the top navigation bar, select a region. In the left-side navigation pane, choose Manage APIs > APIs.

  2. On the APIs page, find the created FC-test API and choose image > Authorize in the Actions column.

  3. On the Authorize page, set the Stage parameter to Release. Enter the name of the application you created in the search bar of the Choose Apps for Authorization section. In the search result, select the created application, click Add in the Actions column, and then click Confirm. A message appears to inform you that the application is authorized to call the API.

    image

Debug the API

API Gateway supports online debugging. We recommend that you use this feature to check whether an API is correctly configured before you call this API on clients.

  1. Log on to the API Gateway console. In the left-side navigation pane, choose Call APIs > Debug.

  2. On the Debug page, select the FC-test API that you created, set Verification Method to Use AppCode, and then click Send Request. The following information indicates that the configuration is successful.

    image

Call the API

By performing the preceding steps, you created and published the API, created and authorized an application to call the API, and debugged the API. In this step, you can use the AppCode provided by API Gateway to call a published API from a business system. For more information about how to call an API, see Call an API in simple authentication mode. In this example, cURL is used to call the API.

curl -i -X GET "http://fd6f8e2b7bf44ab181a56****-cn-hangzhou.alicloudapi.com" -H "Authorization:APPCODE 7d2b7e4945ce44028ab00***"

The following figure shows the result of an API call.

image