Function Compute provides template code that is suitable for different scenarios. You can directly use the template code or modify the template code based on your business requirements. This topic describes how to use a function template and configure template parameters.

Background information

Function Compute provides various function templates. You can view template details on the Use Template tab of the Create Function page in the Function Compute console. You must configure triggers for specific templates before you can use the templates, such as the template that is used to back up Object Storage Service (OSS) incremental data. The following template list shows the information about each template:

alimebot-nodejs: Intelligent Robot

This template provides a basic function structure. Developers of Dialog Studio (DS) can use this template for coding.

  • Input:
    • event: the parameter information passed by upstream nodes.
    • context: the context information, such as dialog context.
  • Output: result. You can use asynchronous callbacks to read the results.

image-crawler: Image crawler

This template is used to crawl all JPG image files from the URL that you specify and save these files to OSS.

Note Before you use this function, make sure that the following operations are complete:
  • Input:
    • URL: the URL from which you want to crawl JPG images.
    • EndPoint: the endpoint of the OSS bucket.
    • BucketName: the name of the OSS bucket.
  • Output: All JPG files in the URL are uploaded to the specified OSS bucket.
    • ok: The operation is complete.
    • error: An error occurs.

image-recognition: Image recognition

When you upload images to OSS, you can trigger the Function Compute service to recognize the images. Then, the images are classified and stored in different OSS buckets based on the result of image recognition.

Note Before you use this function, make sure that the following operations are complete:
  • Input:
    • EndPoint: the endpoint of the OSS bucket.
    • BucketName: the name of the OSS bucket.
  • Output: Images are classified and stored in different OSS buckets based on the result of the image recognition.
    • ok: The operation is complete.
    • error: An error occurs.

flask-web: Deploy a Flask project on Function Compute

This template allows you to access functions by using URLs to implement serverless deployment of Flask web projects.

  • Input: Trigger this function by using HTTP requests.
  • Output: After the function is executed, a web HTML page is opened.

load-large-code-package: Load large code packages

This template uses the initializer function to upload large code packages in Function Compute. If the size of your code package exceeds the upper limit of the Function Compute system, you can upload part of the code package to OSS in advance, and load the code packages in the initializer function. Function Compute ensures that the initializer function is executed only once before the function logic is executed. This way, all code is loaded before requests arrive and the features and performance of the function logic are not affected.

Note Before you use this function, make sure that the following operations are complete:
  • Before you load code for the initializer function, configure the following environment variables:
    • Bucket: the OSS bucket in which the code package is stored.
    • CodeObject: the name of the code package object.
    • Endpoint: the endpoint of the region where the code package is stored.
    • CodePath: the directory in which the code package is stored after the code package is downloaded from OSS and decompressed.
  • Add the AliyunOSSReadOnlyAccess permission in the Service Role field. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
  • Input: You can configure the input parameters based on your business requirements.
    • Initializer: the initializer function.
    • handler: the handler function.
  • Output: You can configure the output parameters based on your business requirements.

aliyun-config-python3: Configure audit rules

This template is a custom rule template for Cloud Config. You can use this template to create custom compliance rules for continuous compliance of your cloud infrastructure.

api-gateway-nodejs 12: Obtain API Gateway strings

This template is a backend implementation of API Gateway. You can use this function to obtain different response content, including HTML pages, JSON files, and images.

  • Input:
     {
        "queryParameters": {
            "response": "html|json|image"
        }
    }
  • Output: HTML pages, JSON files, or images.
    html, or json, or image

copy-oss-object-python3: Back up incremental OSS data

You can use this template to back up files in a specified OSS bucket to other resources.

Note Add the AliyunOSSReadOnlyAccess permission in the Service Role field. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
  • Input: Trigger this function by using OSS events.
  • Output: After this function is executed, OSS files are backed up in an asynchronous manner.

data-lake-analytics: Data lake practices

This template uses Function Compute to call Data Lake Analytics (DLA) that uses the serverless architecture to query and analyze OSS data and return the results to ApsaraDB RDS for MySQL. You can use Function Compute together with DLA to schedule and process lightweight data in the cloud. For more information, see Data Lake Practices Based on Data Lake Analytics.

  • Input: You can configure the input parameters based on your business requirements.
  • Output: You can configure the output parameters based on your business requirements.

get-object-meta: Obtain the properties of an OSS object

You can upload an object whose name contains a specified prefix or suffix to an OSS bucket. Function Compute uses the event parameter to obtain region, bucket, and object names. Then Function Compute writes the metadata of the object to a file and uploads the file to a specified OSS bucket.

Note Before you use this function, make sure that the following operations are complete:
  • Input: Call the PostObject API operation of OSS to upload objects and then trigger the execution of a function. The function obtains the region, bucket, and names of the uploaded objects from the input event parameter.
  • Output: A file that is used to record the metadata of the uploaded objects is generated and uploaded to a specified OSS bucket.

