All Products
Document Center

Configure API Gateway

Last Updated: Mar 31, 2020


Terms and descriptions:

Function Compute: an event-driven compute service. Function Compute allows you to write and upload code without having to manage infrastructure such as servers. Function Compute allocates the required compute resources to elastically run your code. Fees are calculated based on the actual resources consumed. For more information, see What is Function Compute.

Fun: a deployment tool for serverless applications. It helps you manage resources of services such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file. For more information, visit Fun.

API Gateway: provides you with high-performance and highly available API hosting services. API Gateway helps you release applications deployed on services such as ECS and Container Service and provides API lifecycle management to publish, manage, and maintain APIs. You can perform simple operations to publish data or services in a quick, safe, and cost-effective manner.

Function Compute provides HTTP triggers and the local debugging feature to facilitate web application development.

However, even with these features it can still be time consuming and easy to make mistakes when you deploy a Function Compute application deployed with API Gateway to different regions. You can use Fun to achieve this task in a single click.

This topic describes how to deploy and publish API Gateway for Function Compute applications by using Fun.

We recommend that you obtain basic knowledge of Fun before you perform the following operations.

Note: This topic includes only the core code and configurations. For the detailed process, visit Fun.

Basic examples

Write function code

You must write code to construct a function. The following example shows how to write a function to display the phrase “hello world”.

  1. exports.handler = function(event, context, callback) {
  2. var response = {
  3. isBase64Encoded: false,
  4. statusCode: 200,
  5. body: 'hellow wrold'
  6. };
  7. callback(null, response);
  8. };

You can also use the code in Using Function Compute as the backend service of API Gateway to construct an advanced function.

Configure API Gateway

  1. HelloworldGroup: # Api Group
  2. Type: 'Aliyun::Serverless::Api'
  3. Properties:
  4. StageName: RELEASE
  5. DefinitionBody:
  6. '/': # request path
  7. get: # http method
  8. x-aliyun-apigateway-api-name: hello_get # api name
  9. x-aliyun-apigateway-fc: # The function to be triggered when the API operation is requested.
  10. arn: acs:fc:::services/${fc.Arn}/functions/${helloworld.Arn}/
  11. timeout: 3000

Replace fc and helloworld with your service name and function name in the x-aliyun-apigateway-fc parameter.

Publish the function

Use the fun deploy command to publish your function. The result is as follows:


You can open the displayed URL to access your function.

More examples

The Fun project provides demos and documentation related to API Gateway.

Advanced usage

For more information about configurations such as configuring HTTPS, timeout period, and whether a parameter is required, see Create an API . Fun supports parameters such as ApiName, Visibility, RequestConfig, RequestParameters, ServiceParameters, and ServiceParametersMap.

To configure by using Fun, you must rewrite API parameters in the following mapping rules:

Use hyphens (-) to connect the camelcase parameter name, convert all letters of the new name to lowercase, and then add the x-aliyun-apigateway prefix.

For example, the following parameters are rewritten in the following format:

  1. ApiName => x-aliyun-apigateway-api-name
  2. Visibility => x-aliyun-apigateway-visibility
  3. RequestParameters => x-aliyun-apigateway-request-parameters
  4. ServiceParametersMap => x-aliyun-apigateway-service-parameters-map

Some parameters can be configured in a fine-grained dimension. For example, in Create an API, the RequestParameters is a string array that contains parameters such as ApiParameterName, LocationParameterType, and Required.

The first letters of the fine-grained parameter names are in lowercase in Fun commands:

  1. ApiParameterName => apiParameterName
  2. Location => location

Mapping examples

The parameters of RequestParameters are rewritten as follows:

  1. x-aliyun-apigateway-request-parameters:
  2. - apiParameterName: 'token'
  3. location: 'Query'
  4. parameterType: 'String'
  5. required: 'REQUIRED'
  6. - apiParameterName: 'token2'
  7. location: 'Query'
  8. parameterType: 'String'
  9. required: 'REQUIRED'

Two required parameters in the Query type, token and token2, are configured in the preceding example.

Configure constant parameters and system parameters

The constant parameters can be rewritten as follows:

  1. x-aliyun-apigateway-request-parameters:
  2. - apiParameterName: 'token'
  3. location: 'Query'
  4. parameterType: 'String'
  5. required: 'REQUIRED'
  6. defaultValue: 'e'
  7. x-aliyun-apigateway-service-parameters:
  8. - serviceParameterName: 'token'
  9. location: 'Query'
  10. parameterCatalog: 'CONSTANT'

The system parameters can be rewritten as follows:

  1. x-aliyun-apigateway-request-parameters:
  2. - apiParameterName: 'CaClientIp'
  3. location: 'Query'
  4. parameterType: 'String'
  5. required: 'REQUIRED'
  6. x-aliyun-apigateway-service-parameters:
  7. - serviceParameterName: 'CaClientIp'
  8. location: 'Query'
  9. parameterCatalog: 'SYSTEM'

Configure existing APIs

You can use the following method to quickly configure an existing API in Fun.

Log on to the API Gateway console and select an API. Click Edit and then save without making any changes.

Find the RequestParameters configurations in the ModifyApi.json request:


Rewrite the parameter names by using the mapping rules.


This topic describes how to use Fun to deploy and publish API Gateway for Function Compute applications. If your application is simple, you can directly use the configurations in the preceding examples. For complex applications, you can use the mapping rules to configure.

You can use Fun to repeatedly deploy API Gateway in multiple regions, collaboratively develop applications, and control versions. We recommend that you use Fun to deploy API Gateway.