This topic shows you how to use Funcraft to deploy an API Gateway and functions in Function Compute. For simple functions, you can use the configurations provided in this topic. For complex functions, you can use the mapping rules described in this topic to customize the configurations.

Background information

Function Compute provides HTTP triggers and supports on-premises debugging. This helps you use Function Compute to develop web applications. For more information, see Overview and Run the fun local command to debug functions on premises. In some cases, functions in Function Compute that connect to an API Gateway need to be deployed to different regions. Manual deployment is complicated and prone to errors. You can use Funcraft to deploy an API Gateway and functions in Function Compute to all regions with a few clicks.

Before you use Funcraft to publish an API Gateway and functions in Function Compute, we recommend that you read the topics related to Funcraft. For more information, see Features.

Note You can use Funcraft to repeatedly deploy an API Gateway in multiple regions, collaboratively develop applications, and control versions. We recommend that you use Funcraft to deploy an API Gateway. This topic provides only the core code or configurations. For more information about the complete sample code, see funcraft.
The following terms are involved in this topic:
  • Function Compute: a fully managed and event-driven computing service. When you use Function Compute, you can focus on writing and uploading code. You do not need to purchase and manage infrastructure resources such as servers. Function Compute prepares elastic computing resources for you to run your code. You are charged only for the resources that are consumed. For more information, see What is Function Compute?.
  • Funcraft: a deployment tool for serverless applications. Funcraft can help you manage the resources of services such as Function Compute, API Gateway, and Log Service. You can use Funcraft to develop, build, and deploy applications by defining resources in the template.yml resource configuration file. For more information, see funcraft.
  • API Gateway: provides you with high-performance and highly available API hosting services. API Gateway helps you publish applications deployed on Alibaba Cloud services, such as Elastic Compute Service (ECS) and Container Service. API Gateway allows you to publish APIs and maintain the lifecycle of APIs. You can perform simple operations to publish data or services in a fast, safe, and cost-efficient manner.

Basic examples

  1. Run the following code to write a function. The function can be a simple function whose input is hello world or involve complex program. For more information, see Use Function Compute as the backend service of an API operation.
    exports.handler = function(event, context, callback) {
      var response = {
          isBase64Encoded: false,
          statusCode: 200,
          body: 'hello world'
      };
      callback(null, response);
    }; 
  2. Add the following configurations to configure an API Gateway:
    HelloworldGroup: # Api Group
      Type: 'Aliyun::Serverless::Api'
      Properties:
        StageName: RELEASE
        DefinitionBody:
          '/': # request path
            get: # http method
              x-aliyun-apigateway-api-name: hello_get # api name
              x-aliyun-apigateway-fc: # The function to be triggered when the API operation is requested. ARN: acs:fc:::services/${fc.Arn}/functions/${helloworld.Arn}/
                timeout: 3000                        
    Note Replace fc and helloworld with your service name and function name in the x-aliyun-apigateway-fc parameter.
  3. Run the fun deploy command to publish your function. You can open the displayed URL to access your function.
    fun deploy

Advanced usage

For more information about configurations such as configuring HTTPS and a timeout period, see Create an API. Funcraft supports parameters such as ApiName, Visibility, RequestConfig, RequestParameter, ServiceParameter, and ServiceParametersMap.

If you edit a Funcraft configuration file based on the Create an API topic, you must understand the mapping rules between API and Funcraft parameters. Rewrite API parameters based on the following mapping rules:
  • The parameter name must be in lowercase.
  • Hyphens (-) are used to connect the camelcase parameter name.
  • The x-aliyun-apigateway prefix must be added.

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

ApiName => x-aliyun-apigateway-api-name
Visibility => x-aliyun-apigateway-visibility
RequestParameters => x-aliyun-apigateway-request-parameters
ServiceParametersMap => x-aliyun-apigateway-service-parameters-map            
Some parameters can be configured in a fine-grained dimension. For example, the RequestParameter parameter described in the Create an API topic is a string array that contains parameters such as ApiParameterName, Location, ParameterType, and Required. You can convert the parameters in the RequestParameter parameter into Funcraft parameters based on the following examples:
ApiParameterName => apiParameterName
Location => location            

Mapping examples

The parameters in the RequestParameter parameter are rewritten for Funcraft in the following configuration:
x-aliyun-apigateway-request-parameters:
  - apiParameterName: 'token'
    location: 'Query'
    parameterType: 'String'
    required: 'REQUIRED'
  - apiParameterName: 'token2'
    location: 'Query'
    parameterType: 'String'
    required: 'REQUIRED' 

Constant parameters and system parameters

You can configure parameters based on the Create an API topic and the mapping rules.
  • The following part shows the configuration of constant parameters:
    x-aliyun-apigateway-request-parameters:
      - apiParameterName: 'token'
        location: 'Query'
        parameterType: 'String'
        required: 'REQUIRED'
        defaultValue: 'e'
    x-aliyun-apigateway-service-parameters:
      - serviceParameterName: 'token'
        location: 'Query'
        parameterCatalog: 'CONSTANT'  
  • The following part shows the configuration of system parameters:
    x-aliyun-apigateway-request-parameters:
      - apiParameterName: 'CaClientIp'
        location: 'Query'
        parameterType: 'String'
        required: 'REQUIRED'
    x-aliyun-apigateway-service-parameters:
      - serviceParameterName: 'CaClientIp'
        location: 'Query'
        parameterCatalog: 'SYSTEM'   

Configure existing APIs

You can perform the following steps to efficiently configure an existing API in Funcraft:

  1. Log on to the API Gateway console.
  2. In the left-side navigation pane, choose Publish APIs > APIs. Find the API that you want to configure in Funcraft and click its name. Click Edit in the upper-right corner. Then, Click Save without making changes.
  3. Find the ModifyApi.json request. Rewrite the names of parameters such as RequestParamter based on the mapping rules.