get-oss-md5-python3: Obtain the MD5 hash value of an OSS file

You can use this template to calculate the MD5 hash value of an OSS file. You can read files in streaming mode to improve computing efficiency.

Note Add the AliyunOSSReadOnlyAccess permission in the Service Role field. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
  • Input:
    {
      "bucket": "my-bucket",
      "object": "my-object"
    }
  • Output: the MD5 hash value of a specified file.

logstore-replication: Back up logs

You can use this function to subscribe to real-time data from Log Service and copy the data to another Logstore. For more information, see aliyun-log-fc-functions.

Note Before you use this function, make sure that the following operations are complete:
  • Enable Log Service.
  • Add the AliyunLogFullAccess permission in the Service Role field. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
  • This function reads data and writes the data to a Logstore in batches. We recommend that you specify a memory size that is greater than 384 MB for the function. If the log traffic of the Logstore is large, we recommend that you set the timeout period greater than 120s.
  • Input: After you add information, such as the shard cursor information about the Logstore, you can enter a command in function event to specify the locations where the function starts and stops to read data. You can add additional configurations to implement custom function logics. For example, you can configure the function to process data and specify the destination where data is written to. When data is written to a shard, Log Service triggers function execution at regular intervals.
  • Output: If the function is executed, the data from the source Logstore is processed and then written to the destination Logstore. A serialized string in the JSON format is returned, including ingestLines (number of lines of read logs), ingestBytes (number of bytes of read logs), shipLines (number of lines of output logs), and shipBytes (number of bytes of output logs).

    If an exception occurs during the execution of a function, such as data read/write failures on a Logstore, the function is terminated.

marketplace-weather-demo: Use Alibaba Cloud Marketplace

You can use the service packages in Alibaba Cloud Marketplace by using Function Compute. In this template, the weather forecast, air quality, clothing index, and other services in Alibaba Cloud Marketplace are used to show how to use the template function. These services are provided by vendors in Alibaba Cloud Marketplace, free of charge.

Notice You must activate the weather forecast service in Alibaba Cloud Marketplace and obtain the AppCode of the service in the Alibaba Cloud Marketplace console.
  • Input: the AppCode that is provided by the service vendor.
  • Output: data such as overall weather forecast and air quality that is returned in the JSON format.

oss-download-zip-upload: Download and compress OSS images

You can use this function to download images from an OSS bucket, compress the images into a ZIP file, and upload the ZIP file to a specified bucket.

Note Add the AliyunOSSFullAccess and AliyunLogFullAccess permissions in the Service Role field. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
  • Input:
    • EndPoint: the endpoint of the OSS bucket.
    • BucketName: the name of the OSS bucket.
  • Output:

    ZipFileSize: the size of the compressed file. Unit: bytes.

oss-get-object-info: Obtain the information about an object

You can use this function to obtain details about an object in a specified OSS bucket.

Note Add the AliyunOSSReadOnlyAccess and AliyunLogFullAccess permissions in the Service Role filed. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
  • Input:
    • EndPoint: the endpoint of the OSS bucket.
    • BucketName: the name of the OSS bucket.
  • Output:
    • last_modified: the time when the object was last updated.
    • key: the name of the object.

oss-shipper-csv: Create a CSV file and save the file in OSS

This function is used to subscribe to the real-time data of Log Service. This function extracts the values of the configured log fields and uses the values to create a CSV file. You can directly save this CSV file in OSS or use Snappy or GZIP to compress the file before you write the file to OSS. For more information, see oss-shipper-csv functions.

Note Before you use this function, make sure that the following operations are complete:
  • Input: After you add information, such as the shard cursor information about the Logstore, you can enter a command in function event to specify the locations where the function starts and stops to read data. You can add additional configurations to implement custom function logics. For example, you can configure the function to process data and specify the destination where data is written to. When data is written to a shard, Log Service triggers function execution at regular intervals.
  • Function output: The function processes the data of the source Logstore into a CSV file and writes the file to OSS.

read-write-tablestore-nodejs12: Read data from and write data to Tablestore

You can use this function to call the PutRow operation to write data to Tablestore and call the GetRow operation to read data from Tablestore.

Note Before you use this function, make sure that the following operations are complete:
  • Input: You can configure the input parameters based on your business requirements.
  • Output: The response after you call the GetRow operation is returned.

read-write-tablestore-python3: Read data from and write data to Tablestore

You can use this function to call the PutRow operation to write data to Tablestore and call the GetRow operation to read data from Tablestore.

Note Before you use this function, make sure that the following operations are complete:
  • Input: You can configure the input parameters based on your business requirements.
  • Output: The response after you call the GetRow operation is returned.

return-apigateway-string: Obtain the strings returned from API Gateway

You can use this function to obtain the parameters that are transmitted from API Gateway, including HTML pages, JSON files, and images.

  • Input: APIPara, which is the request parameter and can be configured in API Gateway.
  • Output: api_rep, which is the standard response string of API Gateway.