edit-icon download-icon

Function entry definition

Last Updated: Mar 21, 2018

This section describes the runtime envrionments currently supported by Function Compute and how to write basic functions. Function Compute supports the following languages:

Nodejs

Function Compute currently supports the following Node.js runtime environments:

  • Nodejs6 (runtime = nodejs6)
  • Nodejs8 (runtime = nodejs8)

Function handler: index.handler

The format of handler is “[File name].[Function name]”. For example, if handler specified during function creation is index.handler, Function Compute automatically loads the handler function defined in index.js.

When Nodejs runtime is used, a Node.js function must be defined. A simple function is defined as follows:

  1. exports.handler = function(event, context, callback) {
  2. callback(null, 'hello world');
  3. };
  1. Function name
    • exports.handler must correspond to the “Handler” of the created function. For example, if the Handler is set to index.handler upon the function creation, Function Compute automatically loads the handler function defined in index.js.
  2. event parameter
    • The event parameter is the data passed to function invocation call. Function Compute doesn’t do any transformation but just passes through the value to user function. The parameter type is Buffer.
  3. context parameter
    • The context parameter contains the operation information of the function (such as the request ID and temporary Accesskeys). The parameter is of the object type. The Node.js guide section describes the context structure and usage.
  4. callback parameter
    • The callback parameter returns the result of a called function. Its signature is function(err, data). Same as callback that is frequently used in Node.js, its first parameter is error and second parameter is data. If err is not blank when the function is called, the function returns HandledInvocationError; otherwise, the function returns the data information. If the data type is Buffer, the data is directly returned. If the data type is object, the data is converted into a JSON string and then returned. If the data is of another type, it is converted into a string and then returned.

Python

Function Compute currently supports the following Python running environments:

  • Python 2.7 (runtime = python2.7)
  • Python 3.6 (runtime = python3)

Function handler: main.my_handler

The format of handler is “[File name].[Function name]”. For example, if handler specified during function creation is main.my_handler, Function Compute automatically loads the my_handler function defined in main.py.

When Python runtime is used, a Python function must be defined. A simple function is defined as follows:

  1. def my_handler(event, context):
  2. return 'hello world'
  1. Function name
    • my_handler must correspond to the “Handler” field of the created function. For example, if the Handler is set to main.my_handler upon the function creation, Function Compute automatically loads the my_handler function defined in main.py.
  2. event parameter
    • The event parameter is the data passed to function invocation call. Function Compute doesn’t do any transformation but just passes through the value to user function. The parameter type in Python2.7 is str and in Python3 is bytes.
  3. context parameter
    • The context parameter contains the operation information of the function (such as the request ID and temporary Accesskeys). The parameter is of the FCContext type. The Python guide section describes the context structure and usage.
  4. Return value
    • The return value of a function is returned to you as the result of invoking the function. It can be of any type. For a simple type, Function Compute converts the result to a string before returning it. For a complicated type, Function Compute converts the result to a JSON string before returning it.

Java

Function Compute currently supports the following Java runtime environment:

  • OpenJDK 1.8.0 (runtime = java8)

Function handler: example.HelloFC::handleRequest

The format of handler is {package}.{class}::{method}. For example, if the package name is example and the class name is HelloFC, the handler specified during function creation is example.HelloFC::handleRequest.

When Java runtime is used, a class must be defined and a pre-defined interface must be implemented. A simple function is defined as follows:

  1. package example;
  2. import com.aliyun.fc.runtime.Context;
  3. import com.aliyun.fc.runtime.StreamRequestHandler;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. public class HelloFC implements StreamRequestHandler {
  8. @Override
  9. public void handleRequest(
  10. InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
  11. outputStream.write(new String("hello world").getBytes());
  12. }
  13. }
  1. Package name/Class name
    • A package and a class can have custom names, but their names must correspond to the “handler” field of the created function. In the previous example, the package name is “example” and the class name is “HelloFC”. Therefore, the handler specified during function creation is example.HelloFC::handleRequest. The format of “handler” is {package}.{class}::{method}. For now, the method name has to be handleRequest.
  2. Implemented interface
    • Your code must implement the pre-defined interface. In the previous example, StreamRequestHandler is implemented, wherein inputStream is the data imported when the function is called, and outputStream is used to return the function execution result. For more information about the function interfaces, see Function interfaces.
  3. context parameter
    • The context parameter contains the operation information of the function (such as the request ID and temporary Accesskeys). The parameter is of the com.aliyun.fc.runtime.Context type. The Java guide section describes the context structure and usage.
  4. Return value
    • The function that implements the StreamRequestHandler interface returns execution results by using the outputStream parameter.

The dependency of the com.aliyun.fc.runtime package can be referenced in the following pom.xml:

  1. <dependency>
  2. <groupId>com.aliyun.fc.runtime</groupId>
  3. <artifactId>fc-java-core</artifactId>
  4. <version>1.0.0</version>
  5. </dependency>

Before a function is created, the fc-java-core and other dependencies must be compiled into a JAR package. After the JAR package is created, use the fcli or console to create function with the package. The following uses fcli as an example:

  1. rockuw-MBP:hello-java (master) $ ls -lrt
  2. total 16
  3. -rw-r--r-- 1 rockuw staff 7690 Aug 31 19:45 hellofc.jar
  4. >>> mkf hello-java -t java8 -h example.HelloFC::handleRequest -d ./functions/hello-java
  5. >>> invk hello-java
  6. hello world
  7. >>>
Thank you! We've received your feedback.