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:
exports.handler = function(event, context, callback) {
callback(null, 'hello world');
};
- Function name
exports.handler
must correspond to the “Handler” of the created function. For example, if the Handler is set toindex.handler
upon the function creation, Function Compute automatically loads thehandler
function defined inindex.js
.
- 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
.
- 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
- 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.
- The context parameter contains the operation information of the function (such as the request ID and temporary Accesskeys). The parameter is of the
- 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 returnsHandledInvocationError
; otherwise, the function returns the data information. If the data type isBuffer
, the data is directly returned. If the data type isobject
, 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.
- The callback parameter returns the result of a called function. Its signature is
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:
def my_handler(event, context):
return 'hello world'
- Function name
my_handler
must correspond to the “Handler” field of the created function. For example, if the Handler is set tomain.my_handler
upon the function creation, Function Compute automatically loads themy_handler
function defined inmain.py
.
- 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 isbytes
.
- 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
- 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.
- The context parameter contains the operation information of the function (such as the request ID and temporary Accesskeys). The parameter is of the
- 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:
package example;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.StreamRequestHandler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class HelloFC implements StreamRequestHandler {
@Override
public void handleRequest(
InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
outputStream.write(new String("hello world").getBytes());
}
}
- 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 behandleRequest
.
- 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
- Implemented interface
- Your code must implement the pre-defined interface. In the previous example,
StreamRequestHandler
is implemented, whereininputStream
is the data imported when the function is called, andoutputStream
is used to return the function execution result.
- Your code must implement the pre-defined interface. In the previous example,
- 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.
- The context parameter contains the operation information of the function (such as the request ID and temporary Accesskeys). The parameter is of the
- Return value
- The function that implements the
StreamRequestHandler
interface returns execution results by using theoutputStream
parameter.
- The function that implements the
The dependency of the com.aliyun.fc.runtime
package can be referenced in the following pom.xml:
<dependency>
<groupId>com.aliyun.fc.runtime</groupId>
<artifactId>fc-java-core</artifactId>
<version>1.0.0</version>
</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:
rockuw-MBP:hello-java (master) $ ls -lrt
total 16
-rw-r--r-- 1 rockuw staff 7690 Aug 31 19:45 hellofc.jar
>>> mkf hello-java -t java8 -h example.HelloFC::handleRequest -d ./functions/hello-java
>>> invk hello-java
hello world
>>>