Using an APIG Trigger

For details about the APIG event source, see Supported Event Sources.

Prerequisites

You have created an API group, for example, APIGroup_test. For details, see Creating an API Group.

Creating an APIG Trigger

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click Create Function.
  3. Set the following parameters:

    • Template: Select Create from scratch.
    • Function Name: Enter a function name, for example, apig.
    • App: Select default.
    • Agency: Select Use no agency.
    • Description: Enter additional information about the function. This parameter is optional.
    • Runtime: Select Python 2.7.
    • Handler: Enter index.handler.
    • Code Entry Mode: Select Edit code inline and enter the code shown below.
      # -*- coding:utf-8 -*-
      import json
      def handler (event, context):
          body = "<html><title>Functiongraph Demo</title><body><p>Hello, FunctionGraph!</p></body></html>"
          print(body)
          return {
              "statusCode":200,
              "body":body,
              "headers": {
                  "Content-Type": "text/html",
              },        
              "isBase64Encoded": False
          }

  4. Click Create Now.
  5. On the Triggers tab page, click Create Trigger.
  6. Configure the trigger information.

    Table 1 Trigger information

    Parameter

    Description

    Trigger Type

    Select API Gateway (APIG).

    API Name

    Enter an API name, for example, API_apig.

    API Group

    An API group is a collection of APIs. You can manage APIs by API group.

    Select APIGroup_test.

    Environment

    An API can be called in different environments, such as production, test, and development environments. API Gateway provides the environment management function, which allows you to define different request paths for an API in different environments.

    To ensure that the API can be called, select RELEASE.

    Security Authentication

    There are three authentication modes:

    • App: AppKey and AppSecret high security authentication. This authentication mode is recommended. For details, see App Authentication.
    • IAM: IAM medium security authentication. This mode grants access to public cloud users only. For details, see IAM Authentication.
    • None: No authentication. Access is granted to all users.

    Select None.

    Protocol

    There are two types of protocols:

    • HTTP
    • HTTPS

    Select HTTPS.

    Timeout (ms)

    Enter 5000.

  7. Click OK.

    Figure 1 APIG trigger
    1. The URL of the APIG trigger is https://0ed9f61512d34982917a4f3cfe8ddd5d.apig.xxx.xxx.com/apig.
    2. After the APIG trigger is created, an API named API_apig is generated on the API Gateway console. You can click the API name in the trigger list to go to the API Gateway console.

Invoking the Function

  1. Enter the URL (see Figure 2) of the APIG trigger in the address bar of a browser, and press Enter.

    Figure 2 APIG trigger URL

  2. After the function is executed, check the execution result, as shown in Figure 3.

    Figure 3 Returned result

Viewing the Execution Result

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click the name of the apig function.
  3. On the displayed function details page, click the Logs tab to query the function running logs.
  4. Click View Context in the same row as a log to view log details.