This article discusses Fun tool and fun install and how you can use these third-party dependencies to develop Function Compute.

This article shows the new feature of the Fun tool and fun install, which can help you easily install apt and pip software packages. Also, through this, you can install these third-party dependencies to the system directory of Function Compute, developing Function Compute. But before we get to all the nitty gritty stuff of this article, let's first take a look at several important concepts mentioned in this article.

Background Information

First, there's Function Compute, which is an event-driven service that allows users to write and upload code without having to manage server health or consider some other factors. Function Compute prepares and auto scales to the necessary amount of computing resources to run user code. The user only pays for the resources required to run their code.

Next, there's Fun, which is a deployment tool for serverless applications. It helps you manage resources, such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file.

Last, there's also fun install, which is a sub-command of the Fun tool for installing pip and apt dependencies. It provides two forms, command line interface and fun.yml description file.

Note The tips described in this article require the Fun version 2.9.3 and later.

For Function Compute, installing third-party dependencies is a big pain point. However, Fun install solidifies the best practices into tools based on previous experiences and achievements, which is convenient for users to install dependencies.


Use the Fun Install Init

Use the fun install init command to initialize a fun.yml file in the current directory. This step is not necessary if you plan to write fun.yml and run tasks in batches using the "fun install" command, init is a good start.

Run the fun install init command in the root directory of the Function Compute project, and choose a runtime.
fun install init
Select runtime (Use arrow keys)
A fun.yml file is generated in the current directory. The content is as follows:
runtime: python2.7
tasks: []

Install pip package dependency

The following command installs the Python TensorFlow package
fun install --runtime python2.7 --package-type pip tensorflow
skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
Task => [UNNAMED]
     => PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow
Below is a description of the parameters in the above code script:
  • --runtime specifies the runtime. If the fun.yml file has been initialized, this option can be omitted because the runtime is declared in the fun.yml.
  • --package-type specifies the type of dependency to install. pip and apt are the current two optional values.
  • tensorflow is a pip package name.

This command is run in the container provided by fc-docker. The commands run inside the container are printed line by line. For example, the PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow command is actually run, too.

After the installation has complete, a .fun directory is generated, the runable files are placed in the .fun/python/bin directory, and the library files are placed in the .fun/python/lib/python2.7/site-packages.
└── python
    ├── bin
    │   ├── freeze_graph
    │   ├── markdown_py
    │   ├── pbr
    │   ├── saved_model_cli
    │   ├── tensorboard
    │   ├── tflite_convert
    │   ├── toco
    │   └── toco_from_protos
    └── lib
        └── python2.7
            └── site-packages
                ├── tensorboard
                ├── tensorboard-1.12.2.dist-info
                ├── tensorflow
                ├── tensorflow-1.12.0.dist-info
                ├── termcolor-1.1.0.dist-info
Compared with the previous pip install -t . <package-name> method, the storage location of installed files for the "fun install" command is more organized, and the dependency files and code files are separated, which is convenient to clean up, and initialize the dependency files with OSS or NAS after splitting. However, this also brings a new issue, which is that it requires the user to customize the environment variable library file before it can be found by the program. For the convenience of users, the fun install env is provided to print the necessary environment variables.
fun install env

If you use fun local and fun deploy for debugging and deployment, you do not need to worry about environment variables, which have been set up for you.

Use --save for Persistence

After the --save parameter is added to the install command, the command is persisted as a task, and saved to the fun.yml file.
fun install --runtime python2.7 --package-type pip --save tensorflow
skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
Task => [UNNAMED]
     => PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow
After the --save parameter is added to the above command, check the contents of fun.yml:
runtime: python2.7
  - pip: tensorflow
    local: true
Then, directly run fun install without parameters to run the tasks in sequence.
fun install
skip pulling image aliyunfc/runtime-python2.7:build-1.2.0...
Task => [UNNAMED]
     => PYTHONUSERBASE=/code/.fun/python pip install --user tensorflow
Use -v to Display Detailed Logs
fun install -v
skip pulling image aliyunfc/runtime-python3.6:build-1.2.0...
Task => [UNNAMED]
     => apt-get update (if need)
Ign stretch InRelease
Get:1 stretch-updates InRelease [91.0 kB]
Get:2 stretch-backports InRelease [91.8 kB]
Get:3 stretch/updates InRelease [94.3 kB]
Hit stretch Release.gpg
Hit stretch Release
Get:4 stretch-updates/main Sources [3911 B]

Install apt package dependency

