All Products
Search
Document Center

Function operation

Last Updated: May 30, 2019

Functions are associated with services. One service can have multiple functions that share the log and role information configured for the service. However, the functions remain independent of each other. For more information about functions in Function Compute, see Create a function.

Contents

This topic describes how to create, update, query, or delete a function using the console or the command-line tool fcli.

Manage functions in the console

Create function

The process of creating a function using the console is as follows:

  1. Log on to the console.

  2. Select a service.

  3. Create a function for the service.

  4. Select a template. Function Compute provides various function templates to help you quickly build an application. For more information, see Use template. In most scenarios, you can select an empty function template.

  5. To create a trigger for a function, you can either create the trigger when creating the function, or after the function is created. For more information, see Create a trigger.

  6. Set the service name, function name, and runtime environment.

  7. (Optional) Set the Environment variables and function parameters.

  8. (Optional) If the function execution requires a permission currently unavailable to the service role, grant the role that permission. For more information about permissions, see RAM.

  9. Verify that all settings are correct and click Create.

Update function

On the function details page, you can view the details of a function in the Function Properties area.

![Update a function](http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/pic/73338/intl_en/1533628761642/EditFunctions.png)

Delete function

Before you delete a function, make sure that no trigger is configured for the function.

Delete a function

Manage functions by using fcli

Create function

mkf: Creates a function.

  • -b —code-bucket string: Specifies the OSS bucket for the code.
  • -o —code-object string: Specifies the object key of the code in the bucket.
  • -d —code-dir string: Specifies the directory of the code.
  • -f —code-file string: Specifies the compressed code file.
  • -h —handler string: Sets a function handler in the “File name.Function name” format. For example, if you set the handler argument to hello_world.handler, the handler function in the hello_world.js script file is used as the handler.
  • -m —memory int32: Specifies the memory size for function execution.
  • -t —runtime string: Specifies the language used in function execution.
  1. // The code is stored in the service directory.
  2. // //The code is stored in OSS. In the code, -t specifies the runtime environment, -h specifies the function handler, -b specifies the OSS bucket of the code, and -o specifies the object key of the code in the bucket.
  3. >>> mkf myFunction -t nodejs6 -h myFunction.handler -b ossBucketName -o objectKey
  4. // The code is stored locally. In the code, -d specifies the code directory and -m sets the memory size for function execution.
  5. >>> mkf myFunction -t nodejs6 -h myFunction.handler -d codeDir/myFunction -m 512
  6. // The code is stored in the local code.zip file.
  7. >>> mkf myFunction -h myFunction.handler -f code.zip -t nodejs6

Update function

upf: Updates a function. The parameters of this function are the same as the parameters of mkf.

  1. // The code is stored in the service directory.
  2. // The code is stored in OSS. In the code, -t specifies the runtime environment, -b specifies the OSS bucket of the code, and -o specifies the object key of the code in the bucket.
  3. >>> upf myFunction -t nodejs6 -h myFunction.handler -b ossBucketName -o objectKey
  4. // The code is stored locally. In the code, -d specifies the code directory and -m sets the memory size for function execution.
  5. >>> upf myFunction -t nodejs6 -h myFunction.handler -d codeDir/myFunction -m 512
  6. //The code is stored in the local code.zip file.
  7. >>> upf myFunction -h myFunction.handler -f code.zip -t nodejs6

Query function

Run the ls command to list all functions of the service.

Run function

invk: Triggers a function execution.

  • -e —encode: Performs Base64 encoding on the return value of the function.
  • -f —event-file string: Reads the event information from the file.
  • -s —event-str string: Reads the event information from the string.
  • -o —output-file string: Specifies the name of the file to which the return value is written.
  • -t —type string: Specifies the trigger type. Sync indicates synchronous trigger and Async indicates asynchronous trigger. The default value is Sync.
  1. // If no input parameters or trigger events are required, you can directly call a function.
  2. >>> invk myFunction
  3. //Perform Base64 encoding on the return value of the function.
  4. >>> invk myFunction -e
  5. // Read the event information from the string.
  6. >>> invk myFunction -s 'hello,world'
  7. // Read the event information from the file.
  8. >>> invk myFunction -f event.json
  9. //Write the return value to the result.txt file.
  10. >>> invk myFunction -o code/result.txt
  11. // Set the trigger type to Async.
  12. >>> invk myFunction -t Async

Delete function

rm: Deletes a function. Before you delete a function, make sure that no trigger is configured for the function.