All Products
Search
Document Center

Function Compute:Install a third-party dependency for a function

Last Updated:Feb 02, 2024

Function Compute runtimes have built in common dependencies that you can reference in functions. Function Compute also supports third-party dependencies. This topic describes how to install a third-party dependency for a function.

Background

For more information about the common built-in dependencies in Function Compute runtimes, see the following topics:

Install a dependency by using a layer

Function Compute publishes dependency libraries that can be directly used. For more information, see Configure common layers for a function. If you want to obtain more common layers, see awesome-layers.

Install a dependency by using an official common layer

  • Official common layers

    After you create a function, go to the function details page. On the Configuration tab. In the left-side navigation pane, click Layers and then click Modify. In the panel that appears, choose +Add Layer > Add Official Common Layer. For more information, see Configure common layers for a function.

  • Non-official common layers

    Find the desired layer from awesome-layers and obtain the Alibaba Cloud Resource Name (ARN) of the layer. On the function details page, click the Configuration tab. In the left-side navigation pane, click the Layers tab and then click Modify. In the panel that appears, choose +Add Layer > Add Layer by ARN.

Install a dependency by building a custom layer

  • Build a layer online

    Log on to the Function Compute console. In the left-side navigation pane, choose Advanced Features > Layers. For more information, see Build a custom layer.

    Note
    • This method applies only to Node.js and Python runtimes.

    • This method does not support dependencies that contain system dynamic-link libraries with the .so extension. Example: Puppeteer dependency library for Node.js. If dependencies include dynamic-link libraries, we recommend that you use a Dockerfile to build a layer.

  • Build a layer on an on-premises machine

    You can build a custom layer on an on-premises machine. For more information, see Create a .zip file for a layer. The on-premises machine and the desired Function Compute runtime must run the same operating system and use the same processor architecture. In addition, if the operating system is not Linux x86-64, the dependency libraries cannot depend on the underlying environment or processor architecture. Otherwise, we recommend that you build layers in the Function Compute console or use a Dockerfile to build layers.

    For example, the numpy library for Python depends on its underlying environment. If your on-premises machine runs a macOS with M1 chips, you cannot install dependencies on the on-premises machine.

  • Use Dockerfile to build a layer

    If dependencies contain the underlying dynamic-link libraries or fail to be installed on your on-premises machine, you can use a Dockerfile to install the dependencies. For more information, see Use a Dockerfile to build a layer.

Use the Function Compute console to install dependencies

Package and upload dependencies to the Function Compute console

  1. Compress a third-party dependency and its code files into a .zip file.

    Important
    • You must compress all files in the code directory into the .zip file. The handler file of a function must be in the root directory of the .zip file.

    • The packaging method varies based on the OS of your on-premises machine. You can use a packaging method based on your business requirements.

  2. Log on to the Function Compute console and upload a code package by using one of the following methods to deploy the function: Upload ZIP Package, Upload Folder or Upload Package Using OSS.

Install a dependency by using a web IDE terminal

  1. Log on to the Function Compute console and find the function that you want to manage.

  2. On the details page of the function, click the Code tab. On the Web IDE page, choose Terminal > New Terminal. In the code editor, run the command that is used to install dependencies.

    The following sample code shows how to install a dependency in Python. For more information, see Deploy a code package.

    pip install -t . <PackageName> # Replace PackageName with the actual name of the dependency
  3. View the installation results. After the dependency is installed for the function, the directory in the following figure appears:install-rely