For Function Compute, using apt-get to install dependencies is another common installation problem. The fun install command can also be used for installation purposes.
fun install --runtime python3 --package-type apt libzbar0
skip pulling image aliyunfc/runtime-python3.6:build-1.2.0...
Task => [UNNAMED]
     => apt-get update (if need)
     => apt-get install -y -d -o=dir::cache=/code/.fun/tmp libzbar0
     => bash -c 'for f in $(ls /code/.fun/tmp/archives/*.deb); do dpkg -x $f /code/.fun/root; done;'
     => bash -c 'rm -rf /code/.fun/tmp/archives'

The usage method and its parameters of this command are similar to that of the pip package dependency. You only need to set --package-type to apt, and the package name to the deb package name that can be installed by apt-get.

Use fun.yml

fun.yml is composed of a group of tasks. When you run the fun install command, tasks are executed in sequence to achieve batch installation.

The file format of fun.yml is as follows:
runtime: python3
  - name: install libzbar0
      apt: libzbar0
    local: true
  - name install Pillow by pip
      pip: Pillow
    local: true
  - name: just test shell task
      shell: echo '111' > 1.txt

The runtime field is required. Currently, three types of tasks are available: apt, pip, and shell. The fun.yml file is stored in the directory to which the function codeUri in the template.yml file pointed. If multiple functions are declared in the template.yml and stored in different codeUri directories, multiple fun.yml files need to be created.

The name field of all tasks is optional. If the name field is not entered, the output is as follows:
Task => [UNNAMED]

apt/pip tasks

The tasks of apt and pip type are subtypes of install tasks. The description format is similar to the following:
name: install libzbar0
apt: libzbar0
local: true
The preceding task description is equivalent to the following command:
fun install --package-type apt libzbar0

During installation using fun install, use the --save parameter to generate the description structure of the above task in the fun.yml file of the current directory.

The local field defaults to true, indicating that the dependency will be installed in the .fun subdirectory of the current directory, and will be packed together in the zip package. Set it to false to install the dependency into the system directory. This is generally used to compile the dependency. For example, if an execution file or library is required at compile or build time but not at run time, then local: false can be set, so that the file will be ignored during packaging, and the file size of the final zip package will not be affected.

shell tasks

shell tasks are designed for source code-based installation scenarios.
name: install from source
shell: ./ --disable-report-builder --disable-lpsolve --disable-coinmp


The following is an example of python3 implementing the deployment of a simple QR code identification program to Function Compute. In this example, the pip pyzbar library is used to identify the QR code. The pyzbar library relies on the libzbar0 library installed by apt-get. The pip Pillow library is required to load images. The fun.yml file is described as follows:
runtime: python3
  - apt: libzbar0
    local: true
  - pip: Pillow
    local: true
  - pip: pyzbar
    local: true
Use fun install to install dependencies
fun install
skip pulling image aliyunfc/runtime-python3.6:build-1.2.0...
Task => [UNNAMED]
     => apt-get update (if need)
     => apt-get install -y -d -o=dir::cache=/code/.fun/tmp libzbar0
     => bash -c 'for f in $(ls /code/.fun/tmp/archives/*.deb); do dpkg -x $f /code/.fun/root; done;'
     => bash -c 'rm -rf /code/.fun/tmp/archives'
Task => [UNNAMED]
     => PYTHONUSERBASE=/code/.fun/python pip install --user Pillow
Task => [UNNAMED]
     => PYTHONUSERBASE=/code/.fun/python pip install --user pyzbar
The content of the template.yml file is as follows:
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
    Type: 'Aliyun::Serverless::Service'
      Type: 'Aliyun::Serverless::Function'
        Handler: index.handler
        Runtime: python3
        Timeout: 60
        MemorySize: 128
        CodeUri: .
The content of the file is as follows:
from pyzbar.pyzbar import decode
from pyzbar.pyzbar import ZBarSymbol
from PIL import Image

def handler(event, context):
    img ='./qrcode.png')
    return decode(img, symbols=[ZBarSymbol.QRCODE])[0].data
Use fun local to execute locally
fun local invoke pyzbar-fun
skip pulling image aliyunfc/runtime-python3.6:1.2.0...

RequestId: 964980d1-1f1b-4f91-bfd8-eadd26a307b3          Billed Duration: 630 ms         Memory Size: 1998 MB    Max Memory Used: 32 MB

Thalassiodracon is the output result after identification.


This article introduces a new feature of the Fun tool, fun install, which allows you to easily install apt and pip software packages. For engineering requirements of multiple installations, you can consider persisting the installation steps as the fun.yml file. The fun.yml file provides more features than the command line, and shell tasks can be written to support source code installation. The dependency can be installed to the system directory by setting local: false to deal with the situation of compiling dependency instead of running dependency.