Develop Function Compute by Installing Third-Party Dependencies

By Yi Xian

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.

Procedure

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.

A fun.yml file is generated in the current directory. The content is as follows:

Install pip package dependency

The following command installs the Python TensorFlow package

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.

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.

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.

After the --save parameter is added to the above command, check the contents of fun.yml:

Then, directly run fun install without parameters to run the tasks in sequence.

Use -v to Display Detailed Logs

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.

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:

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:

apt/pip tasks

The tasks of apt and pip type are subtypes of install tasks. The description format is similar to the following:

The preceding task description is equivalent to the following command:

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.

Examples

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:

Use fun install to install dependencies

The content of the template.yml file is as follows:

The content of the index.py file is as follows:

Use fun local to execute locally

Thalassiodracon is the output result after identification.

Summary

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.

Original Source

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store